简体   繁体   中英

Hassle in Linkedlist in C

Suppose I have 3 nodes already in the list (ie 10 , 20). And I want to insert 30. So wrote as below

struct node *p,*temp;
p=start;
temp=(struct node*)malloc(sizeof(struct node));
temp->info=30;
temp->link=NULL;
while(p-link!=NULL) 
{
  p=p->link;
}
p->link=temp;

It worked perfectly until I do this: while(p!=NULL) rest is same.. Similarly in case of showing the node I wrote as follows

while(p!=NULL)
    {
        printf("%d \n",p->info);
        p=p->link;
    }

This also worked fine until I changed it to: while(p->link!=NULL)

I want to know what is happening why is not working? Give me the reason why we are using while(p!=NULL) in case of showing the all the data and while(p->link!=NULL) in case of inserting any node?

A linked list, in C is a struct that contains data and a pointer to the next element (and maybe a pointer to the previous element, but that doesn't seem to be your case). It would be something like this:

struct node {
    int data; //could be any type
    struct node *next;
};

To insert elements in the end of the list, you need to go to the end of it - when there is no next element. To do that, you check node->next == NULL . Then you make node->next = new_node

To find an element in the list, you need to loop for all elements until you find it. If the element doesn't exists in the list, you need to prevent the user from accessing an invalid element, so you test node == NULL to finish the loop safely. This case is also applicable if you want to just print all data in the list - you need to reach all elements, the test would be the same (if you were testing node->next == NULL , you would skip the last element).

Before the first while loop, you've got something like this. Two nodes (not three as you state) in a linked-list, pointed to by start and a new node, pointed to by temp :

start-->+---------+  +-->+---------+      +---------+<--temp
        |info:10  |  |   |info:20  |      |info:30  |
        |link:xxxx|--+   |link:NULL|      |link:NULL|
        +---------+      +---------+      +---------+

To add the new node to the end of the list, you need to advance p from the first node (pointed to by start ) until p points at the node whose link is NULL (ie, until p->link!=NULL fails to be true):

start-->+---------+  +-->+---------+      +---------+<--temp
        |info:10  |  |   |info:20  |      |info:30  |
        |link:xxxx|--+   |link:NULL|      |link:NULL|
        +---------+      +---------+      +---------+
                            ^
                            |
                         p -+

Once you have found the last node, you then "plumb" in your new node to get a three-element list:

start-->+---------+  +-->+---------+  +-->+---------+
        |info:10  |  |   |info:20  |  |   |info:30  |
        |link:xxxx|--+   |link:yyyy|--+   |link:NULL|
        +---------+      +---------+      +---------+

However, when you're printing all of the nodes, you start p at the first node (ie start ) and print the details of every node until p has been set to NULL (ie p!=NULL fails to be true). If you stopped when p->link was NULL , you would not have printed the last node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM