Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include using namespace std; template struct nodeType { Type info; nodeType *link; }; template class circularLinkedList { public: //Overloads the assignment operator. const circularLinkedList

image text in transcribed

#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 info link; } cout info  *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 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 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 info == deleteItem) { trailCurrent->link = current->link; count--; delete current; } else cout & 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 /ext 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 > num; while (num != -999) { list1.insertNode(num); cin >> num; } cout > num; cout > num; cout  

Please modify our existing ClassDemo2 (please find it on Canvas site) 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 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 modify our existing ClassDemo2 (please find it on Canvas site) 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 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

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

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions