-1

I'm trying to creating linear linked list recursively with c language, but keep sticking from here and the code is not working with the error "Linker Tools Error LNK2019". Sadly i can't understand what's the matter. Here is my code.

Thanks for your big help in advance.

#include <stdio.h>
#include <stdlib.h>


struct node
{
    char num;                        //Data of the node
    struct node *nextptr;           //Address of the next node
};
typedef struct node element;
typedef element *link;
link head;


void displayList();         // function to display the list

int main()
{   
    char s[] = "abc";
    link stol(s);
    {
        link head;
        if (s[0] == '\0')return(NULL);
        else {
            head = (link)malloc(sizeof(element));
            head->num = s[0];
            head->nextptr = stol(s + 1);
            return(head);
        }
    }

    printf("\n\n Linked List : To create and display Singly Linked List :\n");
    printf("-------------------------------------------------------------\n");

    displayList();
    return 0;
}


void displayList()
{
    link tmp;
    if (head == NULL)
    {
        printf(" List is empty.");
    }
    else
    {
        tmp = head;
        while (tmp != NULL)
        {
            printf(" Data = %d\n", tmp->num);       // prints the data of current node
            tmp = tmp->nextptr;                     // advances the position of current node
        }
    }
}
Backs
  • 24,430
  • 5
  • 58
  • 85
Cho Jinuk
  • 1
  • 1
  • Define `stol` function outside(and before) `main` functrion. – BLUEPIXY Jun 17 '17 at 14:27
  • Thanks. Do you mean i need to put "link stol" outside(and before) main function? – Cho Jinuk Jun 17 '17 at 14:43
  • I mean like [this](http://ideone.com/IHPo0I) – BLUEPIXY Jun 17 '17 at 14:49
  • 1
    1. Do not cast `malloc` - see https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc 2. Do not hide pointers with `typedef` – Ed Heal Jun 17 '17 at 17:07
  • Huge thanks for your help @BLUEPIXY , I'm checking the entire code that you gave me and comparing what did i do wrong. – Cho Jinuk Jun 18 '17 at 01:52
  • Thanks @Ed Heal, i checked the casting one you linked. but i can't understand the meaning of 'hide pointers with typedef' as you mentioned. Is that meaning like only using 'typedef struct node *link' not like the origin two lines? – Cho Jinuk Jun 18 '17 at 01:56
  • Do not do `typedef element *link;`, Makes debugging harder – Ed Heal Jun 18 '17 at 02:58
  • Ok, i will just write `}*link` like this at the end of struct node! Thanks – Cho Jinuk Jun 18 '17 at 06:32

1 Answers1

0

You redefine a link object called head in your main() function. It hides the global head variable.

Removing the definition inside main would fix your problem, but you should consider passing a link* as a parameter to your displayList function in any case.

I've just noticed this statement return(head); in main(). You program exits prematurely as a result as well.

Everytime I look at your app, I find more issues. If I were you, I'd start by creating a function that adds a node to the list. It's much easier to add new nodes to the front of the list, so you should try that first. Try adding to the tail once you get this running. Adding to the tail is very similar, but you have to 'walkthe list first to get to the last element, exactly as you already do indisplayList()` Another way is keeping the address of the last node* you've added to the list. Like I said, it adds a bit of complexity, so get it working with addToHead first.

void addToHead(link* l, node* n)
{
  n->nextptr = l->nextptr;
  l->nextptr = n;
}

in your main, you can allocate one new node at a time, as you already do with malloc(). Initialize its contents num with an integer, and let addToHead deal with the pointer stuff. Your use of pointers is terrible, but lists are quite easy, and addToList pretty much shows what can and what should be put in pointers - namely other pointers.

You can remove almost everything in main() before the first printf. You'll have to

  1. start loop:
  2. write a prompt so the user knows what to do using printf()
  3. read input from user using scanf("%d", &n), or equivalent.
  4. break from the loop if user enters a negative value.
  5. malloc() a new node
  6. set its data num = n
  7. call addToHead to add the node.
  8. Loop until user enters an empty string, or -1.

That should take about 8 to 10 lines of code. if in doubt, you will easily find documentation on scanf, with google or on http://en.cppreference.com/w/c.

Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • Thanks! cuz of this comment i finally arrange my whole messy conceptual thinking of 'global & local variable' by googling :) – Cho Jinuk Jun 18 '17 at 06:33