Question

Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

Please fill in this code to reverse a linked list: (written in C/C++)

#include  
#include  
#include 

using namespace std;

/* Link list node */
struct Node { 
        int data; 
        // your code here
}; 

/* Function to reverse the linked list */
static void reverse(struct Node** head_ref) 
{ 
        // your code here 
} 

/* Function to push a node */
void push(struct Node** head_ref, int new_data) 
{ 
        // your code here 
} 

/* Function to print linked list */
void printList(struct Node* head) 
{ 
        struct Node* temp = head; 
        while (temp != NULL) { 
                cout << temp->data << " "; 
                temp = temp->next; 
        } 
} 

/* Driver program to test above function*/
int main() 
{ 
        /* Start with the empty list */
        struct Node* head = NULL; 

        push(&head, 10); 
        push(&head, 20); 
        push(&head, 15); 
        push(&head, 85); 

        cout << "Forward list\n"; 
        printList(head); 
        reverse(&head); 
        cout << "\nReversed list \n"; 
        printList(head); 
} 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

IDEONE Link: https://ideone.com/xyoCMR

Raw Code:

Used stack to reverse the linked list.

#include <iostream>
using namespace std;

#include<bits/stdc++.h>

using namespace std;

/* Link list node */
struct Node {
int data;
       Node *next;
// your code here
};

/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
// your code here
struct Node *head = *head_ref;
stack<int> s;
struct Node*temp = head;
while(temp!=NULL){
s.push(temp->data);
temp = temp->next;
}
temp = head;
while(temp!=NULL){
temp->data = s.top();
s.pop();
temp = temp->next;
}
//return head;
*head_ref = head;
}

/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
// your code here
Node *t = new Node();
t->data = new_data;
t->next = *head_ref;
*head_ref = t;
}

/* Function to print linked list */
void printList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}

/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;

push(&head, 10);
push(&head, 20);
push(&head, 15);
push(&head, 85);

cout << "Forward list\n";
printList(head);
reverse(&head);
cout << "\nReversed list \n";
printList(head);
}

Add a comment
Know the answer?
Add Answer to:
Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...
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
  • Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h>...

    Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • In C++, change this code to a circular linked list #include <iostream> using namespace std; struct...

    In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • 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++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

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

  • Im making a generic linked list. I cant figure out how to make an object of...

    Im making a generic linked list. I cant figure out how to make an object of my class from main. My 3 files are main.cpp dan.h dan.cpp The error is: 15 6 [Error] prototype for 'void ll<T>::insert()' does not match any in class 'll<T>' #include <iostream> #include <string> #include "dan.h" using namespace std; int main() { ll<int> work; work.insert(55);//THIS IS THE LINE THATS GIVING ME THE ERROR, Without this line it compiles and //runs. } #ifndef DAN_H #define DAN_H #include...

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