What is the Linked List Program in C
What is the Linked List? What is the Linked List Program in C In the C programming Language linked list is a sequence of DS (data structures), which are connected with each other via the links. A linked list is a sequence of links that contain items. Each link contains a connection to another link. the Linked list is the second most-used data structure after array.
Linked List Program in C
#include <stdio.h> struct linkList { int data; struct linkList* nextNode; }; int main() { struct linkList firstNode, secondNode, thirdNode, head; firstNode.data=10; secondNode.data=20; firstNode.nextNode=&secondNode; thirdNode.data=30; secondNode.nextNode=&thirdNode; thirdNode.nextNode=NULL; head.nextNode=&firstNode; while(head.nextNode!=NULL){ printf("Data= %d ",head.nextNode->data); head.nextNode=head.nextNode->nextNode; } return 0; }
Linked list Insertion
Recursive function program In the C (clikhere)
#include <stdio.h> struct linkList { int data; struct linkList* nextNode; }; int main() { struct linkList firstNode, secondNode, thirdNode, *head; firstNode.data=10; secondNode.data=20; firstNode.nextNode=&secondNode; thirdNode.data=30; secondNode.nextNode=&thirdNode; thirdNode.nextNode=NULL; head=&firstNode; while(head!=NULL){ printf("Data= %d ",head->data); head=head->nextNode; } return 0; }
Data= 10
Data= 20
Data= 30
- Check this same code with *head, and try to understand the difference
And think of pointers concepts. - Pointer can have the address of a variable of the same type as the pointer variable
when the pointer type is a struct type, then if you want to access the members of struct then you have to use -> symbol.
Linked list in Datastructer
- but *head is a pointer variable
e=&a;
suppose a=10;
print (“%d”,*e) ; // it will print the value pointed by ‘ e ‘
Which is the address of a and it will print 10;
Now the head is pointing to the first node, which means it has access to firstNode’s data and nextNode.
Linked List in C
head=&firstNode; while(head!=NULL){ printf("Data= %d ",head->data); head=head->nextNode; }
loop until head not getting NULL,
initially head has firstNode, it will print its data, then we say
head=head->next;
Means head will point to nextnode ” which is the secondnode ” then it will move to the thirdnode.