Question

^^^ Q3. I am trying to implement double linked list but I was failed to write...

^^^

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("\nDoubly 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! \n",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("\nThe item %d is Deleted\n",curr->info);

     free(curr);

     return(start);

}

void display(DL *start)

{

   DL *last,*temp = start;

   if( (temp != NULL) )

     {

        printf("\nThe Elements of Doubly Linked List from Left to Right ");

                printf("\n ROOT-> ");

                while(temp != NULL )

                {

                     printf("%d -> ",temp->info);

                     last = temp;

                     temp = temp->rptr;

                }

                printf("NULL");

                printf("\nThe Elements of Doubly Linked List from Left to Right");

                printf("\n ROOT-> ");

                while(last != NULL )

                {

                     printf("%d -> ",last->info);

                     last = last->lptr;

                }

                printf("ROOT\n\n");

     }

   else

     printf("\nEmpty List\n");

}

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------------------------

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Required code :

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("\nEnter 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);

}

Add a comment
Know the answer?
Add Answer to:
^^^ Q3. I am trying to implement double linked list but I was failed to write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • I am stuck on a data structure problem, I am just going off of Geeks for...

    I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • Public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=nul...

    public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; } else{ Node n = list.head; Node temp = list.head; while(n.next!=null){ n=n.next; n.prev=temp; temp=temp.next; } n.next=node; node.prev=n; System.out.println(node.prev + " " + n.data); } } How can i turn this doubly linked list into a circular doubly linkedlist, using java

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT