Write down a C Program to insert in beginning & end, delete a node in beginning & end, Traverse for the Singly Linked List and also modify a specified node for the Singly Linked List using Global start variable & call by reference & by using the Switch Case.
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *start=NULL;
- void create_nodebegining(int);
- void create_nodeend(int);
- void delete_nodebegining();
- void delete_nodeend();
- void modify(int,int);
- void traverse();
- int main()
- {
- int data,i,n,ch,data1;
- printf("\n Enter The Value of data to be Entered:\n");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- printf("\n Enter The Value Should be Enter In Data of Node:\n");
- scanf("%d",&data);
- create_nodeend(data);
- }
- printf("\n Please Enter Your Choise \n ");
- printf("\n Press 1 for Insert in Beginning \n Press 2 for Insert in end \n Press 3 Delete in Beginning \n Press 4 for delete in end \n Press 5 for modify \n press 6 for traverse \n");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- printf("\n Enter The Value For Node Data:\n");
- scanf("%d",&data);
- create_nodebegining(data);
- break;
- case 2:
- printf("\n Enter The Value For Node Data:\n");
- scanf("%d",&data);
- create_nodeend(data);
- break;
- case 3:
- delete_nodebegining();
- break;
- case 4:
- delete_nodeend();
- break;
- case 5:
- printf("\n Enter The Data To be Modified:\n");
- scanf("%d",&data1);
- printf("\n Enter The Data Modified:\n");
- scanf("%d",&data);
- modify(data1,data);
- break;
- case 6:
- traverse();
- break;
- }
- traverse();
- }
- void create_nodeend(int x)
- {
- struct node *t,*t1;
- t=(struct node*)malloc(sizeof(struct node));
- t->data=x;
- t->next=NULL;
- if(start==NULL)
- {
- start=t;
- }
- else
- {
- t1=start;
- while(t1->next!=NULL)
- {
- t1=t1->next;
- }
- t1->next=t;
- }
- }
- void create_nodebegining(int x)
- {
- struct node *t,*t1;
- t=(struct node*)malloc(sizeof(struct node));
- t1=start;
- t->data=x;
- t->next=t1;
- start=t;
- traverse();
- }
- void delete_nodeend()
- {
- int d;
- struct node *t,*t1;
- t=start;
- while(t->next!=NULL)
- {
- t1=t;
- t=t->next;
- }
- d=t->data;
- free(t);
- t1->next=NULL;
- traverse();
- }
- void delete_nodebegining()
- {
- int d;
- struct node *t,*t1;
- t=start->next;
- d=start->data;
- free(start);
- start=t;
- traverse();
- }
- void modify(int x,int y)
- {
- struct node *t;
- t=start;
- while(t->data!=x)
- {
- t=t->next;
- }
- t->data=y;
- traverse();
- }
- void traverse()
- {
- struct node *t;
- t=start;
- while(t->next!=NULL)
- {
- printf("\n %d",t->data);
- t=t->next;
- }
- printf("\n %d\n\n",t->data);
- }