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!
Part 2- - Create and display a singly Linked List with 5 elements (see below)                 Node*...
Part 2- - Create and display a singly Linked List with 5 elements (see below)                 Node* head                  Node*second                  Node*third                 Node*forth                  Node*fifth 2-Assign the data 5, 6, 8, 10, 12 to each node 3-Display the output GIVEN CODE: #include <studio.h> struct Array { int A[10]; int size; int length; }; void Display(struct Array arr) {      int i; printf("\nElements are\n");      for(i=0;ilengthsize) arr->A[arr->length++]=x; } void Insert(struct Array *arr,int index,int x) {      int i;          if(index>=0 && index <=arr->length) {      for(i=arr->length;i>index;i--)      arr->A[i]=arr->A[i-1]; arr->A[index]=x; arr->length++; }...
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;...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
Using C++, Create a singly Linked List of patients list so that you can sort and...
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List. Each patient's record or node should have the following data: Patient Name: Age: Last 4 digits of Social Security Number: The program should first display a menu that gives the user the option to: 1) Add a Patient's record...
In Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked...
C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked list is palindrome or not. Input: a -> b-> NULL a -> b -> a-> NULL s -> a -> g -> a -> r-> NULL r -> a -> d -> a -> r-> NULL Output: not palindrome palindrome not palindrome palindrome Note:- Code should work on MS-Visual Studio 2017 and provide outputs with code
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT