Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int data;
- struct node *link;
- }N;
- N *f;
- N *r;
- N *getnode()
- {
- N *New;
- New=(N*)malloc(sizeof(N*));
- New->data=0;
- New->link=NULL;
- return New;
- }
- void Insert(int x)
- {
- N *New;
- New=getnode();
- New->data=x;
- if(f==NULL)
- {
- f=New;
- r=f;
- }
- else
- {
- r->link=New;
- r=New;
- }
- }
- void Delete()
- {
- N *x,*y;
- x=f;
- if(f==NULL)
- {
- printf("underfow\n");
- }
- else
- {
- if(f==r)
- {
- f=NULL;
- r=NULL;
- }
- else
- {
- while(1)
- {
- y=x;
- x=x->link;
- if(x==r)
- {
- break;
- }
- }
- r=y;
- r->link=NULL;
- free(x);
- }
- }
- }
- void Display()
- {
- N *x;
- x=f;
- if(f==NULL)
- {
- printf("underfow\n");
- }
- else
- {
- while(x!=NULL)
- {
- printf("%d\n",x->data);
- x=x->link;
- }
- }
- }
- int main()
- {
- int n,ch;
- f=NULL;
- r=NULL;
- while(1)
- {
- printf("1>Insert\n2>Delete\n3>display\n4>exit\nenter the number :");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- printf("enter the data :");
- scanf("%d",&n);
- Insert(n);
- Display();
- break;
- case 2:
- Delete();
- printf("deleted\n");
- Display();
- break;
- case 3:
- Display();
- break;
- case 4:
- exit(0);
- default:
- printf("invaid choice");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement