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

Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
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...
HI i will write user's manual for a doubly/singly linked list , How can i write...
HI i will write user's manual for a doubly/singly linked list , How can i write User's manual ?
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT