UNDERSTANDING LINKED LIST BY C LANGUAGE
ABOUT LINKED LIST 😊
So, Today's we are going to understand the linked list, this post is fully different from my previous post i.e. there is no link between them. But you know that my blog name is "Path For Next Generation" that's why my post doesn't consist of any particular topics. So my post contains the knowledge for next generation.
Let's get a Start towards Linked List, Basically, it is a topic which taught to students doing his/her graduation from Computer Science Engineering. Also, people of technology background knows about it, But this is the most important topic for Comp. Sc. Engineer. Those who heard about it or studied in his/her past time they can understand the concept of the linked list just by seeing the codes and it was difficult for other people.
I am putting the code below show that you can learn or understand the linked list,
Code For Singly Linked List:-
Only understanding this single program you will be able to write any program related to Singly Linked List:-
#include<stdio.h>
#include<stdlib.h>
int forsearch=-1; //Global variable
typedef struct linkedlist //declaring a structure
{
int data;
struct linkedlist *next;
}node;
void printlist(node *head) //Function to print the linked list
{
node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
if(temp->next!=NULL)
printf("->");
temp=temp->next;
}
}
void push(node **head_ref,int new_data) //Function to insert the element at start of list
{
node *new_node=(node *)malloc(sizeof(node));
new_node->data=new_data;
new_node->next=*head_ref;
*head_ref=new_node;
return;
}
void insertafter(node *prev_node,int new_data,int old_data) //Function to insert the data or new //node after some old node
{
while(prev_node!=NULL)
{
if(prev_node==NULL)
{
printf("The given previous node cannot be NULL.");
return;
}
if(prev_node->data==old_data)
{
node *new_node=(node *)malloc(sizeof(node*));
new_node->data=new_data;
new_node->next=prev_node->next;
prev_node->next=new_node;
}
prev_node=prev_node->next;
}
return;
}
void append(node **head_ref,int new_data) //Function to insert new node/data at end of list.
{
node *new_node=(node *)malloc(sizeof(node));
node *last=*head_ref; // Assigning node of list to other pointer variable.
new_node->data=new_data;
new_node->next=NULL;
if(*head_ref==NULL)
{
*head_ref=new_node;
return;
}
while(last->next!=NULL)
last=last->next;
last->next=new_node;
return;
}
void deletenode(node **head_ref, int key) //Function to delete the node from the list after knowing //data of it
{
node *temp=*head_ref;
node *prev;
if(temp!=NULL && temp->data==key)
{
*head_ref=temp->next;
free(temp);
return;
}
while(temp!=NULL && temp->data!=key)
{
prev=temp;
temp=temp->next;
}
if(temp==NULL)
return;
prev->next=temp->next;
free(temp);
}
void deletenodebypos(node **head_ref,int n) //deleting the node by knowing the position of node
{
node *temp=*head_ref;
node *prev;
if(n==1)
{
*head_ref=temp->next;
free(temp);
return;
}
int i;
for(i=1;temp!=NULL && i<n;i++)
{
prev=temp;
temp=temp->next;
}
if(temp==NULL && temp->next==NULL)
return;
prev->next=temp->next;
free(temp);
}
int getcount(node *head) //Function to count the number of nodes in the list
{
if(head==NULL)
return 0;
return 1+getcount(head->next);
}
int searchbykey(node *head,int key) //Function to search the node by knowing the data of it
{
node *current=head;
if(current==NULL)
return 0;
while(current!=NULL)
{
if(current->data==key)
return 1;
current=current->next;
forsearch++;
}
return 1;
}
void searchbyindex(node **head,int index,int x) //Function to search the node by knowing the //index of it
{
node *temp=*head;
int i;
if(index+1>x || index<0)
{
printf("Data not found!!\n");
return;
}
for(i=0;temp!=NULL && i<index;i++)
{
temp=temp->next;
}
printf("\nData at index %d is %d.",index,temp->data);
return;
}
void fromstart(node **head,int n) //Function to finding the data stored in nth position of node //from start
{
node *temp=*head;
int i;
for(i=1;temp!=NULL && i<n;i++)
temp=temp->next;
printf("\nData in node %d is: %d\n",n,temp->data);
printf("Address in node %d is: %d\n\n",n,temp->next);
return;
}
int fromend(node *head,int n,int x) //Function to finding the data stored in nth position of node //from end
{
node *temp=head;
int count=0;
while(temp!=NULL)
{
if(count==x-n)
return(temp->data);
count++;
temp=temp->next;
}
}
void swapnodes(node **head_ref,int x,int y) //Function of swapping of two nodes after knowing //the data stored in it.
{
if(x==y)
return;
node *prevX=NULL,*currX=*head_ref;
while(currX->data!=x)
{
prevX=currX;
currX=currX->next;
}
node *prevY=NULL,*currY=*head_ref;
while(currY->data!=y)
{
prevY=currY;
currY=currY->next;
}
if(currX==NULL || currY==NULL)
return;
if(prevX!=NULL)
prevX->next=currY;
else
*head_ref=currY;
if(prevY!=NULL)
prevY->next=currX;
else
*head_ref=currX;
node *temp=currY->next;
currY->next=currX->next;
currX->next=temp;
return;
}
void middle(node **head,int x) //Function to find the middle element of list
{
node *temp=*head;
int i;
if(x%2!=0)
{
for(i=0;temp!=NULL && i<x/2;i++)
temp=temp->next;
printf("Data at middle is %d\n",temp->data);
return;
}
if(x%2==0)
{
for(i=1;temp!=NULL && i<x/2;i++)
temp=temp->next;
int y=temp->data;
temp=temp->next;
printf("Data at middle is %d and %d\n",y,temp->data);
return;
}
}
void deletelist(node **head) //Function to Delete the total list
{
node *current=*head;
node *next=NULL;
while(current!=NULL)
{
next=current->next;
free(current);
current=next;
}
*head=NULL;
return;
}
void reverse(node **head) //Function to reverse the list
{
node *prev=NULL;
node *current=*head;
node *next;
while(current!=NULL)
{
next=current->next;
current->next=prev;
prev=current;
current=next;
}
*head=prev;
}
int main()
{
node *first=NULL;
int a,b,pos,n;
int x,y;
int c;
int data,new_data;
do
{
printf("Enter the data:");
scanf("%d",&data);
push(&first,data);
printf("\nWant to enter more data(1/0):\n");
scanf("%d",&c);
}while(c!=0);
printf("\n\nContents of singly linked list:\n");
printlist(first);
do{ printf("\n\nYou want to edit linked list data:");
printf("\n1) Insertion.\n");
printf("2) Deletion.\n");
printf("3) length of linked list(number of nodes).\n");
printf("4) Searching an element.\n");
printf("5) Nth node of the list.\n");
printf("6) Swapping of nodes.\n");
printf("7) Middle element of linked list.\n");
printf("8) Deleting the linked list.\n");
printf("9) Reversing the linked list.\n");
printf("\nEnter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:
printf("Enter place of insertion:");
printf("\n1) At start.");
printf("\n2) After some node.");
printf("\n3) At End.");
printf("\nEnter your choice:");
scanf("%d",&b);
switch(b)
{
case 1:
printf("Enter your data:");
scanf("%d",&data);
push(&first,data);
printlist(first);
break;
case 2:
printf("Enter data after which you want to insert:");
scanf("%d",&data);
printf("Enter your new data:");
scanf("%d",&new_data);
insertafter(first,new_data,data);
printlist(first);
break;
case 3:
printf("Enter new data:");
scanf("%d",&new_data);
append(&first,new_data);
printlist(first);
break;
}
break;
case 2:
printf("\nEnter your choice:");
printf("\n1) Delete by data.");
printf("\n2) Delete by position.\n");
scanf("%d",&b);
switch(b)
{
case 1:
printf("Enter data to delete:");
scanf("%d",&data);
deletenode(&first,data);
printlist(first);
break;
case 2:
printf("Enter position of node:");
scanf("%d",&pos);
deletenodebypos(&first,pos);
printlist(first);
break;
}
break;
case 3:
printf("Length of your linked list is: %d\n",getcount(first));
break;
case 4:
printf("\nEnter the choice:");
printf("\n1) Search by key.");
printf("\n2) Search by index.\n");
scanf("%d",&b);
switch(b)
{
case 1:
printf("\nEnter the searching data:");
scanf("%d",&data);
a=searchbykey(first,data);
if(a==1)
printf("\n Data Found at index %d !!",forsearch+1);
else
printf("\n Data not found!!");
break;
case 2:
printf("\nEnter the index:");
scanf("%d",&a);
x=getcount(first);
searchbyindex(&first,a,x);
break;
}
break;
case 5:
printf("\nEnter your choice:");
printf("\n1) From start.");
printf("\n2) From end.\n");
scanf("%d",&a);
switch(a)
{
case 1:
printf("\nEnter the value of n:");
scanf("%d",&n);
fromstart(&first,n);
break;
case 2:
printf("\nEnter the value of n:");
scanf("%d",&n);
x=getcount(first);
printf("\nData at index %d is %d\n",n,fromend(first,n,x));
break;
}
break;
case 6:
printf("\nEnter the data for swapping:-");
printf("\nData 1:");
scanf("%d",&x);
printf("\nData 2:");
scanf("%d",&y);
swapnodes(&first,x,y);
printlist(first);
break;
case 7:
y=getcount(first);
middle(&first,y);
break;
case 8:
deletelist(&first);
printf("\nLinked list deleted!!\n");
printlist(first);
break;
case 9:
reverse(&first);
printf("\nReverse linked list is:\n");
printlist(first);
printf("\n");
break;
}
printf("\nWant to edit more?(1/0)\n");
scanf("%d",&c);
}while(c!=0);
return 0;
}
Code For Circular Linked List:-
In this type of linked list, there is no end. It means the last node of that linked list is connected with the first node of a list.
To See how it works CLICK HERE.
Code For Doubly Linked List:-
This linked list is similar to the Singly Linked List and the small difference in it we can traverse from start or from the end of the list but in Sinlgly we didn't have that right.
To See how it works CLICK HERE.
Comments
Post a Comment