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

This is a c++ project that is due in a couple days. I am absolutely awful with p

ID: 3662415 • Letter: T

Question

This is a c++ project that is due in a couple days. I am absolutely awful with pointers so I would appreciate the answer to include detailed comments on how pointers work in this case.

Write a C++ class (or set of classes) that implements a linked list. The type of data contained in the nodes should be double. The maximum number of nodes in the list is 50. Implement at least the following functions: ·
Constructor and destructor; ·
void insert (double number); - inserts an element with the value number at the end of the list; ·
int isEmpty(); - returns 1 if the list is empty, 0 otherwise; ·
int numOfElements(); - returns the number of elements in the list.
You do not need to include any other classes for full credit, but you can if you choose. Remove debug folder and put the entire project including source code in a folder named Task1.

Explanation / Answer

#include <iostream>
using namespace std;

class LinkedList{
// Struct inside the class LinkedList
// This is one node which is not needed by the caller. It is just
// for internal work.
struct Node {
double x;
Node *next;
};

// public member
public:
// constructor
LinkedList(){
head = NULL; // set head to NULL
}

// This prepends a new value at the beginning of the list
void insert(double val){
Node *n = new Node(); // create new Node
n->x = val; // set value
n->next = head; // make the node point to the next node.
// If the list is empty, this is NULL, so the end of the list --> OK
head = n; // last but not least, make the head point at the new node.
}

// returns the first element in the list and deletes the Node.
// caution, no error-checking here!
double delete{
Node *n = head;
double ret = n->x;

head = head->next;
delete n;
return ret;
}

// private member
private:
Node *head; // this is the private member variable. It is just a pointer to the first Node
};

int main() {
LinkedList list;

list.insert(5.5);
list.insert(10.5);
list.insert(20.5);

cout << list.delete() << endl;
cout << list.delete() << endl;
cout << list.delete() << endl;
// because there is no error checking in popValue(), the following
// is undefined behavior. Probably the program will crash, because
// there are no more values in the list.
// cout << list.delete() << endl;
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