Question

In: Computer Science

C++: Write a reverse function that receives a reference to a integer linked list and reverses...

C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first node of linked list and returns the reference of first node, after reversing the linked list. [User Input is not necessary]. Note: Do not use built in library function for linked list. You need to define your own linked list.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include<iostream>
using namespace std;

//a simple Node structure
struct Node{
        int data;
        Node* next;
};

//method to print the contents of a linked list, given first node
void print_list(Node *first){
        //looping through each node and printing data
        for(Node* n=first;n!=NULL;n=n->next){
                cout<<n->data;
                //printing -> if there is a next node
                if(n->next!=NULL){
                        cout<<" -> ";
                }
        }
        cout<<endl;
}

//method to insert a value at the end of a linked list pointed by first,
//and return the head of the list
Node* insert(Node* first, int data){
        //creating new node and setting data and next
        Node* n=new Node();
        n->data=data;
        n->next=NULL;
        //if first is NULL, returning n as new first
        if(first==NULL){
                return n;
        }else{
                //otherwise finding last node, and adding n as next of last
                Node* curr=first;
                while(curr->next!=NULL){
                        curr=curr->next;
                }
                curr->next=n;
                //returning first node
                return first;
        }
}

//required method to reverse a linked list
Node* reverseLinkedList(Node *node){
        //taking a reference to first node
    Node* curr=node;
    //initializing two Node* variables to NULL
    Node* prev=NULL;
    Node* next=NULL;
    //looping until curr is NULL
    while(curr!=NULL){
        //storing next of curr in next
        next=curr->next;
        //setting prev as new next node of prev
        curr->next=prev;
        //assigning curr to prev
        prev=curr;
        //and setting next as new curr
        curr=next;
        }
        //finally, returning prev as new first node
        return prev;
}

//main method for testing
int main(){
        //initializing an empty first node
        Node* first=NULL;
        //adding some numbers
        first=insert(first,1);
        first=insert(first,4);
        first=insert(first,2);
        first=insert(first,3);
        first=insert(first,6);
        first=insert(first,5);
        
        //printing list
        cout<<"List: ";
        print_list(first);
        
        //reversing
        first=reverseLinkedList(first);
        
        //printing list again
        cout<<"Reversed List: ";
        print_list(first);
        
        //before exiting, deleting memory of all nodes to prevent memory leaks.
        while(first!=NULL){
                Node* n=first;
                first=first->next;
                delete n;
        }
        return 0;
}

/*OUTPUT*/

List: 1 -> 4 -> 2 -> 3 -> 6 -> 5
Reversed List: 5 -> 6 -> 3 -> 2 -> 4 -> 1

Related Solutions

Write, specify and prove the function reverse that reverses an array in place. Take care of...
Write, specify and prove the function reverse that reverses an array in place. Take care of the unmodified part of the array at some iteration of the loop. Assume that the swap function is already proved. Note: Prototype is as below. [7 M] [CO2] void swap(int* a, int* b); void reverse(int* array, size_t len){ }
In this problem, you will write a program that reverses a linked list. Your program should...
In this problem, you will write a program that reverses a linked list. Your program should take as input a space-separated list of integers representing the original list, and output a space-separated list of integers representing the reversed list. Your algorithm must have a worst-case run- time in O(n) and a worst-case space complexity of O(1) beyond the input. For example, if our input is: 5 7 1 2 3 then we should print: 3 2 1 7 5 Please...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Write a function void reverse(char * s) that reverses the string passed as an argument. Your...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your code should use pointer arithmetic (it may increment and decrement pointers, but it may not use array indexing). Here is a piece of code that shows the behavior of reverse: char buf[100]; strcpy(buf, “hello”); reverse(buf); printf(“%s\n”, buf); // output should be olleh
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
write a C++ function called inc whose parameter is an integer reference type and that increases...
write a C++ function called inc whose parameter is an integer reference type and that increases the parameter by one when it is called.
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT