Question

Question 3: Reversing a singly-linked list of integers Suppose that you have a singly-linked list detined via the following data type (30) Integet itemt net: et stem in 1ist Complete the following function so that it reverses the list pointed to by the arguwent void reverse linkedlist(list itom tx shead)



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


#include <iostream>

using namespace std;

//Struct for node
typedef struct list_item_t
{
int data;
list_item_t *next;
} list_item_t ;

list_item_t *head;


void add(int x)
{
list_item_t *newNode = new list_item_t();
newNode->data = x;
newNode->next = NULL;

if(head==NULL)
{
head = newNode;
return;
}
list_item_t *temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = newNode;
}

void display()
{
list_item_t *temp = head;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}

void reverse_linked_list(list_item_t *node){
//Make this node as prev
list_item_t *prev = node;
//Make next node as current
list_item_t *current = prev->next;
//Set previous node of first node to null
prev->next = NULL;
//Loop through the list
while(current!=NULL){
//Store next node of current node
list_item_t *nextnext = current->next;
//Mark current node's next to previous node
current->next = prev;
//Make current node previous node
prev = current;
//Update the currentNode to next node
current = nextnext;
}
//Finally change the head
head = prev;
}


int main()
{
add(2);
add(3);
add(4);
add(5);
display();
reverse_linked_list(head);
cout<<"After reversing the linked list "<<endl;
display();
// int p = prod(head);
//cout<<"Product is "<<p<<endl;
return 0;
}

OUTPUT :

Add a comment
Know the answer?
Add Answer to:
Question 3: Reversing a singly-linked list of integers Suppose that you have a singly-linked list detined...
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
  • Exercise-1:15 points Develop a node class and a singly list class. The node class should have two...

    Exercise-1:15 points Develop a node class and a singly list class. The node class should have two state variables namely data and nextNode. The singly list class should contain the following methods . Middlelnsert-insert a node somewhere in the middle of the list . Startinsert-insert a node at start of the Linked list Endinsert-insert a node at the end of the Linked list . Delete-delete a node Traverse-prints all the node's data Reverse -reverses the linked list Note: Choose appropriate...

  • Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic...

    Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic singly linked list that can hold any data type. The interface has been specified and provided to you in a header file called mylist.h. So your job is to write mylist.c that implements each function whose prototype is included in mylist.h. Specifically, you are asked to write the following functions: struct Node *addFront(struct List *list, void *data) void traverseList(struct List *list, void (*f)(void *))...

  • I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas....

    I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • Draw a sketch of a singly linked list containing the following int values: 3, 1, 4,...

    Draw a sketch of a singly linked list containing the following int values: 3, 1, 4, 1, 5. The 3 should be at the front of the list. Remember to sketch the head pointer, each node, and each node's next pointer. 2. Write the code for the following new member function for our SinglyLinkedList class. You should write out the definition of the function, but do not need to write out all of the rest of the class. Your code...

  • Pull out question 8 on Exercises. See also: Program 4.16 invert (), Program 4.4 printList () Configuring -main () 1. Create a linked list 2. Function call of 8 (a) and (b) 3. Check the accuracy of th...

    Pull out question 8 on Exercises. See also: Program 4.16 invert (), Program 4.4 printList () Configuring -main () 1. Create a linked list 2. Function call of 8 (a) and (b) 3. Check the accuracy of the output of r list and l list 4. Repeat steps 1-3 above Please use c language void printlist(listPointer first) printf ("The list contains: ) for (; first; first = first→link) printf ("%4d", first-"data); printf("In") Program 4.4: Printing a list the nodes are...

  • 11.) Suppose you have a linked list of Node objects from the Textbook Collections Framework and...

    11.) Suppose you have a linked list of Node objects from the Textbook Collections Framework and currentNode has been initialized to refer to the head node in the list. Which of these statements needs to appear in a loop that goes through the list's items one-by-one? Select one: a. currentNode++ b. currentNode = currentNode.next c. currentNode = previous() d. currentNode += 1 e. currentNode = next() 12.) A singly linked node contains which of these fields? Select one: a. data...

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
ADVERTISEMENT