Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project-1 Please modify our existing ClassDemo2 program and add characters ( char ) and names ( strings ) to be added to the linked list

Project-1

Please modify our existing ClassDemo2 program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices:

(a) whole numbers

(b) single characters

(c) strings

Once the user makes a selection from the question "Enter 1 for for char, enter 2 for strings, or 3 for integer ending with a x" the list above then your program should be able to process the data appropriately. This includes:

(a) insert

(b) print

(c) delete

(d) search

(e) copy

You will use the existing code (class demo file) and modify it to process char and string data type.

Please upload .cpp file(s) along with screenshots of all solutions/screens. You can take a screenshot of your solutions by hitting PrintScreen button on your keyboard and then by pasting that image into a Word document.

//This program tests various operation of a linked list //45 67 23 89 -999 #include  #include  using namespace std; template  struct nodeType { Type info; nodeType *link; }; template  class circularLinkedList { public: //Overloads the assignment operator. const circularLinkedList& operator=(const circularLinkedList& otherList) { if (this != &otherList) //avoid self-copy { copyList(otherList); }//end else return *this; } //Initializes the list to an empty state. //Postcondition: first = NULL, last = NULL, // count = 0 void initializeList() { destroyList(); } //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty; otherwise, returns false. bool isEmptyList() { return (first == NULL); } void print() const { nodeType *current; //pointer to traverse the list current = first->link; while (current != first) //while more data to print { cout << current->info << " "; current = current->link; } cout << first->info << " "; } //Function to return the number of nodes in the list. //Postcondition: The value of count is returned. int length() { return count; } //Function to delete all the nodes from the list. //Postcondition: first = NULL, last = NULL, // count = 0 void destroyList() { nodeType *temp; nodeType *current = NULL; if (first != NULL) { current = first->link; first->link = NULL; } while (current != NULL) { temp = current; current = current->link; delete temp; } first = NULL; //initialize last to NULL; first has already //been set to NULL by the while loop count = 0; } //Function to return the first element of the list. //Precondition: The list must exist and must not be empty. //Postcondition: If the list is empty, then the program terminates; otherwise, the first element of the list is returned. Type front() { assert(first != NULL); return first->link->info; //return the info of the first node } //Function to return the last element of the list. //Precondition: The list must exist and must not be empty. //Postcondition: If the list is empty, then the program terminates; otherwise, the last element of the list is returned. Type back() { assert(first != NULL); return first->info; //return the info of the first node } //Function to determine whether searchItem is in the list. //Postcondition: Returns true if searchItem is found in the list; otherwise, it returns false. bool search(const Type& searchItem) { nodeType *current; //pointer to traverse the list bool found = false; if (first != NULL) { current = first->link; while (current != first && !found) { if (current->info >= searchItem) found = true; else current = current->link; found = (current->info == searchItem); } } return found; } void insertNode(const Type& newitem) { nodeType *current; //pointer to traverse the list nodeType *trailCurrent; //pointer just before current nodeType *newNode; //pointer to create a node bool found; newNode = new nodeType; //create the node newNode->info = newitem; //store newitem in the node newNode->link = NULL; //set the link field of the node //to NULL if (first == NULL) //Case 1 e.g., 3 { first = newNode; first->link = newNode; count++; } else { if (newitem >= first->info)//e.g., 25 > 3 { newNode->link = first->link; first->link = newNode; first = newNode; } else { trailCurrent = first; //e.g., 1 < 3 current = first->link; found = false; while (current != first && !found) if (current->info >= newitem) found = true; else { trailCurrent = current; current = current->link; } trailCurrent->link = newNode; newNode->link = current; } count++; }//end else } //Function to delete deleteItem from the list. //Postcondition: If found, the node containing deleteItem is deleted from the list, first points to the first // node, and last points to the last node of the updated list. void deleteNode(const Type& deleteItem) { nodeType *current; //pointer to traverse the list nodeType *trailCurrent; //pointer just before current bool found; if (first == NULL) //Case 1; list is empty. cout << "Can not delete from an empty list." << endl; else { found = false; trailCurrent = first; current = first->link; while (current != first && !found) if (current->info >= deleteItem) found = true; else { trailCurrent = current; current = current->link; } if (current == first) { if (first->info == deleteItem) { if (first == first->link) first = NULL; else { trailCurrent->link = current->link; first = trailCurrent; } delete current; count--; } else cout << "The item to be deleted is not in the list." << endl; } else if (current->info == deleteItem) { trailCurrent->link = current->link; count--; delete current; } else cout << "Item to be deleted is not in the list." << endl; } //end else } //Default constructor //Initializes the list to an empty state. //Postcondition: first = NULL, last = NULL, // count = 0 circularLinkedList() { first = NULL; count = 0; } //Copy constructor circularLinkedList(const circularLinkedList& otherList) { first = NULL; copyList(otherList); } //Destructor //Deletes all the nodes from the list. //Postcondition: The list object is destroyed. ~circularLinkedList() { destroyList(); } protected: int count; //variable to store the number of elements in the list nodeType *first; //pointer to the first node of the list nodeType *last; //pointer to the last node of the list private: //Function to make a copy of otherList. //Postcondition: A copy of otherList is created and assigned to this list. void copyList(const circularLinkedList& otherList) { nodeType *newNode; nodeType *current; nodeType *tempFirst; if (first != NULL) destroyList(); if (otherList.first == NULL) { first = NULL; count = 0; } else { current = otherList.first->link; //current points to the //list to be copied count = otherList.count; //copy the first node tempFirst = new nodeType; //create the node tempFirst->info = current->info; //copy the info last = tempFirst; //make last point to the //first node current = current->link; //make current point to the //next node //copy the remaining list while (current != otherList.first) { newNode = new nodeType; //create a node newNode->info = current->info; last->link = newNode; last = newNode; current = current->link; }//end while if (tempFirst == last) { first = tempFirst; first->link = first; } else { newNode = new nodeType; //create a node newNode->info = current->info; last->link = newNode; first = newNode; first->link = tempFirst; } }//end else } }; int main() { circularLinkedList list1, list2; int num; cout << ""Enter 1 for for char, enter 2 for strings, or 3 for integer ending with a x"" << endl; cin >> num; while (num != -999) { list1.insertNode(num); cin >> num; } cout << endl; cout << "List 1: "; list1.print(); cout << endl; cout << "Length List 1: " << list1.length() << endl; cout << "Enter the number to be searched: "; cin >> num; cout << endl; if (list1.search(num)) cout << num << " found in the list" << endl; else cout << num << " not in the list" << endl; cout << "Enter the number to be deleted: "; cin >> num; cout << endl; list1.deleteNode(num); cout << "After deleting the node, " << "List 1: "; list1.print(); cout << endl; cout << "Length List 1: " << list1.length() << endl; list2 = list1; cout << "List 2: "; list2.print(); cout << endl; cout << "Length List 2: " << list2.length() << endl; cout << "List 1: "; list1.print(); cout << endl; return 0; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Structure Of The Relational Database Model

Authors: Jan Paredaens ,Paul De Bra ,Marc Gyssens ,Dirk Van Gucht

1st Edition

3642699588, 978-3642699580

More Books

Students also viewed these Databases questions