#include<stdio.h>
#include<ctype.h>
typedef struct node
{
int info;
struct node *left;
struct node *right;
}node;
void display(node *first)
{
node *temp=first;
if(first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
while(temp != NULL)
{
printf(" %d |",temp->info);
temp=temp->right;
}
printf("\n");
return;
}
void insertatfirst(node **first,node **last,int n)
{
node *new1 = (node*)malloc(sizeof(node));
if(new1 == NULL)
{
printf("\nNo location available.");
return;
}
new1->info=n;
new1->left=NULL;
if(*first == NULL)
{
new1->right=NULL;
*first=*last=new1;
return;
}
new1->right=*first;
(*first)->left=new1;
*first=new1;
return;
}
void insertatlast(node **first,node **last,int n)
{
node *new1=(node*)malloc(sizeof(node));
if(new1 == NULL)
{
printf("\nNo location available.");
return;
}
new1->info=n;
new1->right=NULL;
if(*first == NULL)
{
new1->left=NULL;
*first=*last=new1;
return;
}
new1->left=*last;
(*last)->right=new1;
*last=new1;
return;
}
void insertafterspecnode(node **first,node **last)
{
int n=0;
int inf=0;
node *temp=*first;
node *new1=(node*)malloc(sizeof(node));
if(new1 == NULL)
{
printf("\nNo location available.");
return;
}
printf("\nEnter the INFO of specific node :");
scanf("%d",&n);
while(temp->info != n && temp->right != NULL)
temp=temp->right;
if(temp->right == NULL && temp->info != n)
{
printf("\nSpecified node does not exist.\n");
return;
}
printf("\nEnter Info : ");
scanf("%d",&inf);
new1->info=inf;
if(temp == *last)
{
new1->right=NULL;
(*last)->right=new1;
new1->left=*last;
*last=new1;
display(*first);
return;
}
new1->right=temp->right;
temp->right->left=new1;
temp->right=new1;
new1->left=temp;
display(*first);
}
void deleteatfirst(node **first,node **last)
{
node *temp = NULL;
if(*first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
if(*first == *last)
{
printf("\nDeleted Node : %d\n",(*first)->info);
temp=*first;
*first=*last=NULL;
free(temp);
return;
}
temp=*first;
printf("\nDeleted Node : %d\n",temp->info);
(*first)->right->left=NULL;
*first = (*first)->right;
free(temp);
return;
}
void deleteatlast(node **first,node **last)
{
node *temp = NULL;
if(*first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
if(*first == *last)
{
printf("\nDeleted Node : %d\n",(*first)->info);
temp=*first;
*first=*last=NULL;
free(temp);
return;
}
temp=*last;
printf("\nDeleted Node : %d\n",temp->info);
(*last)->left->right = NULL;
*last=(*last)->left;
free(temp);
}
void deletespecnode(node **first,node **last)
{
node *temp=*first;
int n=0;
if(*first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
printf("\nEnter INFO of specific node : ");
scanf("%d",&n);
while(temp->info != n && temp->right != NULL)
temp=temp->right;
if(temp->right == NULL && temp->info != n)
{
printf("\nSpecified node does not exist.\n");
return;
}
if(temp == *first)
{
deleteatfirst(&*first,&*last);
return;
}
if(temp == *last)
{
deleteatlast(&*first,&*last);
return;
}
printf("\nDeleted Node : %d\n",temp->info);
temp->left->right = temp->right;
temp->right->left = temp->left;
free(temp);
return;
}
void copylinkedlist(node **first,node **cfirst,node **clast)
{
node *temp=*first;
node *ctemp=*cfirst;
*cfirst = NULL;
*clast = NULL;
if(*first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
while(temp != NULL)
{
insertatlast(&*cfirst,&*clast,temp->info);
temp=temp->right;
}
printf("\nOriginal Linked List : \n");
display(*first);
printf("\nCopied Linked List : \n");
display(*cfirst);
return;
}
void main()
{
node *first = NULL, *last = NULL, *cfirst = NULL, *clast = NULL;
int opt,w_t=1,n=0;
while(w_t)
{
printf("\n1. InsertAtFirst\n2. InsertAtLast\n3. Insert After Specific Node\n4. Delete At First\n5. Delete At Last\n6. Delete specified node\n7. Traverse (Display)\n8. Copy the given linked list\n9. Exit\n>> ");
scanf("%d",&opt);
switch(opt)
{
case 1: printf("\nEnter Info : ");
scanf("%d",&n);
insertatfirst(&first,&last,n);
display(first);
break;
case 2: printf("\nEnter Info : ");
scanf("%d",&n);
insertatlast(&first,&last,n);
display(first);
break;
case 3:insertafterspecnode(&first,&last);
break;
case 4:deleteatfirst(&first,&last);
break;
case 5:deleteatlast(&first,&last);
break;
case 6:deletespecnode(&first,&last);
break;
case 7:display(first);
break;
case 8:copylinkedlist(&first,&cfirst,&clast);
break;
case 9:w_t=0;
break;
}
}
}
#include<ctype.h>
typedef struct node
{
int info;
struct node *left;
struct node *right;
}node;
void display(node *first)
{
node *temp=first;
if(first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
while(temp != NULL)
{
printf(" %d |",temp->info);
temp=temp->right;
}
printf("\n");
return;
}
void insertatfirst(node **first,node **last,int n)
{
node *new1 = (node*)malloc(sizeof(node));
if(new1 == NULL)
{
printf("\nNo location available.");
return;
}
new1->info=n;
new1->left=NULL;
if(*first == NULL)
{
new1->right=NULL;
*first=*last=new1;
return;
}
new1->right=*first;
(*first)->left=new1;
*first=new1;
return;
}
void insertatlast(node **first,node **last,int n)
{
node *new1=(node*)malloc(sizeof(node));
if(new1 == NULL)
{
printf("\nNo location available.");
return;
}
new1->info=n;
new1->right=NULL;
if(*first == NULL)
{
new1->left=NULL;
*first=*last=new1;
return;
}
new1->left=*last;
(*last)->right=new1;
*last=new1;
return;
}
void insertafterspecnode(node **first,node **last)
{
int n=0;
int inf=0;
node *temp=*first;
node *new1=(node*)malloc(sizeof(node));
if(new1 == NULL)
{
printf("\nNo location available.");
return;
}
printf("\nEnter the INFO of specific node :");
scanf("%d",&n);
while(temp->info != n && temp->right != NULL)
temp=temp->right;
if(temp->right == NULL && temp->info != n)
{
printf("\nSpecified node does not exist.\n");
return;
}
printf("\nEnter Info : ");
scanf("%d",&inf);
new1->info=inf;
if(temp == *last)
{
new1->right=NULL;
(*last)->right=new1;
new1->left=*last;
*last=new1;
display(*first);
return;
}
new1->right=temp->right;
temp->right->left=new1;
temp->right=new1;
new1->left=temp;
display(*first);
}
void deleteatfirst(node **first,node **last)
{
node *temp = NULL;
if(*first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
if(*first == *last)
{
printf("\nDeleted Node : %d\n",(*first)->info);
temp=*first;
*first=*last=NULL;
free(temp);
return;
}
temp=*first;
printf("\nDeleted Node : %d\n",temp->info);
(*first)->right->left=NULL;
*first = (*first)->right;
free(temp);
return;
}
void deleteatlast(node **first,node **last)
{
node *temp = NULL;
if(*first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
if(*first == *last)
{
printf("\nDeleted Node : %d\n",(*first)->info);
temp=*first;
*first=*last=NULL;
free(temp);
return;
}
temp=*last;
printf("\nDeleted Node : %d\n",temp->info);
(*last)->left->right = NULL;
*last=(*last)->left;
free(temp);
}
void deletespecnode(node **first,node **last)
{
node *temp=*first;
int n=0;
if(*first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
printf("\nEnter INFO of specific node : ");
scanf("%d",&n);
while(temp->info != n && temp->right != NULL)
temp=temp->right;
if(temp->right == NULL && temp->info != n)
{
printf("\nSpecified node does not exist.\n");
return;
}
if(temp == *first)
{
deleteatfirst(&*first,&*last);
return;
}
if(temp == *last)
{
deleteatlast(&*first,&*last);
return;
}
printf("\nDeleted Node : %d\n",temp->info);
temp->left->right = temp->right;
temp->right->left = temp->left;
free(temp);
return;
}
void copylinkedlist(node **first,node **cfirst,node **clast)
{
node *temp=*first;
node *ctemp=*cfirst;
*cfirst = NULL;
*clast = NULL;
if(*first == NULL)
{
printf("\nLinked List is empty.\n");
return;
}
while(temp != NULL)
{
insertatlast(&*cfirst,&*clast,temp->info);
temp=temp->right;
}
printf("\nOriginal Linked List : \n");
display(*first);
printf("\nCopied Linked List : \n");
display(*cfirst);
return;
}
void main()
{
node *first = NULL, *last = NULL, *cfirst = NULL, *clast = NULL;
int opt,w_t=1,n=0;
while(w_t)
{
printf("\n1. InsertAtFirst\n2. InsertAtLast\n3. Insert After Specific Node\n4. Delete At First\n5. Delete At Last\n6. Delete specified node\n7. Traverse (Display)\n8. Copy the given linked list\n9. Exit\n>> ");
scanf("%d",&opt);
switch(opt)
{
case 1: printf("\nEnter Info : ");
scanf("%d",&n);
insertatfirst(&first,&last,n);
display(first);
break;
case 2: printf("\nEnter Info : ");
scanf("%d",&n);
insertatlast(&first,&last,n);
display(first);
break;
case 3:insertafterspecnode(&first,&last);
break;
case 4:deleteatfirst(&first,&last);
break;
case 5:deleteatlast(&first,&last);
break;
case 6:deletespecnode(&first,&last);
break;
case 7:display(first);
break;
case 8:copylinkedlist(&first,&cfirst,&clast);
break;
case 9:w_t=0;
break;
}
}
}
No comments:
Post a Comment