Answered step by step
Verified Expert Solution
Question
1 Approved Answer
LinkedListType code: #include using namespace std; #ifndef LINKEDLISTTYPE_H_INCLUDED #define LINKEDLISTTYPE_H_INCLUDED //Definition of the node template struct nodeType { Type info; nodeType *link; }; template class
LinkedListType code:
#includeusing namespace std; #ifndef LINKEDLISTTYPE_H_INCLUDED #define LINKEDLISTTYPE_H_INCLUDED //Definition of the node template struct nodeType { Type info; nodeType *link; }; template class linkedListType { public: void initializeList(); bool isEmptyList() const; void print() const; int length() const; void destroyList(); Type front() const; Type back() const; bool search(const Type& searchItem) const; void insertFirst(const Type& newItem); void insertLast(const Type& newItem); void deleteNode(const Type& deleteItem); nodeType * begin(); nodeType * end(); linkedListType(); ~linkedListType(); linkedListType(const linkedListType & otherList); const linkedListType & operator= (const linkedListType &);/o protected: int count; nodeType *first; nodeType *last; private: void copyList(const linkedListType & otherList); }; template bool linkedListType ::isEmptyList() const { return (first == NULL); } template linkedListType ::linkedListType() //default constructor { first = NULL; last = NULL; count = 0; } template void linkedListType ::destroyList() { nodeType *temp; //pointer to deallocate the memory //occupied by the node { temp = first; //set temp to the current node first = first->link; //advance first to the next node delete temp; //deallocate the memory occupied by temp } last = NULL; //initialize last to NULL; first has already //been set to NULL by the while loop count = 0; } template void linkedListType ::initializeList() { destroyList(); //if the list has any nodes, delete them } template void linkedListType ::print() const { nodeType *current; //pointer to traverse the list current = first; //set current so that it points to //the first node while (current != NULL) //while more data to print { cout info link; } }//end print template int linkedListType ::length() const { return count; } template Type linkedListType ::front() const { assert(first != NULL); return first->info; //return the info of the first node }//end front template Type linkedListType ::back() const { assert(last != NULL); return last->info; //return the info of the last node }//end back template nodeType * linkedListType ::begin() { return first; } template nodeType * linkedListType ::end() { return last; } template void linkedListType ::copyList (const linkedListType & otherList) { nodeType *newNode; //pointer to create a node nodeType *current; //pointer to traverse the list if (first != NULL) //if the list is nonempty, make it empty destroyList(); if (otherList.first == NULL) //otherList is empty { first = NULL; last = NULL; count = 0; } else { current = otherList.first; //current points to the //list to be copied count = otherList.count; //copy the first node first = new nodeType ; //create the node first->info = current->info; //copy the info first->link = NULL; //set the link field of //the node to NULL last = first; //make last point to the //first node current = current->link; //make current point to //the next node //copy the remaining list while (current != NULL) { newNode = new nodeType ; //create a node newNode->info = current->info; //copy the info newNode->link = NULL; //set the link of /ewNode to NULL last->link = newNode; //attach newNode after last last = newNode; //make last point to //the actual last node current = current->link; //make current point //to the next node }//end while }//end else }//end copyList template linkedListType ::~linkedListType() //destructor { destroyList(); } template linkedListType ::linkedListType (const linkedListType & otherList) { first = NULL; copyList(otherList); }//end copy constructor template const linkedListType & linkedListType ::operator= (const linkedListType & otherList) { if (this != &otherList) //avoid self-copy { copyList(otherList); }//end else return *this; } template bool linkedListType :: search(const Type& searchItem) const { nodeType *current; //pointer to traverse the list bool found = false; current = first; //set current to point to the first /ode in the list while (current != NULL && !found) //search the list if (current->info == searchItem) //searchItem is found found = true; else current = current->link; //make current point to //the next node return found; }//end search template void linkedListType ::insertFirst(const Type& newItem) { nodeType *newNode; //pointer to create the new node newNode = new nodeType ; //create the new node newNode->info = newItem; //store the new item in the node newNode->link = first; //insert newNode before first first = newNode; //make first point to the //actual first node count++; //increment count if (last == NULL) //if the list was empty, newNode is also //the last node in the list last = newNode; }//end insertFirst template void linkedListType ::insertLast(const Type& newItem) { nodeType *newNode; //pointer to create the new node newNode = new nodeType ; //create the new node newNode->info = newItem; //store the new item in the node newNode->link = NULL; //set the link field of newNode //to NULL if (first == NULL) //if the list is empty, newNode is //both the first and last node { first = newNode; last = newNode; count++; //increment count } else //the list is not empty, insert newNode after last { last->link = newNode; //insert newNode after last last = newNode; //make last point to the actual //last node in the list count++; //increment count } }//end insertLast template void linkedListType ::deleteNode(const Type& deleteItem) { nodeType *current; //pointer to traverse the list nodeType *trailCurrent; //pointer just before current bool found; if (first == NULL) //Case 1; the list is empty cout info == deleteItem) //Case 2 { current = first; first = first->link; count--; if (first == NULL) //the list has only one node last = NULL; delete current; } else //search the list for the node with the given info { found = false; trailCurrent = first; //set trailCurrent to point //to the first node current = first->link; //set current to point to //the second node while (current != NULL && !found) { if (current->info != deleteItem) { trailCurrent = current; current = current-> link; } else found = true; }//end while if (found) //Case 3; if found, delete the node { trailCurrent->link = current->link; count--; if (last == current) /ode to be deleted //was the last node last = trailCurrent; //update the value //of last delete current; //delete the node from the list } else cout StackType:
#pragma once #include#include using namespace std; class stackType { int maxStackSize; int stackTop; int* list; void copyStack(const stackType&); public: bool isEmptyStack() const; bool isFullStack() const; void initializeStack(); void push(const int&); void pop(); int top() const; const stackType& operator=(const stackType&); stackType(int = 100); ~stackType(); stackType(const stackType&); }; #include"stackType.h" bool stackType::isEmptyStack() const { return (stackTop == 0); } bool stackType::isFullStack() const { return (stackTop == maxStackSize); } stackType::stackType(int s) { if (s Part 1: Include the header file for the template class LinkedListType to your program. Include the header file for the template class StackType to your program. Part3: Write the following functions to deal with a linked list and the stack class as nonmember functions: 1) InsertForward: this function should copy all the element from the Stack to a linked list, insert the new nodes at the end of the linked list. 2) InsertBackward: this function should copy the nodes from linked list to another linked list, insert the new nodes at the beginning of the linked list. 3) Print List: this function print the linked list content. 4) SearchItem: this function takes a lined list and a value, then search for the value in the list and return the node contains this value. 5) InsertAt: this function insert a new node in a specific location. 6) Search Index: this function return a node from a specific location. 7) CopyList: this function should copy all the elements from the link list to the stack. To test your functions create an object of integer from StackType and two LinkedListType, then call your function in appropriate order
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started