Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to reverse a singly linked list withut modifying the value of next, only the value of the nodes: C++ //Implement the three member

I need to reverse a singly linked list withut modifying the value of next, only the value of the nodes: C++

//Implement the three member functions new_reverse, new_sort and removeAll.

//Linked List Operations #include //cin and cout using namespace std; class Node { public: int value; Node* next; Node() { next = nullptr; }//default constructor Node(int i) { value = i; next = nullptr; }//constructor

};

class LinkedList { public: Node* head; LinkedList() { head = nullptr; } void makeList(int m, int n);//create a linked list of m nodes with //values randomly distrubuted in 0..n-1 void printList(); void new_reverse(); //You are only allow to modify "values" of nodes, but not "next" void new_sort();//Sorting -- Only modify pointers (next or temporary pointers). //Changes of values of nodes are not allowed. //You are not allowed to use external structures such as array or linked list //to perform operations and transfer the values back to the linked list.

void removeAll(int k);//Remove all nodes with value k

}; void LinkedList::makeList(int m, int n) { for (int i = 0; i < m; i++) { Node* p = new Node(rand() % n); p->next = head; head = p; } }

void LinkedList::printList() { Node* p = head; cout << endl; while (p) { cout << p->value << " "; p = p->next; } }

void LinkedList::new_reverse() {//make sure you don't modift the next, just the values of node so that it becomes reversed //You need to implement this function. if (!head || !head->next) return; Node* p1 = head, *p2, *p3; p2 = p1->next; } }

above is the code that my professor gave us to work with

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions