Question

in C++ function that reverses a linked list of integers and reverses the order of its...

in C++ function that reverses a linked list of integers and reverses the order of its nodes. The function will have one call-by-reference parameter that is a pointer to the head of the list. After the function is called, this pointer will point to the head of a linked list that has the same nodes as the original list but in the reverse of the order they had in original list. Note that your function will neither create nor destroy any nodes. It will simply rearrange nodes. Place your function is suitable test program

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

Since you have not provided your existing actual code, I am providing the code as per my understanding.

CODE

#include <iostream>

using namespace std;

struct Node {

  int data;

  struct Node* next;

  Node(int data)

  {

    this->data = data;

    next = NULL;

  }

};

struct LinkedList {

  Node* head;

  LinkedList()

  {

    head = NULL;

  }

  Node* reverseLinkedList(Node* head) {

if (head == NULL || head->next == NULL)

return head;

Node* rest = reverseLinkedList(head->next);

head->next->next = head;

head->next = NULL;

return rest;

}

  void display(struct Node *head)

  {

    struct Node* temp = head;

    while (temp != NULL) {

      cout << temp->data << " ";

      temp = temp->next;

    }

  }

  void insert(int data)

  {

    Node* temp = new Node(data);

    temp->next = head;

    head = temp;

  }

};

int main()

{

  LinkedList ll;

  ll.insert(100);

  ll.insert(12);

  ll.insert(33);

  ll.insert(8);

  ll.display(ll.head);

  ll.head = ll.reverseLinkedList(ll.head);

  cout << "\nReversed Linked list is: \n";

  ll.display(ll.head);

  return 0;

}

Add a comment
Know the answer?
Add Answer to:
in C++ function that reverses a linked list of integers and reverses the order of its...
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
  • Language:C++ only numbers 4 and 5 please 2. 1. Use the Node class from the slides...

    Language:C++ only numbers 4 and 5 please 2. 1. Use the Node class from the slides to create a linked list of int's. Create a Node pointer called ptrHead that points to the head of the list. Make the list 5 nodes long. Generate random values to populate the list's data. All nodes should be in the heap. Add a function called addNodeFront that takes a pointer to the head of a list as an input parameter and that adds...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • Please help in C# CHECKS: Function Reverse reverses the position of three integers Values printed in...

    Please help in C# CHECKS: Function Reverse reverses the position of three integers Values printed in order and in reverse order --------------------------------------------------------------------------- Create a program named Reverse3 whose Main() method declares three integers named firstInt, middleInt, and lastInt. Assign the following values to the integers: 23 to firstInt 45 to middleInt 67 to lastInt Then display the values and pass them to a method named Reverse that accepts them as reference variables, places the first value in the lastInt variable,...

  • In C++ Assume entries in a linked list are of type struct listrec: struct listrec {...

    In C++ Assume entries in a linked list are of type struct listrec: struct listrec {             struct listrec    *prev; float                 value;             struct listrec    *next;   }; listrec *head, *tail; Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested: Write a...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • true or false A linked list is a list of items, called nodes, in which the...

    true or false A linked list is a list of items, called nodes, in which the order is determined by the address, called of the nodes 1. 2. The pointer to a linked list-that is, the pointer to the first node in the list-is stored in a separate location called the head or first. 3. A linked list is a dynamic data structure.

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • c++ only Program 2: Linked List Class For this problem, let us take the linked list...

    c++ only Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we’ll expand its functionality and make it a doubly linked list with the ability to traverse in both directions.    Since the list is doubly linked, each node will have the following structure: struct Node { int number; Node * nextNode;...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

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