Link List Program D
Link List Program D
Link List Program D
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Output:
#include<conio.h>
struct list
int info;
node *first,*now,*temp,*temp1;
void insert(void);
void delet(void);
void disp(void);
main()
int ch;
clrscr();
do
printf("\n\n\t****VIEW MENU****\n");
printf("\t1..INSERT..\n\t2..DELETE..\n\t3..DISPLAY..\n\t4..EXIT\n");
scanf("%d",&ch);
switch (ch)
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
disp();
break;
case 4:
exit();
}while(ch!=5);
getch();
return;
void insert(void)
int s;
clrscr();
do
scanf("%d",&s);
switch(s)
case 1:
insertf();
break;
case 2:
insertl();
break;
case 3:
insertb();
break;
case 4:
break;
}while(s!=4);
return;
}
void delet(void)
int ch1;
clrscr();
do
scanf("%d",&ch1);
switch(ch1)
case 1:
deletf();
break;
case 2:
deletl();
break;
case 3:
deletb();
break;
case 4:
break;
}while(ch1!=4);
return;
deletf()
{ if(first==0)
else
{
temp1=first;
first=first->r;
first->l=NULL;
free(temp1);
return;
deletb()
int no=0,i=0;
if(first==0)
else
scanf("%d",&no);
if(no==1)
deletf();
else
temp=first;
// temp1=temp;
temp=temp->r;
if(no>i+1)
printf("\n\nPOSITION IS WRONG\n");
else
{
if(i==no)
temp->l->r=temp->r;
temp->r->l=temp->l;
free(temp);
else if(temp->r==NULL)
deletl();
return;
deletl()
temp=first;
if(temp==NULL)
getch();
return;
else
while(temp->r!=NULL)
// temp1=temp;
temp=temp->r;
}
temp->l->r=NULL;
free(temp);
return;
insertf()
if(first==0)
first=(node *)malloc(sizeof(node));
scanf("%d",&first->info);
first->l=NULL;
first->r=NULL;
else
now=(node *)malloc(sizeof(node));
scanf("%d",&now->info);
now->l=NULL;
now->r=first;
first->l=now;
first=now;
return;
}
insertl()
if(first==0)
first=(node *)malloc(sizeof(node));
scanf("%d",&first->info);
first->l=NULL;
first->r=NULL;
else
now=(node *)malloc(sizeof(node));
scanf("%d",&now->info);
temp=first;
while(temp->r!=NULL)
temp=temp->r;
now->r=NULL;
temp->r=now;
now->l=temp;
return;
insertb()
int pos=0,i=0;
if(first==0)
else
{
scanf("%d",&pos);
if(pos==1)
insertf();
else
temp=first;
temp=temp->r;
if(pos>i+1)
printf("\n\nPOSITION IS WRONG\n");
return;
else
if(i==pos)
now=(node *)malloc(sizeof(node));
scanf("%d",&now->info);
now->r=temp;
now->l=temp->l;
temp->l->r=now;
temp->l=now;
//temp1->next=now;
else if(temp->r==NULL)
insertl();
return;
void disp(void)
temp=first;
if(temp==0)
else
while(temp!=0)
printf("%d\n",temp->info);
temp=temp->r;
return;
Output:
3.circular link list:-
//Circular LL
#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *first,*now,*temp,*temp1,*last;
void insert(void);
void delet(void);
void disp(void);
main()
{
int ch;
clrscr();
do
{
printf("\n\n\t****VIEW MENU****\n");
printf("\t1..INSERT..\n\t2..DELETE..\n\t3..DISPLAY..\n\t4..EXIT\n");
printf("\nEnter Your Choice:==>");
scanf("%d",&ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
disp();
break;
case 4:
exit();
}
}while(ch!=5);
getch();
return;
}
void insert(void)
{
int s;
clrscr();
do
{
printf("\n1..Insert First\n2..Insert Last\n3..Insert At Specified Position\n4..Back to main\n ");
printf("\nEnter Your Choice:==>");
scanf("%d",&s);
switch(s)
{
case 1:
insertf();
break;
case 2:
insertl();
break;
case 3:
insertb();
break;
case 4:
break;
}
}while(s!=4);
return;
}
void delet(void)
{
int ch1;
clrscr();
do
{
printf("\n1..Delete First\n2..Delete Last\n3..Delete At Specified Position\n4..Back to main\n ");
printf("\nEnter Your Choice:==>");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
deletf();
break;
case 2:
deletl();
break;
case 3:
deletb();
break;
case 4:
break;
}
}while(ch1!=4);
return;
}
insertf()
{
if(first==0)
{
first=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&first->info);
first->next=0;
last=first;
}
else
{
now=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&now->info);
now->next=first;
last->next=now;
first=now;
}
printf("\nNode is Succesfully Inserted\n");
return;
}
insertl()
{
if(first==0)
{
first=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&first->info);
first->next=NULL;
last=first;
}
else
{
now=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&now->info);
temp=first;
while(temp!=last)
temp=temp->next;
last->next=now;
now->next=first;
last=now;
}
printf("\nNode is Succesfully Inserted\n");
return;
}
insertb()
{
int no=0,i=0;
if(first==NULL)
printf("\nThere is no Element in Link List");
else
{
printf("\nEnter Position Of Node:=>");
scanf("%d",&no);
if(no==1)
insertf();
else
{
temp=first;
for(i=1;temp!=last && i<no;i++)
{
temp1=temp;
temp=temp->next;
}
if(no>i+1)
{
printf("\n\nPOSITION IS WRONG\n");
return;
}
else
{
if(i==no)
{
now=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&now->info);
now->next=temp1->next;
temp1->next=now;
}
else if(temp==last)
insertl();
}
}
}
printf("\nNode is Succesfully Inserted\n");
return;
}
void disp(void)
{
/* if(temp==NULL)
{
temp=first;
} */
temp=first;
if(temp==last)
printf("\nThere is no Element in List");
else
{
while(temp!=last)
{
printf("%d\n",temp->info);
temp=temp->next;
}
printf("%d\n",temp->info);
return;
}
}
deletf()
{
if(first==last)
{
// last=last->next;
temp=NULL;
free(last);
free(first);
printf("This is last Element Whish is deleted \n");
printf("\nThere is no element in list");
}
else
{
temp1=first;
first=first->next;
free(temp1);
printf("\nNode is Succesfully Deleted\n");
}
return;
}
deletb()
{
int no=0,i=0;
if(first==NULL)
printf("\nThere is no Element in Link List");
else
{
printf("\nEnter Position Of Node:=>");
scanf("%d",&no);
if(no==1)
deletf();
else
{
temp=first;
for(i=1;temp!=last && i<no;i++)
{
temp1=temp;
temp=temp->next;
}
if(no>i+1)
printf("\n\nPOSITION IS WRONG\n");
else
{
if(i==no)
{
temp1->next=temp->next;
free(temp);
}
else if(temp==last)
deletl();
}
}
}
printf("\nNode is Succesfully Deleted\n");
return;
}
deletl()
{
temp=first;
if(temp==last)
printf("\nThere is No Node In List\n");
else
{
while(temp!=last)
{
temp1=temp;
temp=temp->next;
}
temp1->next=first;
last=temp1;
printf("\nNode is Succesfully Deleted %d ",last->info);
free(temp);
}
return;
}
Output: