For this assignment you will be converting the struct version of the linked list
ID: 3689088 • Letter: F
Question
For this assignment you will be converting the struct version of the linked list into a container class for integers. Each node will be hold an integer value and a pointer to the next node. The LinkedList class will contain an integer size, how many things are currently in the list, and a pointer to the first node in the list. Both the ListNode struct and the class definition are provided for you. You are responsible for filling in the implementation of all the LinkedList member functions defined in the class declaration provided.
in c++
Explanation / Answer
#include <iostream>
using namespace std;
// ListNode class
class ListNode {
int data;
ListNode* next;
public:
ListNode() {};
void setInt(int num) { data = num; };
void setNextNode(ListNode* nextNode) { next = nextNode; };
int getData() { return data; };
ListNode* Next() { return next; };
};
// List class
class List {
ListNode *head;
public:
List() { head = NULL; };
void displayList();
void appendItem(int data);
void deleteItem(int data);
};
/**
* display the contents of the list
*/
void List::displayList() {
// Temporary pointer
ListNode *tmpNode = head;
// there is No node
if (tmpNode == NULL) {
cout << "EMPTY LIST" << endl;
return;
}
// 1 ListNode in the list
if ( tmpNode->Next() == NULL ) {
cout << tmpNode->getData();
cout << "-->";
cout << "NULL node" << endl;
}
else {
// go through the items and display the list
do {
cout << tmpNode->getData();
cout << "-->";
tmpNode = tmpNode->Next();
}
while ( tmpNode != NULL );
cout << "NULL node" << endl;
}
}
/**
* append an item to the linked list
*/
void List::appendItem(int data) {
// Create a new ListNode
ListNode* newNode = new ListNode();
newNode->setInt(data);
newNode->setNextNode(NULL);
// Create a temporary pointer
ListNode *tmpNode = head;
if ( tmpNode != NULL ) {
// Nodes are already present in the list
// Parse the items to end of the list
while ( tmpNode->Next() != NULL ) {
tmpNode = tmpNode->Next();
}
// Point the last ListNode to the new ListNode
tmpNode->setNextNode(newNode);
}
else {
// First ListNode in the list
head = newNode;
}
}
/**
* delete an item from the list
*/
void List::deleteItem(int data) {
// Create a temporary pointer
ListNode *tmpNode = head;
// No nodes in the list
if ( tmpNode == NULL )
return;
// Last ListNode of the list
if ( tmpNode->Next() == NULL ) {
deleteItem tmpNode;
head = NULL;
}
else {
// Parse through the nodes
ListNode *prev;
do {
if ( tmpNode->getData() == data ) break;
prev = tmpNode;
tmpNode = tmpNode->Next();
} while ( tmpNode != NULL );
// Adjust the node pointers
prev->setNextNode(tmpNode->Next());
// delete the current ListNode
deleteItem tmpNode;
}
}
int main()
{
// declare a new list
List list;
// append nodes to the linked list
list.appendItem(100);
list.displayList();
list.appendItem(200);
list.displayList();
list.appendItem(300);
list.displayList();
list.appendItem(400);
list.displayList();
list.appendItem(500);
list.displayList();
// delete nodes from the linked list
list.deleteItem(400);
list.displayList();
list.deleteItem(300);
list.displayList();
list.deleteItem(200);
list.displayList();
list.deleteItem(500);
list.displayList();
list.deleteItem(100);
list.displayList();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.