#include<stdio.h>
#include<ctype.h>
typedef struct node
{
int info;
struct node *link;
}node;
void display(node *first)
{
node *temp=first;
if(first == NULL)
{
printf("\nLinked List is Empty.");
return;
}
printf("\n");
while(temp->link != first)
{
printf("| %d ",temp->info);
temp=temp->link;
}
printf("| %d ",temp->info);
}
node* insertrear(node *first,int n)
{
node *temp=first;
node* new1 = (node*)malloc(sizeof(node));
new1->info=n;
new1->link=NULL;
if(new1 == NULL)
{
printf("\nNo location available.");
return first;
}
if(first == NULL)
{
first=new1;
new1->link=first;
display(first);
return first;
}
while(temp->link != first)
{
temp=temp->link;
}
temp->link=new1;
new1->link=first;
display(first);
return first;
}
node* insertfirst(node *first,int n)
{
node* temp=first;
node* new1 = (node*)malloc(sizeof(node));
new1->info=n;
if(new1 == NULL)
{
printf("\nNo location available.");
return first;
}
if(first == NULL)
{
first=new1;
new1->link=first;
display(new1);
return first;
}
new1->link=first;
while(temp->link!=first)
temp=temp->link;
temp->link=new1;
display(new1);
first=new1;
return first;
}
node* insertspecnode(node *first)
{
int n=0,opt,specnode=0;
node *temp=first;
node *pred=NULL;
node *new1 = (node*)malloc(sizeof(node));
if(new1 == NULL)
{
printf("\nNo location available.");
return first;
}
if(first==NULL)
{
printf("\nLinked List is empty...");
return first;
}
clrscr();
display(first);
printf("\n1.Insert Before Node\n2.Insert After Node\n3.Exit\n >> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter specific node: ");
scanf("%d",&specnode);
while(temp->info != specnode && temp->link != first)
{
pred=temp;
temp=temp->link;
}
if(temp->info != specnode)
{
printf("\nNode not found...");
getch();
clrscr();
return first;
}
printf("\nEnter Info: ");
scanf("%d",&n);
if(temp == first)
{
first=insertfirst(first,n);
getch();
clrscr();
return first;
}
new1->info=n;
new1->link=pred->link;
pred->link=new1;
display(first);
getch();
clrscr();
return first;
case 2:
printf("\nEnter specific node: ");
scanf("%d",&specnode);
while(temp->info != specnode && temp->link != first)
{
temp=temp->link;
}
if(temp->info != specnode)
{
printf("\nNode not found...");
getch();
clrscr();
return first;
}
printf("\nEnter Info: ");
scanf("%d",&n);
new1->info=n;
new1->link=temp->link;
temp->link=new1;
display(first);
getch();
clrscr();
return first;
case 3: clrscr();
return first;
default:
printf("\nInvalid Input!");
getch();
clrscr();
break;
}
return first;
}
node* deletefirst(node *first)
{
int n=0;
node* temp=first;
node* t;
n=first->info;
if(first==NULL)
{
printf("\nLinked List is empty...");
return NULL;
}
if(first->link==first)
{
free(temp);
printf("\nDeleted Elemtent : %d",n);
return NULL;
}
first=first->link;
t=first;
while(t->link!=temp)
t=t->link;
t->link=first;
printf("\nDeleted Elemtent : %d",n);
free(temp);
return first;
}
node* deletelast(node *first)
{
node *pred=NULL;
node *temp=first;
if(first == NULL)
{
printf("\nLinked List is empty...");
return first;
}
while(temp->link!=first)
{ pred=temp;
temp=temp->link;
}
if(temp==first)
{
printf("\nDeleted Element: %d",temp->info);
free(first);
return NULL;
}
printf("\nDeleted Element: %d",temp->info);
pred->link=first;
free(temp);
return first;
}
node* deletespecnode(node *first)
{
int opt,specnode=0;
node *temp=first;
node *pred=NULL;
node *cpt=NULL;
if(first==NULL)
{
printf("\nLinked List is empty...");
return first;
}
clrscr();
display(first);
printf("\n1.Delete Before Node\n2.Delete After Node\n3.Exit\n >> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter specific node: ");
scanf("%d",&specnode);
while(temp->info != specnode && temp->link != first)
{ cpt=pred;
pred=temp;
temp=temp->link;
}
if(temp->info != specnode)
{
printf("\nNode not found...");
getch();
clrscr();
return first;
}
if(temp == first)
{
printf("\nNo element present before specified node.");
getch();
clrscr();
return first;
}
if(first->link == temp)
{
printf("\nDeleted Element: %d",first->info);
first=first->link;
first->link=first;
display(first);
getch();
clrscr();
return temp;
}
printf("\nDeleted Elemment: %d",pred->info);
cpt->link=temp;
display(first);
getch();
clrscr();
return first;
case 2:
printf("\nEnter specific node: ");
scanf("%d",&specnode);
while(temp->info != specnode && temp->link != first)
{
temp=temp->link;
}
if(temp->info != specnode)
{
printf("\nNode not found...");
getch();
clrscr();
return first;
}
if(temp->link==first)
{
printf("\nNo element after specified node. ");
getch();
clrscr();
return first;
}
printf("\nDeleted Element: %d",temp->link->info);
temp->link=temp->link->link;
getch();
clrscr();
return first;
case 3: clrscr();
return first;
default:
printf("\nInvalid Input!");
getch();
clrscr();
break;
}
return first;
}
node* duplicate_list(node *first,node *copy)
{
node *temp=first;
copy=NULL;
if(first == NULL)
{
printf("\nLinked List is empty...");
return NULL;
}
while(temp->link != first)
{
copy=insertrear(copy,temp->info);
temp=temp->link;
}
copy=insertrear(copy,temp->info);
printf("\nOriginal Linked List : \t");
display(first);
printf("\nCopied Linked List : \t");
display(copy);
return copy;
}
void main()
{
node *first=NULL;
node *copy=NULL;
int choice,n;
clrscr();
start:
printf("\n\n1.InsertFront\n2.InsertRear\n3.Insert after specified node\n4.Delete at First\n5.Delete at Last\n6.Delete specified node\n7.Display\n8.Copy Linked List\n9.Exit\n >> ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter Info : ");scanf("%d",&n);
first=insertfirst(first,n);
goto start;
case 2: printf("\nEnter Info : ");scanf("%d",&n);
first=insertrear(first,n);
goto start;
case 3:first=insertspecnode(first);
goto start;
case 4:first=deletefirst(first);
goto start;
case 5:first=deletelast(first);
goto start;
case 6:first=deletespecnode(first);
goto start;
case 7:display(first);
goto start;
case 8:copy=duplicate_list(first,copy);
goto start;
case 9:goto exit;
}
exit:
}
#include<ctype.h>
typedef struct node
{
int info;
struct node *link;
}node;
void display(node *first)
{
node *temp=first;
if(first == NULL)
{
printf("\nLinked List is Empty.");
return;
}
printf("\n");
while(temp->link != first)
{
printf("| %d ",temp->info);
temp=temp->link;
}
printf("| %d ",temp->info);
}
node* insertrear(node *first,int n)
{
node *temp=first;
node* new1 = (node*)malloc(sizeof(node));
new1->info=n;
new1->link=NULL;
if(new1 == NULL)
{
printf("\nNo location available.");
return first;
}
if(first == NULL)
{
first=new1;
new1->link=first;
display(first);
return first;
}
while(temp->link != first)
{
temp=temp->link;
}
temp->link=new1;
new1->link=first;
display(first);
return first;
}
node* insertfirst(node *first,int n)
{
node* temp=first;
node* new1 = (node*)malloc(sizeof(node));
new1->info=n;
if(new1 == NULL)
{
printf("\nNo location available.");
return first;
}
if(first == NULL)
{
first=new1;
new1->link=first;
display(new1);
return first;
}
new1->link=first;
while(temp->link!=first)
temp=temp->link;
temp->link=new1;
display(new1);
first=new1;
return first;
}
node* insertspecnode(node *first)
{
int n=0,opt,specnode=0;
node *temp=first;
node *pred=NULL;
node *new1 = (node*)malloc(sizeof(node));
if(new1 == NULL)
{
printf("\nNo location available.");
return first;
}
if(first==NULL)
{
printf("\nLinked List is empty...");
return first;
}
clrscr();
display(first);
printf("\n1.Insert Before Node\n2.Insert After Node\n3.Exit\n >> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter specific node: ");
scanf("%d",&specnode);
while(temp->info != specnode && temp->link != first)
{
pred=temp;
temp=temp->link;
}
if(temp->info != specnode)
{
printf("\nNode not found...");
getch();
clrscr();
return first;
}
printf("\nEnter Info: ");
scanf("%d",&n);
if(temp == first)
{
first=insertfirst(first,n);
getch();
clrscr();
return first;
}
new1->info=n;
new1->link=pred->link;
pred->link=new1;
display(first);
getch();
clrscr();
return first;
case 2:
printf("\nEnter specific node: ");
scanf("%d",&specnode);
while(temp->info != specnode && temp->link != first)
{
temp=temp->link;
}
if(temp->info != specnode)
{
printf("\nNode not found...");
getch();
clrscr();
return first;
}
printf("\nEnter Info: ");
scanf("%d",&n);
new1->info=n;
new1->link=temp->link;
temp->link=new1;
display(first);
getch();
clrscr();
return first;
case 3: clrscr();
return first;
default:
printf("\nInvalid Input!");
getch();
clrscr();
break;
}
return first;
}
node* deletefirst(node *first)
{
int n=0;
node* temp=first;
node* t;
n=first->info;
if(first==NULL)
{
printf("\nLinked List is empty...");
return NULL;
}
if(first->link==first)
{
free(temp);
printf("\nDeleted Elemtent : %d",n);
return NULL;
}
first=first->link;
t=first;
while(t->link!=temp)
t=t->link;
t->link=first;
printf("\nDeleted Elemtent : %d",n);
free(temp);
return first;
}
node* deletelast(node *first)
{
node *pred=NULL;
node *temp=first;
if(first == NULL)
{
printf("\nLinked List is empty...");
return first;
}
while(temp->link!=first)
{ pred=temp;
temp=temp->link;
}
if(temp==first)
{
printf("\nDeleted Element: %d",temp->info);
free(first);
return NULL;
}
printf("\nDeleted Element: %d",temp->info);
pred->link=first;
free(temp);
return first;
}
node* deletespecnode(node *first)
{
int opt,specnode=0;
node *temp=first;
node *pred=NULL;
node *cpt=NULL;
if(first==NULL)
{
printf("\nLinked List is empty...");
return first;
}
clrscr();
display(first);
printf("\n1.Delete Before Node\n2.Delete After Node\n3.Exit\n >> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter specific node: ");
scanf("%d",&specnode);
while(temp->info != specnode && temp->link != first)
{ cpt=pred;
pred=temp;
temp=temp->link;
}
if(temp->info != specnode)
{
printf("\nNode not found...");
getch();
clrscr();
return first;
}
if(temp == first)
{
printf("\nNo element present before specified node.");
getch();
clrscr();
return first;
}
if(first->link == temp)
{
printf("\nDeleted Element: %d",first->info);
first=first->link;
first->link=first;
display(first);
getch();
clrscr();
return temp;
}
printf("\nDeleted Elemment: %d",pred->info);
cpt->link=temp;
display(first);
getch();
clrscr();
return first;
case 2:
printf("\nEnter specific node: ");
scanf("%d",&specnode);
while(temp->info != specnode && temp->link != first)
{
temp=temp->link;
}
if(temp->info != specnode)
{
printf("\nNode not found...");
getch();
clrscr();
return first;
}
if(temp->link==first)
{
printf("\nNo element after specified node. ");
getch();
clrscr();
return first;
}
printf("\nDeleted Element: %d",temp->link->info);
temp->link=temp->link->link;
getch();
clrscr();
return first;
case 3: clrscr();
return first;
default:
printf("\nInvalid Input!");
getch();
clrscr();
break;
}
return first;
}
node* duplicate_list(node *first,node *copy)
{
node *temp=first;
copy=NULL;
if(first == NULL)
{
printf("\nLinked List is empty...");
return NULL;
}
while(temp->link != first)
{
copy=insertrear(copy,temp->info);
temp=temp->link;
}
copy=insertrear(copy,temp->info);
printf("\nOriginal Linked List : \t");
display(first);
printf("\nCopied Linked List : \t");
display(copy);
return copy;
}
void main()
{
node *first=NULL;
node *copy=NULL;
int choice,n;
clrscr();
start:
printf("\n\n1.InsertFront\n2.InsertRear\n3.Insert after specified node\n4.Delete at First\n5.Delete at Last\n6.Delete specified node\n7.Display\n8.Copy Linked List\n9.Exit\n >> ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter Info : ");scanf("%d",&n);
first=insertfirst(first,n);
goto start;
case 2: printf("\nEnter Info : ");scanf("%d",&n);
first=insertrear(first,n);
goto start;
case 3:first=insertspecnode(first);
goto start;
case 4:first=deletefirst(first);
goto start;
case 5:first=deletelast(first);
goto start;
case 6:first=deletespecnode(first);
goto start;
case 7:display(first);
goto start;
case 8:copy=duplicate_list(first,copy);
goto start;
case 9:goto exit;
}
exit:
}
No comments:
Post a Comment