NOTE: My humble request to all; Please learn from this code, copy it to your tool and execute it, but don’t show this as your assignment or project of your college class.
Linked List with every essential function (.c)
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*head;
void end();
void createalist(int n);
void middle();
void display();
void beginning();
void dlfirst();
void dllast();
void dlmiddle();
int len();
main()
{
int n,data,o;
char i;
printf("Enter number of nodes : \n");
scanf("%d",&n);
createalist(n);
display();
do
{
printf("\n: Nodes Modification center :\n");
printf("\n1.Insert Node in the Beginning.");
printf("\n2.Insert Node in the End.");
printf("\n3.Insert Node in the Middle.");
printf("\n4.Delete Node from the beginning.");
printf("\n5.Delete Node in the End.");
printf("\n6.Delete Node in the Middle.\n");
scanf("%d",&o);
switch(o)
{
case 1:
{
beginning();
display();
break;
}
case 2:
{
end();
display();
break;
}
case 3:
{
middle();
display();
break;
}
case 4:
{
dlfirst();
display();
break;
}
case 5:
{
dllast();
display();
break;
}
case 6:
{
dlmiddle();
display();
break;
}
default:
{
printf("\n invalid input :p");
}
}
printf("\nDo you want to modify more? y or n\n");
scanf("%c",&i);
}
while(i=="y");
getch();
}
void createalist(int n)
{
int i,data;
struct node *newnode,*temp;
printf("ENTER NODE1 : \n");
scanf("%d",&data);
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
printf("Unable to create linkedlist :(");
}
else
{
head->data=data;
head->next=NULL;
temp=head;
}
for(i=2;i<=n;i++)
{
printf("ENTER NODE%d : \n",i);
scanf("%d",&data);
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("ERROR NO.402");
}
else
{
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
}
}
}
void beginning()
{
int data;
struct node *newnode,*temp;
printf("\nENTER DATA IN BEGINNING NODE : ");
scanf("%d",&data);
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("EROAAAAR");
}
else
{
newnode->data=data;
newnode->next=head;
head=newnode;
}
}
void dlfirst()
{
struct node *temp;
temp=head;
head=head->next;
free(temp);
}
int len()
{
int count;
struct node *temp;
for(count=1;temp->next!=NULL;count++)
{
temp=temp->next;
}
return count;
}
void middle()
{
int i,data,position;
struct node *newnode,*temp;
printf("enter position : \n");
scanf("%d",&position);
if(position<2)
{
beginning();
}
else
{
printf("enter data in the middle : \n");
scanf("%d",&data);
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("SORRor");
}
temp=head;
for(i=0;i<position-2;i++)
{
temp=temp->next;
}
newnode->data=data;
newnode->next=temp->next;
temp->next=newnode;
}
}
void dlmiddle()
{
int i,pos;
struct node *temp,*del;
printf("Enter position to delete : ");
scanf("%d",&pos);
temp=head;
for(i=0;i<pos-1;i++)
{
del=temp;
temp=temp->next;
}
del->next=temp->next;
free(temp);
}
void end()
{
int data;
struct node *newnode,*temp;
printf("\nenter data in the end: ");
scanf("%d",&data);
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("Eriorr");
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
}
}
void dllast()
{
struct node *temp,*last;
temp=head;
last=head;
while(temp->next!=NULL)
{
last=temp;
temp=temp->next;
}
last->next=NULL;
free(temp);
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("There is no linked list\n");
}
else
{
printf("LINKED LIST CURRENTLY : ");
temp=head;
while(temp!=NULL)
{
printf("|%d",temp->data);
temp=temp->next;
}
printf("|\n");
}
}
Sample Output:
