Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.67 KB | None | 0 0
  1. //node.h
  2. #ifndef NODE_H
  3. #define NODE_H
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. template<class Type>
  10. struct nodeType
  11. {
  12.     Type info;
  13.     nodeType<Type> *link;
  14. };
  15.  
  16. #endif
  17. //end of node.h
  18.  
  19. //linkedListIterator.h
  20. #ifndef LINKED_LIST_ITERATOR_H
  21. #define LINKED_LIST_ITERATOR_H
  22. #include <iostream>
  23. #include "node.h"
  24.  
  25. using namespace std;
  26.  
  27. template <class Type>
  28. class linkedListIterator
  29. {
  30.     public:
  31.         linkedListIterator();
  32.         //default constructor.
  33.         //postCondition: current = null;
  34.  
  35.         linkedListIterator(nodeType<Type> *ptr);
  36.         //constructor with aparameter.
  37.         //postCondition: current = ptr;
  38.  
  39.         Type operator* ();
  40.         //function to overload the dereferencing operator *.
  41.         //postCondition: returns the info contained in the node.
  42.  
  43.         linkedListIterator<Type> operator++();
  44.         //overload the pre-increment operator.
  45.         //postCondition: the iterator is advanced to the next node.
  46.  
  47.         bool operator == (const linkedListIterator<Type> &right) const;
  48.         //overload the equality operator.
  49.         //postCondition: Returns true if this iterator is equal to
  50.         //               the iterator specified by right,
  51.         //               otherwise it returns false.
  52.  
  53.         bool operator !=(const linkedListIterator<Type> &right) const;
  54.         //overload the not equal to operator
  55.         //postCondition: returns true if this iterator is not equal
  56.         //               to the iterator specified by right,
  57.         //               otherwise it returns false.
  58.  
  59.     private:
  60.         nodeType<Type> *current;
  61.         //pointer to point to the current node in the linked list
  62. };
  63. #endif
  64. //end of linkedListIterator.h
  65.  
  66. //linkedListIterator.cpp
  67. #include <iostream>
  68. #include "linkedListIterator.h"
  69. #include "node.h"
  70.  
  71. using namespace std;
  72.  
  73. template <class Type>
  74. linkedListIterator<Type>::linkedListIterator()
  75. {
  76.     current = NULL;
  77. }
  78. template <class Type>
  79. linkedListIterator<Type>::
  80.                         linkedListIterator(nodeType<Type> *ptr)
  81. {
  82.     current = ptr;
  83. }
  84.  
  85. template <class Type>
  86. Type linkedListIterator<Type>::operator* ()
  87. {
  88.     return current->info;
  89. }
  90.  
  91. template <class Type>
  92. linkedListIterator<Type> linkedListIterator<Type>::operator++ ()
  93. {
  94.     current = current -> link;
  95.     return *this;
  96. }
  97.  
  98. template <class Type>
  99. bool linkedListIterator<Type>::operator ==
  100.                     (const linkedListIterator<Type> &right) const
  101. {
  102.     return (current == right.current);
  103. }
  104.  
  105. template <class Type>
  106. bool linkedListIterator<Type>::operator!=
  107.                     (const linkedListIterator<Type> &right) const
  108. {
  109.     return (current != right.current);
  110. }
  111.  
  112. //end of linkedListIterator.cpp
  113.  
  114. //linkedListType.h
  115. #ifndef LINKED_LIST_TYPE_H
  116. #define LINKED_LIST_TYPE_H
  117. #include <iostream>
  118. #include "linkedListIterator.h"
  119. #include "node.h"
  120.  
  121. using namespace std;
  122.  
  123. template <class Type>
  124. class linkedListType
  125. {
  126. public:
  127.     const linkedListType<Type> & operator=
  128.                                 (const linkedListType<Type> &);
  129.     //overload the assignment operator.
  130.    
  131.     void initializeList();
  132.     //initialize the list to an empty state.
  133.     //postCondition: first = NULL, last = NULL, count = 0;
  134.    
  135.     bool isEmptyList() const;
  136.     //function to ditermine whether the list is empty.
  137.     //postCondition: returns true if the list is empty,
  138.     //otherwise returns false
  139.    
  140.     void print() const;
  141.     //function to output the data contained in each node.
  142.     //postcondition: none
  143.    
  144.     int length() const;
  145.     //function to return the number of nodes in the list.
  146.     //postcondition: the value of count is returned.
  147.    
  148.     void destroyList();
  149.     //function to delete all the nodes from the list.
  150.     //postCondition: first = NULL, last = NULL, count = 0;
  151.    
  152.     Type front() const;
  153.     //function to return the first element of the list.
  154.     //preCondition: the list must exist and must not be empty.
  155.     //postCondition: if the list is empty, the program
  156.     //                  terminates; otherwise, the first
  157.     //                  element of the list is returned.
  158.    
  159.     Type back() const;
  160.     //function to return the last element of the list.
  161.     //preCondition: the list must exist and must not be empty.
  162.     //postCondition: if the list is empty, the program terminates;
  163.     //                      otherwise the last element
  164.     //                      of the list is returned.
  165.    
  166.     virtual bool search(const Type& searchItem) const = 0;
  167.     //function to determine whether searchItem is in the list.
  168.     //postCondition: returns true if searchItem is in the list.
  169.     //otherwise the value false is returned
  170.    
  171.     virtual void insertFirst(const Type &newItem) = 0;
  172.     //function to insert newItem at the beginning of the list.
  173.     //postCondition: first points to the new list, newItem is
  174.     //              inserted at the end of the list,
  175.     //              last points to the last node in the list,
  176.     //              and count is incremented by 1.
  177.    
  178.     virtual void insertLast(const Type &newItem) = 0;
  179.     //function to insert newItem at the end of the list
  180.     //postCondition: first points to the new list,
  181.     //               newItem is inserted
  182.     //               at the end of the list,
  183.     //               last points to the last node in the list,
  184.     //               and count is incremented by 1.
  185.    
  186.     virtual void deleteNode(const Type &deleteItem) = 0;
  187.     //function to delete deleteItem from the list.
  188.     //postCondition: if found, the node containing
  189.     //               deleteItem is deleted from the list.
  190.     //               first points to the first node,
  191.     //               last points to the last node of the updated
  192.     //               list, and count is decremented by 1.
  193.    
  194.     linkedListIterator<Type> begin();
  195.     //function to return an iterator at the beginning of the linked list
  196.     //postCondition: returns an iterator such that current is set to first.
  197.    
  198.     linkedListIterator<Type> end();
  199.     //function to return an iterator one element past the last
  200.     //element of the linked list.
  201.     //postCondition: returns an iterator such that current is set to NULL
  202.    
  203.     linkedListType();
  204.     //default constructor
  205.     //initializes the list to an empty state.
  206.     //postCondition: first = NULL, last = NULL, count = 0;
  207.    
  208.     linkedListType(const linkedListType<Type> &otherList);
  209.     //copy constructor
  210.    
  211.     ~linkedListType();
  212.     //destructor
  213.     //deletes all the nodes from the list.
  214.     //postCondition: the list object is destroyed.
  215.    
  216. protected:
  217.     int count; //variable to store the number of elements in the list
  218.     nodeType<Type> *first; //pointer to the first node of the list
  219.     nodeType<Type> *last; //pointer to the last node of the list
  220.  
  221. private:
  222.     void copyList(const linkedListType<Type> &otherList);
  223.     //function to make a copy of otherList.
  224.     //postCondition: a copy of otherList is created and assigned to this list.
  225.  
  226. };
  227. #endif
  228. //end of linkedListType.h
  229.  
  230. //linkedListType.cpp
  231. #include <iostream>
  232. #include "linkedListIterator.h"
  233. #include "linkedListType.h"
  234.  
  235. using namespace std;
  236.  
  237. template <class Type>
  238. bool linkedListType<Type>::isEmptyList() const
  239. {
  240.     return (first == NULL);
  241. }
  242.  
  243. template <class Type>
  244. linkedListType<Type>::linkedListType() //default constructor
  245. {
  246.     first = NULL;
  247.     last = NULL;
  248.     count = 0;
  249. }
  250.  
  251. template <class Type>
  252. void linkedListType<Type>::destroyList()
  253. {
  254.     nodeType<Type> *temp; //pointer to deallocate the memory
  255.                          //occupied by the node
  256.     while (first != NULL) //while there are nodes in the list
  257.     {
  258.         temp = first;    //set temp to the current node
  259.         first = first -> link; //advance first to the next node
  260.         delete temp;        //deallocate the memory occupied by temp
  261.     }
  262.  
  263.     last = NULL; //initialize last to NULL; first has already
  264.                 //been set to NULL by the while loop
  265.     count = 0;
  266. }
  267.  
  268. template <class Type>
  269. void linkedListType<Type>::initializeList()
  270. {
  271.     destroyList(); //if the list has any nodes, delete them
  272. }
  273.  
  274. template <class Type>
  275. void linkedListType<Type>::print() const
  276. {
  277.     nodeType<Type> *current; //pointer to traverse the list
  278.  
  279.     current = first; //set current so that it points to the first node
  280.  
  281.     while (current != NULL) //while more data to print
  282.     {
  283.         cout << current -> info << " ";
  284.         current = current -> link;
  285.     }
  286. } //end print
  287.  
  288. template <class Type>
  289. int linkedListType<Type>::length() const
  290. {
  291.     return count;
  292. }
  293.  
  294. template <class Type>
  295. Type linkedListType<Type>::front() const
  296. {
  297.     assert (first != NULL);
  298.     return first -> info; //return the info of the first node
  299. }//end front
  300. template <class Type>
  301. Type linkedListType<Type>::back() const
  302. {
  303.     assert (last != NULL);
  304.  
  305.     return last -> info; //return the info of the last node
  306. }//end back
  307.  
  308. template <class Type>
  309. linkedListIterator<Type> linkedListType<Type>::begin()
  310. {
  311.     linkedListIterator<Type> temp(first);
  312.  
  313.     return temp;
  314. }
  315.  
  316. template <class Type>
  317. linkedListIterator<Type> linkedListType<Type>::end()
  318. {
  319.     linkedListIterator<Type> temp(NULL);
  320.  
  321.     return temp;
  322. }
  323.  
  324. template <class Type>
  325. void linkedListType<Type>::copyList
  326.                     (const linkedListType<Type> &otherList)
  327. {
  328.     nodeType<Type> *newNode; //pointer to create a node
  329.     nodeType<Type> *current; //pointer to traverse the list
  330.  
  331.     if (first != NULL) //if the list is nonempty, make it empty
  332.         destroyList();
  333.    
  334.     if (otherList.first == NULL) //otherList is empty
  335.     {
  336.         first = NULL;
  337.         last = NULL;
  338.         count = 0;
  339.     }
  340.     else
  341.     {
  342.         current = otherList.first; //current point to the list to be copied
  343.         count = otherList.count;
  344.         //copy the first node
  345.         first = new nodeType<Type>; //create the node
  346.         first -> info = current -> info; //copy the info
  347.         first -> link = NULL;       //set the link field of the node to NULL
  348.         last = first;               //make last point to the first node
  349.         current = current -> link;  //make current point to the next node
  350.  
  351.         //copy the remaining list
  352.  
  353.         while(current != NULL)
  354.         {
  355.             newNode = new nodeType<Type>; //create a node
  356.             newNode -> info = current -> info; //copy the info
  357.             newNode -> link = NULL;         //set the link of newNode to NULL
  358.             last -> link = newNode;         //attach a newNode after last
  359.             last = newNode;                 //make last point to the actual last node
  360.             current = current -> link;      //make current point to the next node
  361.         }//end while
  362.     }//end else
  363. }//end copyList
  364.  
  365. template<class Type>
  366. linkedListType<Type>::~linkedListType() //destructor
  367. {
  368.     destroyList();
  369. }
  370.  
  371. template<class Type>
  372. linkedListType<Type>::linkedListType(const linkedListType<Type> &otherList)
  373. {
  374.     first = NULL;
  375.     copyList(otherList);
  376. } //end copy constructor
  377.  
  378. template<class Type>
  379. const linkedListType<Type> &linkedListType<Type>::operator=
  380.                             (const linkedListType<Type> &otherList)
  381. {
  382.     if (this != &otherList) //avoid self-copy
  383.     {
  384.         copyList(otherList);
  385.     }//end if/else
  386.     return this;
  387. }
  388. //end of linkedListType.cpp
  389.  
  390. //unorderedLinkedList.h
  391. #ifndef UNORDERED_LINKED_LIST_H
  392. #define UNORDERED_LINKED_LIST_H
  393.  
  394. #include <iostream>
  395. #include "node.h"
  396. #include "linkedListType.h"
  397. #include "linkedListIterator.h"
  398.  
  399. using namespace std;
  400.  
  401. template <class Type>
  402. class unorderedLinkedList: public linkedListType<Type>
  403. {
  404. public:
  405.     bool search(const Type &searchItem) const;
  406.     //function to determine whether searchItem is in the list.
  407.     //postCondition: returns true if searchItem is in the list,
  408.     //               otherwise the value false is returned
  409.    
  410.     void insertFirst(const Type &newItem);
  411.     //function to insert newItem at the beginning of the list.
  412.     //postCondition: first point to the new list, newItem is
  413.     //               inserted at the beginning of the list,
  414.     //               last points to last node in the list,
  415.     //               and count is incremented by 1.
  416.    
  417.     void insertLast(const Type &newItem);
  418.     //function to insert newItem at the end of the list.
  419.     //postCondition: first points to the new list,
  420.     //               newItem is inserted at the end of the list,
  421.     //               last points to the last node in the list,
  422.     //               and count is incremented by 1.
  423.    
  424.     void deleteNode(const Type &deleteItem);
  425.     //function to delete deleteItem from the list.
  426.     //postCondition: if found, the node containing
  427.     //               deleteItem is deleted from the list.
  428.     //               first points to the first node,
  429.     //               last points to the last node of the updated
  430.     //               lis, and count is decremented by 1.
  431.    
  432. };
  433. #endif
  434. //end of unorderedLinkedList.h
  435.  
  436. //unorderedLinkedList.cpp
  437. #include <iostream>
  438. #include "node.h"
  439. #include "linkedListIterator.h"
  440. #include "linkedListType.h"
  441. #include "unorderedLinkedList.h"
  442.  
  443. using namespace std;
  444.  
  445. template <class Type>
  446. bool unorderedLinkedList<Type>::
  447.                     search(const Type &searchItem) const
  448. {
  449.     nodeType<Type> *current; //pointer to traverse the list
  450.     bool found = false;
  451.     current = first; //set current to point to the first
  452.                     //node in the list
  453.     while (current != NULL && !found) //search the list
  454.         if(current -> info == searchItem) //search item is found
  455.             found = true;
  456.         else
  457.             current = current -> link; //make current point to the next node
  458.         return found;
  459. }//end search
  460.  
  461. template <class Type>
  462. void unorderedLinkedList<Type>::insertFirst(const Type &newItem)
  463. {
  464.     nodeType<Type> *newNode; //pointer to create the new node
  465.     newNode = new nodeType<Type>; //creat the new node
  466.     newNode -> info = newItem; //store the new item in the node
  467.     newNode -> link = first; //insert newNode before first
  468.     first = newNode; //make first pointo the actual first node
  469.     count ++;       //increment count
  470.  
  471.     if (last == NULL) //if the list was empty, newNode is also the last node in the list
  472.         last = newNode;
  473. }//end insertFirst
  474.  
  475. template<class Type>
  476. void unorderedLinkedList<Type>::insertLast(const Type &newItem)
  477. {
  478.     nodeType<Type> *newNode; //pointer to create the new node
  479.  
  480.     newNode = new nodeType<Type>; //create the new node
  481.     newNode -> info = newItem; //store the new item in the node
  482.     newNode -> link = NULL; //set the link field of newNode to NULL
  483.  
  484.     if (first == NULL) //if the list is empty, newNode is both
  485.                       //both the first and last node
  486.     {
  487.         first = newNode;
  488.         last = newNode;
  489.         count ++; //increment count
  490.     }
  491.     else //the list is not empty, insert newNode after last
  492.     {
  493.         last -> link = newNode; //insert newNode after last
  494.         last = newNode; //make last point to the actual last node in the list
  495.         count ++; //increment count
  496.     }//end else
  497. }//end insertLast
  498.  
  499.  
  500. template <class Type>
  501. void unorderedLinkedList<Type>::deleteNode(const Type &deleteItem)
  502. {
  503.     nodeType<Type> *current; //pointer to traverse the list
  504.     nodeType<Type> *trailCurrent; //pointer just before current
  505.     bool found;
  506.  
  507.     if (first == NULL) //case 1; the list is empty
  508.         cout << "Cannot delete from an empty list." << endl;
  509.     else
  510.     {
  511.         if(first -> info == deleteItem) //case 2
  512.         {
  513.             current = first;
  514.             first = first -> link;
  515.             count --;
  516.  
  517.             if(first == NULL) //the list has only one node
  518.                last = NULL;
  519.  
  520.             delete current;
  521.         }
  522.         else //search the list for the node for node with the given info
  523.         {
  524.             found = false;
  525.             trailCurrent = first; //set trailCurrent to point
  526.                                  //to the first node
  527.             current = first ->link; //set current to point to
  528.                                     //the second node
  529.  
  530.             while (current != NULL && !found)
  531.             {
  532.                 if (current -> info != deleteItem)
  533.                 {
  534.                     trailCurrent = current;
  535.                     current = current -> link;
  536.                 }
  537.                 else
  538.                 found = true;
  539.             } //end while
  540.             if(found) //case3; if found, delete the node
  541.             {
  542.                 trailCurrent -> link = current -> link;
  543.                 count --;
  544.  
  545.                 if (last == current) //node to be deleted was last node
  546.                 last = trailCurrent; //update the value of last
  547.                 delete current; //delete the node from the list
  548.             }
  549.             else
  550.                 cout << "The item to be deleted is not in the list." << endl;
  551.             }//end else
  552.         }//end else
  553.     }//end deleteNode
  554. //end of unorderedLinkedList.cpp
  555.  
  556. //orderedLinkedList.h
  557. #ifndef ORDERED_LINKED_LIST_H
  558. #define ORDERED_LINKED_LIST_H
  559.  
  560. #include <iostream>
  561. #include "linkedListType.h"
  562. #include "node.h"
  563.  
  564. using namespace std;
  565.  
  566. template <class Type>
  567. class orderedLinkedList: public linkedListType<Type>
  568. {
  569. public:
  570.     bool search (const Type &searchItem) const;
  571.     //function to determine whether searchitem is in the list.
  572.     //postCondition: returns true if searchItem is in the list,
  573.     //otherwise returns the value false
  574.    
  575.     void insert(const Type &newItem);
  576.     //function to insert newItem in the list.
  577.     //postCondition: first points to the new list, newItem
  578.     //               is inserted at the proper place in the list,
  579.     //               and count is incremented by 1.
  580.    
  581.     void insertFirst(const Type &newItem);
  582.     //function to insert newItem at the beginning of the list.
  583.     //postCondition: first points to the new list,
  584.     //               newItem is inserted at the proper place in the list,
  585.     //               last points to the last node in the list,
  586.     //               and count is incremented by 1.
  587.    
  588.     void insertLast (const Type &newItem);
  589.     //function to insert newItem at the end of the list.
  590.     //postCondition: first points to the new list,
  591.     //               newItemis inserted at the proper place in the list,
  592.     //               last points to the last node in the list,
  593.     //               and count is incremented by 1.
  594.    
  595.     void deleteNode(const Type &deleteItem);
  596.     //function to delete deleteItem from the list.
  597.     //postCondition: if found, the node containing deleteItem
  598.     //               is deleted from the list,
  599.     //               first points to the first node of the new list,
  600.     //               and count is decremented by 1.
  601.     //               if deleteItem is not in the list,
  602.     //               an appropriate message is printed.
  603.    
  604. };
  605.  
  606. #endif
  607. //end of orderedLinkedList.h
  608.  
  609. //orderedLinkedList.cpp
  610. #include <iostream>
  611. #include "node.h"
  612. #include "linkedListType.h"
  613. #include "orderedLinkedList.h"
  614.  
  615. using namespace std;
  616.  
  617. template <class Type>
  618. bool orderedLinkedList<Type>::
  619.                       search(const Type &searchItem) const
  620. {
  621.     bool found = false;
  622.     nodeType<Type> *current; //pointer to traverse the list
  623.  
  624.     current = first; //start the search at the first node
  625.  
  626.     while (current != NULL && !found)
  627.         if (current -> info >= searchItem)
  628.             found = true;
  629.         else
  630.             current = current -> link;
  631.  
  632.         if(found)
  633.             found = (current -> info == searchItem); //test for equality
  634.    
  635.     return found;
  636. }//end search
  637.  
  638. template<class Type>
  639. void orderedLinkedList<Type>::insert(const Type &newItem)
  640. {
  641.     nodeType<Type> *current; //pointer to traverse the list
  642.     nodeType<Type> *trailCurrent; //pointer just before current
  643.     nodeType<Type> *newNode; //pointer to create a new node
  644.     bool found;
  645.    
  646.     newNode = new nodeType<Type>; //create the node
  647.     newNode -> info = newItem; //store newItem in the node
  648.     newNode -> link = NULL; //set the link field of the node to NULL
  649.  
  650.     if (first == NULL) //case 1
  651.     {
  652.         first = newNode;
  653.         last = newNode;
  654.         count ++;
  655.     }
  656.     else
  657.     {
  658.         current = first;
  659.         found = false;
  660.  
  661.         while (current != NULL && !found) //search the list
  662.             if(current -> info >= newItem)
  663.                 found = true;
  664.             else
  665.             {
  666.                 trailCurrent = current;
  667.                 current = current -> link;
  668.             }
  669.         if (current == first) //case 2
  670.         {
  671.             newNode -> link = first;
  672.             first = newNode;
  673.             count ++;
  674.         }
  675.         else                //case 3
  676.         {
  677.             trailCurrent -> link = newNode;
  678.             newNode -> link = current;
  679.  
  680.             if(curent == NULL)
  681.                 last = newNode;
  682.  
  683.             count ++;
  684.         }
  685.     }
  686. }
  687.  
  688. template <class Type>
  689. void orderedLinkedList<Type>::insertFirst(const Type &newItem)
  690. {
  691.     insert(newItem);
  692. }
  693.  
  694. template <class Type>
  695. void orderedLinkedList<Type>::insertLast(const Type &newItem)
  696. {
  697.     insert(newItem);
  698. }//end insertLasttemplate <class Type>
  699.  
  700. template <class Type>
  701. void orderedLinkedList<Type>::deleteNode(const Type &deleteItem)
  702. {
  703.     nodeType<Type> *current; //pointer to traverse the list
  704.     nodeType<Type> *trailCurrent; //pointer just before curent
  705.     bool found;
  706.  
  707.     if(first == NULL) //case 1
  708.         cout << "Cannot delete from an empty list." << endl;
  709.     else
  710.     {
  711.         current = first;
  712.         found = false;
  713.  
  714.         while (current != NULL && !found) //search the list
  715.             if (current -> info >= deleteItem)
  716.                 found = true;
  717.             else
  718.             {
  719.                 trailCurrent = current;
  720.                 current = current ->link;
  721.             }
  722.         if(current == NULL) //case 4
  723.             cout << "The item to be deleted is not in the list." << endl;
  724.         else
  725.             if(current -> info == deleteItem)//the item to be delete is in the list
  726.             {
  727.                 if(first == current) //case 2
  728.                 {
  729.                     first = first -> link;
  730.  
  731.                     if (first == NULL)
  732.                         last = NULL;
  733.  
  734.                         delete current;
  735.                 }
  736.                 count --;
  737.             }
  738.             else
  739.                 cout << "The item to be deleted is not in the list." << endl;
  740.         }
  741. }//end deleteNode
  742.    
  743. //end of orderedLinkedList.cpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement