Question

This is a c programming problem.

Would you please help me to write this problem???

I really appreciate it if you add comments for explanation step by step.

Thank you.

Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked

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

Code:

#include <stdio.h>
#include <stdlib.h>

struct Node
{
   int value;
   struct Node* prev;
   struct Node* next;
};

struct Node* start = NULL;

struct Node* getNewNode(int val)
{
   struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->value = val;
   newNode->prev = NULL;
   newNode->next = NULL;
   return newNode;
}

void insertAtBeginnning(struct Node* newNode)
{
   if (start == NULL)
   {
       start = newNode;
   }
   else
   {
       start->prev = newNode;
       newNode->next = start;
       start = newNode;
   }
}

void deleteLastNode()
{
   struct Node* temp = start;
   struct Node* prevNode = NULL;
   while (temp->next != NULL)
   {
       prevNode = temp;
       temp = temp->next;
   }

   prevNode->next = NULL;

   free(temp);
}

void print()
{
   printf("\n");
   struct Node* temp = start;
   while (temp != NULL)
   {
       if (temp->next == NULL)
           printf("%d", temp->value);
       else
           printf("%d --> ", temp->value);
       temp = temp->next;
   }
}

void reverse(struct Node* aNode)
{
   if (aNode->next == NULL)
   {
       //When it reaches the last node assign the start pointer as the last node
       start = aNode;
       //Swap the prev pointer and the next pointer of the last node
       struct Node* temp = aNode->prev;
       aNode->prev = aNode->next;
       aNode->next = temp;
       return;
   }

   //Call recursively till it reaches the last node
   reverse(aNode->next);

   //Swap the prev pointer and the next pointer of all the nodes during stack unwinding.
   struct Node* temp = aNode->prev;
   aNode->prev = aNode->next;
   aNode->next = temp;
}

int main()
{
   insertAtBeginnning(getNewNode(2));
   insertAtBeginnning(getNewNode(4));
   insertAtBeginnning(getNewNode(8));
   insertAtBeginnning(getNewNode(10));

   printf("\n Before reversing \n");

   print();

   printf("\n After reversing \n");

   reverse(start);

   print();

   return 0;
}

OUTPUT:

Before reversing 2 10 --> 8 --> 4 --> After reversing 2 --> 4 --> 8 --> 10

Add a comment
Know the answer?
Add Answer to:
This is a c programming problem. Would you please help me to write this problem??? I...
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...

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

  • C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You...

    C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...

  • Write a function to implement linked list consisting of five nodes. Store the data in the...

    Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming George, Paul, Ross, Joe, Elaine, Robert1 Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have...

  • C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

    C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };

  • C programming Write an iterative and recursive version of a function to print out a linked...

    C programming Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;

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

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

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