Please code this in C++, Please use the driver and cpp file to code this assignm
ID: 3592548 • Letter: P
Question
Please code this in C++, Please use the driver and cpp file to code this assignment!
Please also look at the thing in bold carefully as they are the most important!
----------------------------------------------------------------------------------------------------------
For the assignment, we will design a descending order arranged linked list class. This linked list class should hold a series of char type values. The class should have member functions for appending, inserting, and deleting nodes. Also, it will need a destructor that destroys the list.
For this assignment, you will need to provide these public member functions that perform the following tasks:
A) appendNode appends a node containing the char value passed, to the end of the list.
B) insertNode this function inserts a node with the char parameter’s value copied to its char member element. This function must insert the values in descending order. Duplicates are okay.
C)deleteNode this function searches for a node with char parameter’s value as the element to find. The node, if found, is deleted from the list and from memory. If two or more nodes with the same value are found, only one is removed (your choice which one).
The CharList class member-function definitions have been started for you with the displayList function and the destructor already given. Make sure to use the driver program as well. Find both the driver function and member-function definition files that have been provided in the folder under charList_main.cpp and charList.cpp, respectively. Please use the driver and make sure your program can execute the test cases successfully.
DRIVER PROGRAM/MAIN:
#include <iostream>
#include "charList.hpp"
int main()
{
CharList list;
list.appendNode('t');
list.appendNode('s');
list.appendNode('n');
list.appendNode('m');
list.appendNode('j');
list.appendNode('f');
list.appendNode('c');
list.appendNode('a');
list.insertNode('y');
list.insertNode('0');
list.insertNode('9');
list.insertNode('o');
list.insertNode('p');
list.insertNode('@');
list.insertNode('Q');
list.deleteNode('t');
list.displayList();
return 0;
}
charList.cpp:
void CharList::displayList() const
{
ListNode *nodePtr;
nodePtr = head;
short count = 0;
while (nodePtr)
{
std::cout << "[" << nodePtr->value << "] -> ";
++count;
nodePtr = nodePtr->next;
if (count % 10 == 0) {
std::cout << std::endl;
count = 0;
}
}
std::cout << std::endl;
}
CharList::~CharList()
{
ListNode *nodePtr;
ListNode *nextNode;
nodePtr = head;
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
Explanation / Answer
i have written the enitre code from scratch - please find below:
#include <iostream>
using namespace std;
struct ListNode {
char value;
ListNode *next;
};
ListNode *head;
class LinkedList {
public:
char insertNode(char character);
char appendNode(char character);
void deleteNode(char character);
void destroyList();
void displayList();
LinkedList(void) {head = NULL;}
~LinkedList(void) {destroyList();}
};
char LinkedList::appendNode(char character)
{
ListNode *newNode, *nodePtr = head;
newNode = new ListNode;
if(newNode == NULL) {
cout << "Error allocating memory for new list member! ";
return 1;
}
newNode->value = character;
newNode->next = NULL;
if(head == NULL) {
cout << "List was empty - " << newNode->value;
cout << " is part of list's first node. ";
head = newNode;
}
else {
while(nodePtr->next != NULL)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
return 0;
}
char LinkedList::insertNode(char character)
{
struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;
newNode = new ListNode;
if(newNode == NULL) {
cout << "Error allocating memory for new list member! ";
return 1;
}
newNode->value = character;
newNode->next = NULL;
if(head==NULL) {
cout << "List was empty - " << newNode->value;
cout << " is part of list's first node. ";
head = newNode;
}
else {
while((nodePtr != NULL) && (nodePtr->value < character)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(prevNodePtr==NULL)
newNode->next = head;
//This if statement is not complete. What is head supposed to pochar to afterwards.
else
newNode->next = nodePtr; prevNodePtr->next = newNode;
}
return 0;
}
void LinkedList::deleteNode(char character)
{
struct ListNode *nodePtr = head, *prevNodePtr = NULL;
if(head==NULL) {
cout << "The list was empty! ";
return;
}
if(head->value == character) {
head = nodePtr->next;
delete [] nodePtr;
}
else {
while((nodePtr!= NULL)&&(nodePtr->value != character)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr==NULL)
cout << "The value " << character << " is not in this list! ";
else {
prevNodePtr->next = nodePtr->next;
delete [] nodePtr;
}
}
}
void LinkedList::destroyList()
{
struct ListNode *nodePtr = head, *nextNode = nodePtr;
if(head==NULL) {
cout << "The list is empty! ";
return;
}
while (nodePtr != NULL) {
nextNode = nodePtr->next;
delete [] nodePtr;
nodePtr = nextNode;
}
}
void LinkedList::displayList()
{
struct ListNode *nodePtr = head;
if (nodePtr == NULL)
cout << "The list is empty! ";
else {
while (nodePtr != NULL) {
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
}
char main()
{
char character;
character answer, choice;
LinkedList temp;
do {
cout << "Please enter a character value to put in the list --> ";
cin >> character;
temp.insertNode(character);
cout << endl;
cout << "Would you like to put another value charo your list? ";
cin >> answer;
} while(toupper(answer)=='Y');
cout << "I will now display your list! ";
cout << endl;
temp.displayList();
}
Edit & Run
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.