Question

Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using...

Arrays, Lists, Stacks and Queues:

1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next

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

Code Screenshot:

Code Output:

Code:

#include<bits/stdc++.h>
using namespace std;
// Structure to store a linked list Node
struct Node
{
int value;
struct Node *_next;
};

void
push_front (struct Node **_head, int value)
{
struct Node *new_node = (struct Node *) malloc (sizeof (struct Node));
new_node->value = value;
new_node->_next = *_head;
*_head = new_node;
}

// reverse linked list iteratively
void reverse_list (struct Node **_head)
{
struct Node *previous = NULL;
struct Node *current = *_head;
// iterate through the list
while (current != NULL)
{
// store next of current in next
struct Node *next = current->_next;
// move the current node onto the previous
current->_next = previous;
previous = current;
// process next node
current = next;
}
// set head pointer
*_head = previous;
}

int main ()
{
int n;
cout << "Enter no of node in linked list";
cin >> n;
int i, value;
struct Node *_head = NULL;
for (i = 0; i < n; i++)
{
cin >> value;
push_front (&_head, value);
  
}
cout << "Linked list Initially:\n";
struct Node *temp = _head;
while (temp)
{
cout<< temp->value<<"->";
temp = temp->_next;
}
cout<<"Null";
reverse_list (&_head);
cout << "\nLinked list after reverse:\n";

temp = _head;
while (temp)
{
cout<< temp->value<<"->";
temp = temp->_next;
}
cout<<"Null";

return 0;
}

//Please Thumbs Up if it helps you:)

Add a comment
Know the answer?
Add Answer to:
Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using...
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
  • Answer all questions 1- in circular singly linked list, previous pointer of the first node points...

    Answer all questions 1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...

  • Test ADTs: Linked Lists, Stacks and Queues (Python and with proper indentation for better understanding please)....

    Test ADTs: Linked Lists, Stacks and Queues (Python and with proper indentation for better understanding please). Create a program that will choose 30 objects with an attribute of random number between 1 and 100. Place each object it in a queue. From that each object will exit the queue in the order that it entered two waiting stacks. Sort those stacks and create two doubly circular linked lists from those stacks. Merge those lists into one doubly circular linked list....

  • Describe in detail an algorithm for reversing a singly linked list L using only a constant...

    Describe in detail an algorithm for reversing a singly linked list L using only a constant amount of additional space and not using any recursion.

  • Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list....

    Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list. It must return a boolean true if the list contains a cycle, or false otherwise. HasCycle has a reference to a Node object, that points to the head of a linked list, as parameter.

  • Write a program void reverse_list(list **l)in pseudo-code or C++ to reverse the direction of a given...

    Write a program void reverse_list(list **l)in pseudo-code or C++ to reverse the direction of a given singly-linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. The node of this singly-linked list is defined as typedef struct list { item_type item; struct list ∗ next ; } list; void reverse_list(list **l){ } Remark: 15 points for completely correct. 10 points for correctly reversing the list with minor errors.

  • write a java program implementing stacks using arrays. (not with linked list)

    write a java program implementing stacks using arrays. (not with linked list)

  • You are given a pointer to a singly linked list. The singly linked list has cycles...

    You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...

  • C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists...

    C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists of any primitive type you want. (Array of size 2020) (This could be based on MSDN libraries or the lab) – you do not need to instantiate any of the linked lists to contain any actual values. Paste your code for that here (this should only be one line) Based on your code or the lab from 4 or your doubly linked list from...

  • Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...

    Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...

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

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