Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node_tag
- {
- int Data;
- struct node_tag *link;
- }node;
- node *GetNode()
- {
- node *New;
- New=(node*)malloc(sizeof(node));
- New->link=NULL;
- return New;
- }
- void Display(node *Last)
- {
- node *x;
- x=Last->link;
- if(x==NULL){
- printf("empty");
- }
- while(x!=Last)
- {
- printf("%d,",x->Data);
- x=x->link;
- }
- printf("%d,",Last->Data);
- }
- node *InsertAtBegin(node *Last,int x)
- {
- node *New;
- New=GetNode();
- New->Data=x;
- if(Last==NULL)
- {
- Last=New;
- Last->link=Last;
- }
- New->link=Last->link;
- Last->link=New;
- return Last;
- }
- node *InsertAtEnd(node *Last,int x)
- {
- node *New;
- New=GetNode();
- New->Data=x;
- if(Last==NULL)
- {
- Last=New;
- Last->link=Last;
- }
- New->link=Last->link;
- Last->link=New;
- Last=New;
- return Last;
- }
- int Search(node *last,int y)
- {
- node *x,*H;
- x=last->link;
- if(x->Data==y)
- {
- return 1;
- }
- else
- {
- H=x;
- x=x->link;
- while(x!=H)
- {
- if(x->Data==y)
- {
- return 1;
- break;
- }
- x=x->link;
- }
- }
- }
- node *DelFront(node *last)
- {
- node *x,*y;
- if(last->link==last)
- {
- last=NULL;
- }
- else{
- x=last->link;
- y=x->link;
- last->link=y;
- x->link=NULL;
- free(x);
- }
- printf("deleted");
- return last;
- }
- node *DelRear(node *last)
- {
- node *y,*x;
- if(last->link==last)
- {
- x=last;
- last=NULL;
- free(x);
- }
- else{
- y=last;
- while(1)
- {
- x=y;
- y=y->link;
- if(y==last)
- {
- break;
- }
- }
- last=x;
- last=DelFront(last);
- }
- return last;
- }
- node *InsertAtAnyPos(node *last,int a,int p1)
- {
- node *y,*Head;
- int i;
- y=last;
- Head=last;
- for(i=0;i<p1;i++)
- {
- Head=Head->link;
- }
- // printf("%d\n",Head->Data);
- last=InsertAtBegin(Head,a);
- last=y;
- return last;
- }
- node *DelAtAnyPos(node *last,int p1)
- {
- node *y,*head;int cnt=0;
- if(last==NULL)
- {
- printf("Deletion not possible\n");
- return last;
- }
- else
- {
- head=last;
- while(1)
- {
- y=head;
- head=head->link;
- cnt++;
- if(cnt==p1)
- {
- break;
- }
- }
- if(head==last)
- {
- last=DelRear(last);
- }
- else if(p1==1)
- {
- last=DelFront(last);
- }
- else
- {
- y->link=head->link;
- free(head);
- }
- return last;
- }
- }
- node *InsertBeforeData(node *last,int data,int value)
- {
- node *y,*head;
- y=last;int c=0;
- head=last;
- while(head->Data!=value)
- {
- head=head->link;
- c++;
- }
- last=InsertAtAnyPos(head,data,c);
- last=y;
- return last;
- }
- node *InsertAfterData(node *last,int data,int value)
- {
- node *y,*head;
- y=last;int c=1;
- head=last;
- while(head->Data!=value)
- {
- head=head->link;
- c++;
- }
- last=InsertAtAnyPos(head,data,c);
- last=y;
- return last;
- }
- node *DelBeforeData(node *last,int x)
- {
- node *y,*head;
- y=last;int c=1;
- head=last->link;
- while(head->Data!=x)
- {
- head=head->link;
- c++;
- }
- printf("%d\n",c);
- if(c==1)
- {
- last=DelRear(last);
- }
- else
- {
- last=DelAtAnyPos(last,(c-1));
- }
- return last;
- }
- node *DelAfterData(node *last,int x)
- {
- node *y,*head;
- y=last;int c=1;
- head=last->link;
- while(head->Data!=x)
- {
- head=head->link;
- c++;
- }
- printf("%d\n",c);
- if(head==last)
- {
- last=DelFront(last);
- }
- else
- {
- last=DelAtAnyPos(last,(c+1));
- }
- return last;
- }
- node *CreateList()
- {
- int ch,x,s,p1;
- node *Last=NULL;
- while(1)
- {
- ax:
- printf("______________________________\n");
- printf("1.display\n2.Search\n3.Insert Begin\n4.Insert End\n5.Delete Front\n6.Delete Rear\n7.Insert at any Position\n8.Delete from any position\n9.Insert before a given data\n10.Insert after a given data\n11. Delete before a given data\n12. Delete after a given data\n13.Exit :");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- printf("______________________________\n");
- if(Last==NULL)
- {
- printf("empty\n");
- goto ax;
- }
- else
- {
- Display(Last);
- printf("\n");
- }
- break;
- case 2:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- else{
- printf("enter the data to be searched :");
- scanf("%d",&x);
- s=Search(Last,x);
- if(s==1)
- printf("found\n");
- else
- printf("not Found\n");
- }
- break;
- case 3:
- printf("enter the data:");
- scanf("%d",&x);
- Last=InsertAtBegin(Last,x);
- break;
- case 4:
- printf("enter the data:");
- scanf("%d",&x);
- Last=InsertAtEnd(Last,x);
- break;
- case 5:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- else
- Last=DelFront(Last);
- break;
- case 6:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- else
- Last=DelRear(Last);
- break;
- case 7:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- printf("enter the data:");
- scanf("%d",&x);
- printf("enter the position:");
- scanf("%d",&p1);
- Last=InsertAtAnyPos(Last,x,p1);
- break;
- case 8:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- printf("enter the position:");
- scanf("%d",&p1);
- Last=DelAtAnyPos(Last,p1);
- break;
- case 9:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- printf("enter the data:");
- scanf("%d",&x);
- printf("enter the data where you want to insert before:");
- scanf("%d",&p1);
- Last=InsertBeforeData(Last,x,p1);
- break;
- case 10:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- printf("enter the data:");
- scanf("%d",&x);
- printf("enter the data where you want to insert before:");
- scanf("%d",&p1);
- Last=InsertAfterData(Last,x,p1);
- break;
- case 11:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- else if(Last->link==Last)
- {
- printf("only one node is present enter some nodes to perform this function\n");
- goto ax;
- }
- printf("enter the data where you want to delete before:");
- scanf("%d",&x);
- Last=DelBeforeData(Last,x);
- break;
- case 12:
- if(Last==NULL)
- {
- printf("list is Empty\n");
- goto ax;
- }
- else if(Last->link==Last)
- {
- printf("only one node is present enter some nodes to perform this function\n");
- goto ax;
- }
- printf("enter the data where you want to delete after:");
- scanf("%d",&x);
- Last=DelAfterData(Last,x);
- break;
- case 13:
- exit(1);
- }
- }
- }
- int main()
- {
- node *Last1;
- Last1=CreateList();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement