Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- struct node
- {
- int a;
- struct node *p;
- };
- void Insertion_Last(struct node *head, int value);
- void Display(struct node *head);
- int count(struct node *head);
- void insertion_specific_position(struct node *head,int pp,int vv);
- void Deletion(struct node*head,int pp);
- int main()
- {
- int n,i,c,v,pos,q;
- struct node *list;
- list=(struct node *) malloc(1*sizeof(struct node));
- list->a=0;
- list->p=NULL;
- while(1)
- {
- printf("\n----------Menu-----------\n");
- printf("\nPress 0 for quit\n");
- printf("\nPress 1 for insertion at last position\n");
- printf("\nPress 2 for insertion at specific position\n");
- printf("\nPress 3 for display\n");
- printf("\nPress 4 for deletion at specific position\n");
- printf("\nPress 5 for counting total node\n");
- printf("\nPress 2 for linear search\n");
- printf("\n Enter your choice:\n");
- scanf("%d",&c);
- switch(c)
- {
- case 0: exit(0);
- break;
- case 1: printf("\nchoice = Insertion at last position\n");
- printf("\n Enter new value \n");
- scanf("%d",&v);
- Insertion_Last(list,v);
- break;
- case 3: printf("\nchoice = Display\n");
- if(list->p!=NULL)
- Display(list);
- else
- printf("\nThe list is empty\n");
- break;
- case 5: printf("\n choice=Total node count\n");
- n=count(list);
- printf("\n Total node =%d\n",n);
- break;
- case 2: printf("\n choice=Insertion at specific position\n");
- n=count(list);
- if (n!=0)
- {
- M:printf("\n Enter position between %d to %d\n",1,n+1);
- scanf("%d",&pos);
- if(pos>=1 && pos<=n+1)
- {
- printf("\n Enter Value \n");
- scanf("%d",&v);
- insertion_specific_position(list,pos,v);
- }
- else
- {
- printf("\n Invalid position \n");
- goto M;
- }
- }
- else
- printf("\n The List is Empty\n");
- break;
- case 4:printf("\n choice=Deletion From specific position\n");
- n=count(list);
- if(n!=0)
- {
- k:printf("\n enter position between %d to %d\n",1,n);
- scanf("%d",&pos);
- if(pos>=1&&pos<=n)
- Deletion(list,pos);
- else
- {
- printf("\n wrong position\n");
- goto k;
- }
- }
- else
- printf("\n no data to delete\n\n");
- break;
- default: printf("\n\n Wrong position\n");
- }
- }
- }
- void Insertion_Last(struct node *head, int value)
- {
- struct node *temp;
- while(head->p!=NULL)
- head=head->p;
- temp=(struct node *) malloc(1*sizeof(struct node));
- temp->a=value;
- temp->p=NULL;
- head->p=temp;
- }
- void Display(struct node *head)
- {
- while(head->p!=NULL)
- {
- printf("\t->%d->",head->p->a);
- head=head->p;
- }
- }
- int count(struct node *head)
- {
- int t=0;
- while(head->p!=NULL)
- {
- t=t+1;
- head=head->p;
- }
- return t;
- }
- void insertion_specific_position(struct node *head,int pp,int vv)
- {
- int t=0;
- struct node *temp;
- while(head->p!=NULL)
- {
- if(t==pp-1)
- break;
- head=head->p;
- t=t+1;
- }
- temp=(struct node*)malloc(1*sizeof(struct node));
- temp->a=vv;
- temp->p=head->p;
- head->p=temp;
- }
- void Deletion(struct node*head,int pp)
- {
- struct node *temp;
- int i=0;
- while(head->p!=NULL)
- {
- if(pp-1==i)
- break;
- else
- head=head->p;
- i=i+1;
- }
- temp=head->p;
- head->p=temp->p;
- free(temp);
- }
Advertisement
Add Comment
Please, Sign In to add comment