Question

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 list is: a --> NULL

The list in reverse is: a --> NULL

Also, some of the code. I removed everything past the insert function, I need help with this to get unstuck.

#include <stdio.h>

#include <stdlib.h>

/* self-referential structure */

struct listNode {            

   char data; /* each listNode contains a character */

   struct listNode *nextPtr; /* pointer to next node*/

}; /* end structure listNode */

typedef struct listNode ListNode; /* synonym for struct list Node */

typedef ListNode *ListNodePtr; /* synonym for ListNode* */

/* prototypes */

void insert( ListNodePtr *sPtr, char value );

char delete( ListNodePtr *sPtr, char value );

int isEmpty( ListNodePtr sPtr );

void printList( ListNodePtr currentPtr );

void instructions( void );

int main( void )

{

   ListNodePtr startPtr = NULL; /* initially there are no nodes */

   int choice; /* user's choice */

   char item; /* char entered by user */

   instructions(); /* display the menu */

   printf( "? " );

   scanf( "%d", &choice );

   /* loop while user does not choose 3 */

   while ( choice != 3 ) {

      switch ( choice ) {

         case 1:

            printf( "Enter a character: " );

            scanf( "\n%c", &item );

            insert( &startPtr, item ); /* insert item in list */

            printList( startPtr );

            break;

         case 2:

            /* if list is not empty */

            if ( !isEmpty( startPtr ) ) {

               printf( "Enter character to be deleted: " );

               scanf( "\n%c", &item );

               /* if character is found, remove it */

               if ( delete( &startPtr, item ) ) { /* remove item */

                  printf( "%c deleted.\n", item );

                  printList( startPtr );

               } /* end if */

               else {

                  printf( "%c not found.\n\n", item );

               } /* end else */

            } /* end if */

            else {

               printf( "List is empty.\n\n" );

            } /* end else */

            break;

        default:

            printf( "Invalid choice.\n\n" );

            instructions();

            break;

      } /* end switch */

      printf( "? " );

      scanf( "%d", &choice );

   } /* end while */

   printf( "End of run.\n" );

   return 0; /* indicates successful termination */

} /* end main */

/* display program instructions to user */

void instructions( void )

{

   printf( "Enter your choice:\n"

      "   1 to insert an element into the list.\n"

      "   2 to delete an element from the list.\n"

      "   3 to end.\n" );

} /* end function instructions */

/* Insert a new value into the list in sorted order */

void insert( ListNodePtr *sPtr, char value )

{

   ListNodePtr newPtr; /* pointer to new node */

   ListNodePtr previousPtr; /* pointer to previous node in list */

   ListNodePtr currentPtr; /* pointer to current node in list */

   newPtr = malloc( sizeof( ListNode ) ); /* create node */

   if ( newPtr != NULL ) { /* is space available */

      newPtr->data = value; /* place value in node */

    newPtr->nextPtr = NULL; /* node does not link to another node */

     

      previousPtr = NULL;

      currentPtr = *sPtr;

      /* loop to find the correct location in the list */

      while ( currentPtr != NULL && value > currentPtr->data ) {

         previousPtr = currentPtr; /* walk to ...   */

         currentPtr = currentPtr->nextPtr; /* ... next node */

      } /* end while */

      /* insert new node at beginning of list */

      if ( previousPtr == NULL ) {

         newPtr->nextPtr = *sPtr;

         *sPtr = newPtr;

      } /* end if */

      else { /* insert new node between previousPtr and currentPtr */

         previousPtr->nextPtr = newPtr;

         newPtr->nextPtr = currentPtr;

      } /* end else */

   } /* end if */

   else {

      printf( "%c not inserted. No memory available.\n", value );

   } /* end else */

} /* end function insert */

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

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

struct dlink /*structure for double linked list   */

{

char data;

struct dlink *prev;

struct dlink *next;

};

typedef struct dlink node;

void instruction();

node * insert(node *, char);

void display(node *);

void reverse_display(node *);

node * search(node *, char, int *);

void main()

{

node *head;      //structure pointer variable for holding begining address of first node of list;

head=NULL;

char ch;       //variable for storing data

int choice;

   while(1)

   {

    instruction();

    printf("\n Enter your choice according to given instruction");

    scanf("%d",&choice);

    if(choice==4)

    break;

    switch(choice)

    {

    case 1:

      {

      printf("\n Enter data to be insert");

      fflush(stdin);                  //for refreshing input buffer

      scanf("%c",&ch);

      head=insert(head, ch);

      break;

      }

    case 2:

      {

         printf("\n list in sorted order");

         display(head);

         break;

         }

   case 3:

      {

         printf("\n List in reverse order");

         reverse_display(head);

         break;

         }

    default:

         printf("\n Wrong entry, enter right option");

      }

    }

}

void instruction()

{

printf("\n Enter your choice");

printf("\n 1:Insert data into list");

printf("\n 2: Display data");

printf("\n 3: Reverse Display of data");

printf("\n 4: Exit from program");

}

node* insert(node *head, char ch)

{

   node *ptr, *current=head;

   int flag=1 ;

   ptr=(node * ) malloc(sizeof(node));

   ptr->data=ch;

   ptr->next=ptr->prev=NULL;

   if(head==NULL)

   {

    head=ptr;

    return(head);

   }

   else

     current=search(head,ch, &flag);

     if(current->next==NULL && current->data<=ch) /*Insert before*/

     {

     current->next=ptr;

     ptr->prev=current;

   }

   else if(ch<current->data)               /*Insert after*/

    {

     ptr->next=current;

     current->prev=ptr;

     head=ptr;

     }

     else

    {

     ptr->next=current->next;

     ptr->prev=current;

     current->next=ptr;

     ptr->next->prev=ptr;

    }

    return(head);

   }

node* search(node *head, char ch, int *flag)

{

   node *ptr=head;

   while(head!=NULL )

   {

    if(head->data<=ch)

    {

    ptr=head;

    head=head->next;

    }

    else

     break;

    }

    return(ptr);

}

void display(node *head)

{

if(head==NULL)

printf("\n list is empty");

else

     while(head!=NULL)

    {

      printf("%c->" ,head->data);

     head=head->next;

     }

     printf("NULL");

   }

void reverse_display(node *head)

{

if(head==NULL)

    printf("\n list is empty");

else

    while(head->next!=NULL)

    head=head->next;

    while(head!=NULL)

    {

      printf("%c\t" ,head->data);

      head=head->prev;

     }

   }

our program is similar to you but i have written one more function search( ) to find actual position and return to insert function of next input alphabet according to ascending order in the list.

Add a comment
Know the answer?
Add Answer to:
Programming in C: I am trying to modify this linked list to be doubly linked list....
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
  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

  • ^^^ 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...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

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

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

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

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

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