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)

Try this too
#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;

}
OUTPUT

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

firstNode, secondNode, thirdNode are normal variables of structlinkList
  • but *head is a pointer variable

int a,b,c, *e;

Where a, b, c are int variables but e is a pointer variable that can point to another int.

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;

first.nextNode is pointing to second, and second.NextNode pointing to third

Now the head is pointing to the first node, which means it has access to firstNode’s data and nextNode.

Linked List in C

What is the Linked List Program in C
What is the Linked List Program 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.

END

Leave a Reply

x