I need help with my program, it is based off this assignment Design and implemen
ID: 3765010 • Letter: I
Question
I need help with my program, it is based off this assignment
Design and implement your own linked list template class and methods. You will test your new template class using a driver program. The driver program should do the following:
• Create a list of integers,
• Add five integers to the list,
• Display the list,
• Remove a selected integer,
• Display the list,
• Empty the list.
• Create a list of strings,
• Add five strings to the list,
• Display the list,
• Remove a selected string,
• Display the list,
• Empty the list and exit. The list should keep its elements ordered (ascending). Your implementation should not use std:list, but rather build one using pointers. Use the example in the textbook and slides.
Here is my code so far:
main.cpp
#include
#include "LinkedList.h"
#include "Numberlist.h"
using namespace std;
int main()
{
// Define a LinkedList object
LinkedList list;
//Store the NumberList objects in the list
NumberList number1(1);
NumberList number2(2);
NumberList number3(3);
NumberList number4(4);
NumberList number5(5);
//Store NumberList object in list
list.appendNode(number1);
list.appendNode(number2);
list.appendNode(number3);
list.appendNode(number4);
list.appendNode(number5);
//Display the values of the list
cout << "Here are the initial values." << endl;
list.displayList();
cout << endl;
// Delete the 2 integer
cout << "Now deleting the 2 from the list." << endl;
list.deleteNode(number2);
// Display the 4 integers left
cout << "Here are the integers left: " << endl;
list.displayList();
return 0;
Numberlist.h
// Specification file for the NumberList class
using namespace std;
class NumberList
{
private:
int num;
public:
//Constructor
NumberList(int n)
{
num = n;
}
// Mutator
void setnum(int n)
{
num = n;
}
// Accessor functions
int getnum() const
{
return num;
}
};
LinkedList.h
// A class template for holding a linked list.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include // For cout
using namespace std;
template
class LinkedList
{
private:
// Declare a structure for the list.
struct ListNode
{
T value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
int num;
public:
// Constructor
LinkedList()
{ head = nullptr; }
// Destructor
~LinkedList();
// Linked list operations
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList() const;
};
//**************************************************
// appendNode appends a node containing the value *
// passed into newValue, to the end of the list. *
//**************************************************
template
void LinkedList::appendNode(T newValue)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
newNode->next = nullptr;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
{
head = newNode;
}
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node.
nodePtr->next = newNode;
}
}
//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************
template
void LinkedList::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
//**************************************************
// The insertNode function inserts a node with *
// newValue copied to its value member. *
//**************************************************
template
void LinkedList::insertNode(T newValue)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = nullptr; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
{
head = newNode;
newNode->next = nullptr;
}
else // Otherwise, insert newNode.
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to nullptr.
previousNode = nullptr;
// Skip all nodes whose value is less than num.
while (nodePtr != nullptr && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == nullptr)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//******************************************************
// The deleteNode function searches for a node *
// with searchValue as its value. The node, if found, *
// is deleted from the list and from memory. *
//******************************************************
template
void LinkedList::deleteNode(T searchValue)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != nullptr && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
template
LinkedList::~LinkedList()
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr is not at the end of the list...
while (nodePtr != nullptr)
{
// Save a pointer to the next node.
nextNode = nodePtr->next;
// Delete the current node.
delete nodePtr;
// Position nodePtr at the next node.
nodePtr = nextNode;
}
}
#endif
Explanation / Answer
My linked list class is implemented in a seperate class, and needs to instantiate a node when adding new nodes to the end of the list. I have implemented this as follows -
I have set up a driver/test class as follows -
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.