^^^ Q3. I am trying to implement double linked list but I was failed to write th
ID: 3933295 • Letter: #
Question
^^^
Q3. I am trying to implement double linked list but I was failed to write the code so anyone gives the main
Code in the main function
THANK YOU FOR ADVANCE
#include<stdio.h>
#include<stdlib.h>
#include<alloc.h>
struct node
{
int info;
struct node *lptr,*rptr;
};
typedef struct node DL;
DL *delete( ) , *insert ( );
void display();
DL *delete(DL *start,int x)
{
DL *left,*right,*curr;
curr = start;
if( start == NULL)
{
printf(" Doubly Linked List is Empty");
return(start);
}
while( (curr != NULL) && (curr->info != x) )
curr = curr->rptr;
if(curr == NULL)
{
printf("%d does not Exist ... Invalid Deletion! ",x);
return(start);
}
left = curr->lptr;
right = curr->rptr;
if(left == NULL)
{
start = right;
right->lptr = NULL;
}
else
{
left->rptr = right;
if(right != NULL)
right->lptr = left;
}
printf(" The item %d is Deleted ",curr->info);
free(curr);
return(start);
}
void display(DL *start)
{
DL *last,*temp = start;
if( (temp != NULL) )
{
printf(" The Elements of Doubly Linked List from Left to Right ");
printf(" ROOT-> ");
while(temp != NULL )
{
printf("%d -> ",temp->info);
last = temp;
temp = temp->rptr;
}
printf("NULL");
printf(" The Elements of Doubly Linked List from Left to Right");
printf(" ROOT-> ");
while(last != NULL )
{
printf("%d -> ",last->info);
last = last->lptr;
}
printf("ROOT ");
}
else
printf(" Empty List ");
}
DL *insert(DL *start,int x)
{
DL *new,*left,*temp;
new=(DL *) malloc(sizeof(DL));
new->info = x;
new->lptr = NULL;
new->rptr = NULL;
if( start == NULL)
start = new;
else
{
temp = start;
while(temp->rptr != NULL)
temp = temp->rptr;
temp->rptr = new;
new->lptr = temp;
}
return(start);
}
Main()
{
------------------Write the code here------------------------
}
Explanation / Answer
main function for the given program.
main()
{
DL *root = NULL;
int item;
char c;
clrscr();
do
{
printf("Insert/Display/Remove/Quit <> I/D/R/Q ");
printf(" Enter Choice : ");
fflush(stdin);
c = getchar();
switch(c)
{
case 'i':
case 'I': printf("Enter The Element you want to be Inserted : ");
scanf("%d",&item);
root=insert(root,item);
break;
case 'd':
case 'D': display(root);
break;
case 'r':
case 'R':
printf("Enter the Item which you want to Remove :");
scanf("%d",&item);
root = delete(root,item);
break;
case 'Q':
case 'q': return;
}
} while(1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.