/**************************************************************************************** * Program Name: linkedListType.h * Author: Zhixiang Chen * Course: CSCI/CMPE 3333, Fall 14 * Lab 7: Header file for Lab 7 * Date: 08/2014 * Description: This file contains the prototype of the class linkedListType * and the linked-list's node struct definition. *****************************************************************************************/ #ifndef H_linkedListType #define H_linkedListType #include #include #include using namespace std; template struct nodeType { Type info; nodeType *link; }; template class linkedListType { friend ostream & operator<<<>(ostream &, linkedListType & ); public: linkedListType & operator= // overloading assignment operator ( linkedListType&); void initializeList(); // initialize the list to an empty list bool isEmpty(); // chech whether list is empty int length(); // read the number of nodes in list void destroyList(); // to delete all nodes from the list Type front(); // return the first element in the list Type back(); // return the last element in the list bool search( Type & searchItem); // to determine the searchItem is in the list // return true if yes or no otherwise void insertFirst( Type & newItem); // insert the newItem to the beginning of the list void insertLast( Type & newItem); // insert the newItem to the end of the list void deleteNode( Type & deleteItem); // delete the node containing the input item from the list void print(); // print the list linkedListType(); // default ructor linkedListType( linkedListType&); // copy ructor ~linkedListType(); // the destructor bool getNext( Type & ); protected: int count; // store the number of nodes in the list nodeType *first; // the pointer to the first node nodeType *last; // the pointer to the last node nodeType *next; private: void copyList( linkedListType & otherList); //copy other list to the invoking list }; template bool linkedListType::getNext(Type & out) { if (next==NULL) { next = first; return false; } else { out = next->info; next = next->link; return true; } } // print the list template ostream & operator<<<>(ostream& os, linkedListType & rhs) { nodeType *current; //point to current node current = rhs.first; while(current != NULL) { os<info; current = current->link; } return os; } // print the list template void linkedListType::print( ) { nodeType *current; //point to current node current = first; while(current != NULL) { cout<info; current = current->link; } } // initialize the list to an empty list template void linkedListType :: initializeList() { destroyList(); //destroy the list to an empty one } // chech whether list is empty template bool linkedListType::isEmpty() { return count == 0; } // read the number of nodes in list template int linkedListType::length() { return count; } // to delete all nodes from the list template void linkedListType::destroyList() { nodeType *tmp; //local node ptr to help to delete a node while (first!=NULL) //loop until no more nodes are in the list { tmp = first; //get the current node first = first->link; //move to the next node delete tmp; //delete the current node } next = first = last = NULL; count = 0; } // return the first element in the list template Type linkedListType::front() { assert(first!=NULL); //to make sure the list is not empty return first->info; } // return the last element in the list template Type linkedListType::back() { assert(last!=NULL); //to make sure the list is not empty return last->info; } // to determine the searchItem is in the list // return true if yes or no otherwise template bool linkedListType::search( Type & searchItem) { nodeType *current; //ptr to current node bool found = false; //flag for founding status current = first; //point to first while (!found && current!=NULL) { if (current->info == searchItem) found = true; //find the searchItem else //otherwise move to the next node current = current->link; } return found; } // insert the newItem to the beginning of the list template void linkedListType::insertFirst( Type & newItem) { nodeType *newNode; //for creating a new new newNode = new nodeType; //create a new node assert(newNode != NULL); //make sure the new node in indeed created newNode->info = newItem; //load info to new node newNode->link = first; //add new node the front of the list first = newNode; //reset first count++; //increase the count if (last == NULL) //reset last if it is empty last = newNode; next = first; } // insert the newItem to the end of the list template void linkedListType::insertLast( Type & newItem) { nodeType *newNode; //for creating a new new newNode = new nodeType; //create a new node assert(newNode != NULL); //make sure the new node in indeed created newNode->info = newItem; //load info to new node newNode->link = NULL; //make sure the new node is the last one count++; //increase the count if (first == NULL) //when the list is empty { first = last = newNode; } else { last->link = newNode; last = newNode; } next = first; } // delete the node containing the input item from the list template void linkedListType::deleteNode( Type & deleteItem) { nodeType *current, //point to current node *previous; //point to next node bool found; //flag for finding the node containing deleteItem //case 1: the list is empty if(isEmpty()) return; //case 2: the first is to be deleted else if (first->info == deleteItem) { current = first->link; //get the second node if there is one delete first; //delete the first node if (current == NULL) //there is only one node { first = last = NULL; } else { first = current; } count--; return; } //case 3: find the node after the first one else { previous = first; current = first->link; found = false; while (!found && current!=NULL) { if (current->info == deleteItem) { found = true; } else { previous = current; current = current->link; } } if (found) { previous->link = current->link; //skip the current node count--; if (last == current) //delete the last node last = previous; delete current; } } next = first; } // default ructor template linkedListType::linkedListType() { count = 0; next = first = last = NULL; } // copy ructor template linkedListType::linkedListType( linkedListType& otherList) { next = first = NULL; copyList(otherList); } //destructor template linkedListType::~linkedListType() { destroyList(); } //copy list template void linkedListType::copyList( linkedListType & otherList) { nodeType *newNode, //for creating a new node *current; //point to current node if (!isEmpty()) //if list is not empty then destroy it destroyList(); if (otherList.isEmpty()) //otherList is empty { next = first = last = NULL; count = 0; return; } count = otherList.count; //copy the count //otherList is not empty. first take care of the first node first = new nodeType; first->info = otherList.first->info; first->link = NULL; last = first; next = first; //now take care of additional nodes if any current = otherList.first->link; while (current != NULL) { newNode = new nodeType; newNode -> info = current->info; newNode -> link = NULL; last->link = newNode; last = newNode; current = current->link; } } // overloading assignment operator template linkedListType& linkedListType::operator=( linkedListType& otherList) { if (this != & otherList) { copyList(otherList); } return *this; } #endif