Ventus Ventus - 3 years ago 201
C Question

C: Cant display linked list values

So I'm making a linked list with 3 nodes, and right now I'm trying to implement a function that displays the list in C , here is the code :

struct node *createList(struct node *start)
{
int i, n, data;


printf("Enter the number of nodes : ");
scanf("%d", &n);

if (n==0)
return start;

printf("Enter the first element to be inserted : ");

scanf("%d", &data);

printf("%d",data);

start=insertInBeginning(start, data);

for(i=2; i<=n; i++)
{
printf("Enter the next element to be inserted : ");
scanf("%d", &data);
insertAtEnd(start, data);
}
return start;
}

// end of createList


struct node *insertInBeginning(struct node *start, int data)
{
printf("we in begining \n");

struct node *new_node,*current;

printf("declared nodes");

new_node = &data;

printf("got new_node data \n");

if(start == NULL) {

printf("start is null! \n");

start = new_node;

current = new_node;

} else {
printf("start isn't null! \n");

new_node->link=start;

start=new_node;
}

}

//end of insertInBeginning


void insertAtEnd(struct node *start, int data)
{
struct node *new_node,*current,*temp;

new_node->info = data;
if(start == NULL)
{

start= new_node;
current= new_node;

} else
{
temp = start;

while(temp->link!=NULL) {
temp = temp->link;
}
temp -> link = new_node;
}


}

// end of insertAtEnd



void displayList(struct node *start)
{

printf("we in displaylist");
struct node *temp;

temp = start->link;
printf("temp has start values?");

if(temp = NULL)
{
printf("we null now \n");
} else {
printf("we NOT null \n" );
while(temp!=NULL)
{
printf("%d \n",temp->info);
temp = temp->link;
}
}
}


The problem that I am having is that the in the display function the loop completes and but it doesnt print any of the values, however i know that temp is not NULL so it is displaying something, just not what i want, is there anyway to fix this?

Answer Source

You have two big problems here: firstly, (as Martin James points out), you don't have a return from your insertInBeginning() routine, so start is set to some undefined value. You also have a huge problem that you are just casting int data to a node *. You must malloc() memory for the new node and write data into it. (And then return it) You have the same issue in your insertAtEnd() routine--you must malloc() memory to hold the new node.

static struct node *
insertAtStart (struct node *start, int data)
{
    struct node *new;

    new = malloc(sizeof(*new));
    if (new == NULL) {
        perror("malloc");
        exit(1);
    }

   new->value = data;
   new->next = start;
   return new;
}

static struct node *
insertAtEnd (struct node *start, int data)
{
    struct node *cur, *new;

    // create the node * we will be adding.  (This really ought to have been done
    // in the calling routine, so the same code would be used to create the node
    // entry and to store the value.  Note a big issue with this simple a struct,
    // but more complex structs really ought to only be created in one place, so
    // when they change, you won't have bugs where you forget to change one of 
    // the creation locations)
    new = malloc(sizeof(*new));
    if (new == NULL) {
        perror("malloc");
        exit(1);
    }
    new->value = data;
    new->link = NULL;

    // if this is going to be the only entry in the list, just return that it 
    // is the new start
    if (start == NULL) 
        return new;

    // find the last entry
    for (cur = start ; cur->link != NULL ; cur = cur->link)
        ;
    cur->link = new;
    return start;
}

You really want to turn on compiler warnings--it saves a lot of time to have the compiler tell you about these sorts of errors. If you are using gcc, add -Wall -Werror to your compile command (if you are using a different compiler, you'll need to read the man page).

(There are other gcc warnings that you can turn on, but -Wall gets you a lot of useful ones. You can read the man page to find all the ones you want if you want to turn on more)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download