Question

In: Computer Science

The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....

The file supplied.o contains code that can build, display, and destroy a
linear linked list (singly-linked).

For this lab, you will need to write the following two functions in list.cpp,
and add function prototypes for them to list.h. The provided main.cpp has
calls to each of these functions commented out. As you write the functions,
uncomment them from main.cpp.

void reverse(node * head, node *& newHead)

Recursively make a revserse copy of the source list with head where
newhead is the head of the destination list.

void removeLast(node *& head)

Recursively the last node from this list.

You must use the functions with these exact function prototypes. Use the
supplied makefile for the project to build it. Please don't forget the
supplied.o when generating the executable.

Run your program in valgrind and make sure there are no memory errors or
memory leaks. Assuming the executable file is named app

valgrind --tool=memcheck --leak-check=full ./app

list.cpp

#include <iostream>
#include "list.h"

--------------------------------------------

list.h

#include <iostream>
#include <cstring>
#include <cctype>


struct node
{
int data;
node * next;
};

/* These functions are already written and can be called to test out your code */
void build(node * & head); //supplied
void display(node * head); //supplied
void destroy(node * &head); //supplied

//Write your function prototype here:

------------------------------------------------

main.cpp

#include "list.h"
using namespace std;

int main()
{
node * head = NULL;
node * newHead = NULL;

cout << "----------------------------------------------------------------------" << endl;
cout << "Creating the initial list." << endl;
build(head);
display(head);

cout << "----------------------------------------------------------------------" << endl;
cout << "Reversing the list.";
// reverse(head,newHead);
display(newHead);
destroy(newHead);

cout << "----------------------------------------------------------------------" << endl;
cout << "Removing the last element.";
// removeLast(head);
display(head);
destroy(head);


return 0;
}

Solutions

Expert Solution

main.cpp file

#include "list.h"

using namespace std;

int main()
{
        node * head = NULL;
        node * newHead = NULL;

        cout << "----------------------------------------------------------------------" << endl;
        cout << "Creating the initial list." << endl;
        build(head);
        display(head);

        cout << "----------------------------------------------------------------------" << endl;
        cout << "Reversing the list.";
reverse(head,newHead);
        display(newHead);
        destroy(newHead);

        cout << "----------------------------------------------------------------------" << endl;
        cout << "Removing the last element.";
removeLast(head);
        display(head);
        destroy(head);


        return 0;
}

list.h

#include <iostream>
#include <cstring>
#include <cctype>


struct node
{
        int data;
        node * next;
};

/* These functions are already written and can be called to test out your code */
void build(node * & head); //supplied
void display(node * head); //supplied
void destroy(node * &head); //supplied

//Write your function prototype here:
void reverse(node* &head,node* &newHead);
void removeLast(node* &head);
//------------------------------------------------

list.cpp

#include <iostream>
#include "list.h"


void reverse(node* &head, node* &newHead)
{
    /* last node, fix the head */
    if (head == NULL || head->next == NULL) {
        newHead = head;
        return;
    }
    reverse(head->next, newHead);
    head->next->next = head;
    head->next = NULL;
}


void removeLast(struct node* &head){
    
    if(head==NULL){
        return;
    }
    if(head->next==NULL){
      delete head;
      head=NULL;
      return;
    }
    if(head->next->next==NULL){
      delete head->next;
      head->next=NULL;
      return;
    }
    removeLast(head->next);
}

in reverse function:

  • recursively call for next node till we reach the last node i.e. node==null
  • when last node is reached then start reversing i.e. node->next->next=node

in removeLast function:

  • traverse till last node recursively
  • make cases i.e. node==null or node->next==null or node->next->next ==null
  • remove last node

if code snippet is not well aligned please comment there is some problem with code editor i will paste as plane text then.


Related Solutions

In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT