Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that is composed of one source file named Lab5b.cpp and two othe

ID: 3907498 • Letter: W

Question

Write a program that is composed of one source file named Lab5b.cpp and two other textbook source files. The program uses a linked list to manipulate a sequence of numbers. The program must be written in accordance to the following plan:

Description

You should include a program description at the top of the main program according to the template. You may simply use this sentence, "The program uses a linked list to manipulate a sequence of numbers. "

Create a C++ project for this assignment and add the example source files NumberList.h and NumberList.cpp to the project. In Xcode, this can be accomplished by drag-n-drop the files in Finder to the project in Xcode.

In the main function, define an instance variable of NumberList and insert the following numbers in that order into the instance:

75.2

108.3

  38.45

45.83

173.45

163.52

  106.94

Display the list after inserting those numbers.

Delete two numbers from the list:

45.83

173.45

Then display the list again.

Test The Program

The output should look exactly as follows:

Explanation / Answer

*******************************************

NumberList.h

#include <cstdlib>
#include <iostream>
using namespace std;

class NumberList;

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

class NumberList
{
private:
struct Node* head;

public:
NumberList();
struct Node *newNode(int new_data);
void insert(int value);
void printList();
bool remove(int value);
};

************************************************

NumberList.cpp

#include <cstdlib>

#include <cassert>

#include "NumberList.h"

using namespace std;

NumberList::NumberList () {

head = NULL;

}

struct Node *NumberList::newNode(int new_data)

{

struct Node* new_node =

(struct Node*) malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = NULL;

return new_node;

}

void NumberList::insert(int value) {

struct Node* new_node = newNode(value);

struct Node* current;

  

struct Node** head_ref = &head;

if (*head_ref == NULL || (*head_ref)->data >= new_node->data)

{

new_node->next = *head_ref;

*head_ref = new_node;

}

else

{

current = *head_ref;

while (current->next!=NULL &&

current->next->data < new_node->data)

{

current = current->next;

}

new_node->next = current->next;

current->next = new_node;

}

}

void NumberList::printList() {

struct Node *temp = head;

while(temp != NULL)

{

printf("%d ", temp->data);

temp = temp->next;

}

}

bool NumberList::remove(int value){

struct Node *current = head;

struct Node *previous = NULL;

int i = 0;

if (current == NULL)

return false;

while (current != NULL){

if (current->data == value){

//current = current->next;

//head = head->next;

previous->next = current->next;

delete( current );

i++;

break;

}

else{

previous = current;

current = current->next;

}

}

if (i > 0){

return true;

}

else{

return false;

}

}

*****************************************

Lab5b.cpp

#include<stdio.h>
#include<stdlib.h>
#include "NumberList.h"


int main()
{
NumberList noList;
noList.insert(5);
noList.insert(10);
noList.insert(7);
noList.insert(3);
noList.insert(1);
noList.insert(9);
printf(" Displaying list after inserting numbers ");
noList.printList();

noList.remove(7);
printf(" Displaying list after deleting numbers ");
noList.printList();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote