WRITE DOWN A C PROGRAM TO INSERT IN BEGINNING & END, DELETE A NODE IN BEGINNING & END, TRAVERSE FOR THE Circular - DOUBLY LINKED LIST (C-DLL) AND ALSO MODIFY A SPECIFIED NODE FOR THE CIRCULAR - DOUBLY 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
- {
- struct node *prev;
- 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->prev=NULL;
- t->data=x;
- t->next=NULL;
- if(start==NULL)
- {
- start=t;
- t->next=start;
- start->prev=t;
- }
- else
- {
- t1=start;
- while(t1->next!=start)
- {
- t1=t1->next;
- }
- t->next=start;
- t->prev=t1;
- t1->next=t;
- start->prev=t;
- }
- }
- void create_nodebegining(int x)
- {
- struct node *t,*t1;
- t1=start;
- t=(struct node*)malloc(sizeof(struct node));
- t->prev=NULL;
- t->data=x;
- t->next=NULL;
- while(t1->next!=start)
- {
- t1=t1->next;
- }
- start->prev=t;
- t->next=start;
- start=t;
- t1->next=start;
- t->prev=t1;
- traverse();
- }
- void delete_nodeend()
- {
- struct node *t,*t1;
- t=start;
- while(t->next!=start)
- {
- t1=t;
- t=t->next;
- }
- free(t);
- t1->next=start;
- traverse();
- }
- void delete_nodebegining()
- {
- struct node *t,*t1;
- t1=start;t=start;
- while(t1->next!=start)
- {
- t1=t1->next;
- }
- free(start);
- start=t->next;
- start->prev=t1;
- t1->next=start;
- 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!=start)
- {
- printf("\n %d",t->data);
- t=t->next;
- }
- printf("\n %d\n\n",t->data);
- }
No comments:
Post a Comment