Share this post on:

Linked list in C is List which is a collection of elements. There are two ways of maintaining a list in computer memory. The first way is to take an array for sorting the elements of the list, but arrays have some disadvantages. Insertion and deletion of an element from an array requires more processing. If the number of elements in the list is less than the size of array then memory will be wasted and if the number of elements exceed the size of array then also we will have problem. The second way of implementing a list in memory is by using a self referential structure. These types of list are known as linked lists. A linked list is made up node has two parts, first part contains the information and second part contains the address of the next nodes. The address part of the last node of linked list will be NULL. The general form of node of linked list is –

struct node

{

type member1;

type member2;

struct node *link;

};

The nodes of a linked list are not stored contiguously in memory. In array list we could perform all the operations using the array name. In the case of linked list, we’ll perform all the operations with the help of a pointer that points to the first node the linked list. This pointer is generally named start and it is the only source through which we can access our linked list. The list will be considered empty if the pointer start contains NULL value.

Now we will take a linked list that contains an integer value in the information part. The structure for the nodes of this list will be defined as –

struct node

{

int info;

struct node *link;

}

Here is a linked list that has 4 node –

We can clearly see that the address of each node is stored in the link part of the previous node, and the address of first node is stored in the pointer variable start. The link part of the last node contains NULL. The pointer variable start will be declared and initialized to NULL.

Example : Program of linked list

#include<stdio.h>

#include<stdlib.h>

struct node
{
int node;
struct node *link;
};

struct node *create_list(struct node *start);
void display(struct node *start);
void count(struct node *start);
void search(struct node *start,int data);
struct node *addatbeg(struct node *start, int data);
struct node *addatend(struct node *start, int data);
struct node *addafter(struct node *start, int data, int item);
struct node *addbefore(struct node *start, int data, int item);
struct node *addatpos(struct node *start, int data, int pos);
struct node *del(struct node *start, int data);
struct node *reverse(struct node *start);

int main(void)
{
struct node *start= NULL;
int choice, data,item, pos;
while(1)
{
printf(“1.create List\n”);
printf(“2.Display\n”);
printf(“3.Count\n”);
printf(“4.Search\n”);
printf(“5.Add to empty list / Add at beginning\n”);
printf(“6.Add at end\n”);
printf(“7.Add after end\n”);
printf(“8.Add before end\n”);
printf(“9.Add at position\n”);
printf(“10.Delete\n”);
printf(“11.Reverse\n”);
printf(“12.Quit\n”);
printf(“Enter your choice : “);
scanf(“%d”,&choice);

switch(choice)
{
case 1:
start=create_list(start);
break;
case 2:
display(start);
break;
case 3:
count(start);
break;
case 4:
printf(“Enter the elements to be searched : “);
scanf(“%d”,&data);
search(start,data);
break;
case 5:
printf(“Enter the elements to be inserted : “);
scanf(“%d”,&data);
start=addatbeg(start,data);
break;
case 6:
printf(“Enter the element to be inserted”);
scanf(“%d”,&data);
start=addatend(start,data);
break;
case 7:
printf(“Enter the elements to be inserted”);
scanf(“%d”,&data);
printf(“Enter the elements after which to insert : “);
scanf(“%d”,&item);
start=addafter(start,data,item);
break;
case 8:
printf(“Enter the elements to be inserted”);
scanf(“%d”,&data);
printf(“Enter the elements before which to insert : “);
scanf(“%d”,&item);
start=addbefore(start,data,item);
break;
case 9:
printf(“Enter the elements to be inserted”);
scanf(“%d”,&data);
printf(“Enter the position at which to insert : “);
scanf(“%d”,&pos);
start=addatpos(start,data,pos);
break;
case 10:
printf(“Enter the element to be deleted”);
scanf(“%d”,&data);
start=del(start,data);
break;
case 11:
start=reverse(start);
break;
case 12:
exit(1);
default:
printf(“wrong choice\n”);
}

}
return 0;
}

In the function main(), we have taken an infinite loop and inside the loop we have written a switch statement. In the different cases of this switch statement, we have implemented different operations of linked list. To come out of the infinite loop and exit the program we have used the function exit().

The structure pointer start is declared in main() and initialized to NULL. We send this pointer to all function because start is the only way of accessing linked list. Function like those of insertion, deletion, reversal will change our linked list and value of start might change so these function return the value of start. The function like display(), count(), search() do not change the linked list so their return type is void.

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *