Guest User

Untitled

a guest
Jun 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. //Some forward declarations to help the compiler and for organization.
  7. template <class Type>
  8. struct nodeType;
  9.  
  10. template <class Type>
  11. class linkedListType;
  12.  
  13. template <class Type>
  14. class unorderedLinkedListType;
  15.  
  16. //The node struct
  17. template <class Type>
  18. struct nodeType  {
  19.     Type info;
  20.     nodeType *link;
  21. };
  22.  
  23.  
  24. //The linked list class
  25. template <class Type>
  26. class linkedListType {
  27. public:
  28.    
  29.     linkedListType();
  30.     ~linkedListType();
  31.  
  32.     virtual void deleteNode(const Type&) = 0;
  33.  
  34. protected:
  35.     nodeType<Type> *first;
  36.     nodeType<Type> *last;
  37.     int count;
  38. };
  39.  
  40. template <class Type>
  41. linkedListType<Type>::linkedListType() {
  42.     first = NULL;
  43.     last = NULL;
  44.     count = 0;
  45. }
  46.  
  47. template <class Type>
  48. linkedListType<Type>::~linkedListType() {
  49.     nodeType<Type> *temp;
  50.     while (first != NULL) {
  51.         temp = first;
  52.         first = first->link;
  53.         delete temp;
  54.     }
  55.     last = NULL;
  56.     count = 0;
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. //The unordered linked list class
  64. template <class Type>
  65. class unorderedLinkedList : public linkedListType<Type> {
  66. public:
  67.     void insertFirst(const Type& newItem);
  68.     void insertLast(const Type& newItem);
  69.     void deleteNode(const Type& deleteItem);
  70.  
  71. };
  72.  
  73.  
  74. template <class Type>
  75. void unorderedLinkedList<Type>::insertFirst(const Type& newItem) {
  76.     nodeType<Type> *newNode;
  77.     newNode = new nodeType<Type>;
  78.     newNode->info = newItem;
  79.     newNode->link = first;
  80.     first = newNode;
  81.     count++;
  82.     if (last == NULL) {
  83.         last = newNode;
  84.     }
  85. }
  86.  
  87. template <class Type>
  88. void unorderedLinkedList<Type>::insertLast(const Type& newItem) {
  89.     nodeType<Type> *newNode;
  90.     newNode = new nodeType<Type>;
  91.     newNode->info = newItem;
  92.     newNode->link = NULL;
  93.  
  94.     if (first == NULL) {
  95.         first = newNode;
  96.     } else {
  97.         last->link = newNode;
  98.     }
  99.     last = newNode;
  100.     count++;
  101. }
  102.  
  103. template <class Type>
  104. void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem) {
  105.     nodeType<Type> *current;
  106.     nodeType<Type> *trailCurrent;
  107.     bool found;
  108.  
  109.     if (first == NULL)
  110.         cout << "Can't delete from an empty list" << endl;
  111.     else {
  112.         if (first->info == deleteItem) {
  113.             current = first;
  114.             first = first->link;
  115.             count--;
  116.             if(first == NULL)
  117.                 last = NULL;
  118.             delete current;
  119.  
  120.         }
  121.         else {
  122.             found = false;
  123.             trailCurrent = first;
  124.             current = first->link;
  125.             while (current != NULL && !found) {
  126.                 if (current->info != deleteItem) {
  127.                     trailCurrent = current;
  128.                     current = current->link;
  129.                 }
  130.                 else {
  131.                     found = true;
  132.                 }
  133.             }
  134.             if (found) {
  135.                 trailCurrent->link = current->link;
  136.                 count--;
  137.                 if (last == current) {
  138.                     last = trailCurrent;
  139.                 }
  140.                 delete current;
  141.             } else {
  142.                 cout << "The item wasn't found" << endl;
  143.             }
  144.         }
  145.     }
  146. }
  147.  
  148.  
  149. template <class Type>
  150. class priorityQueue {
  151. public:
  152.     priorityQueue();
  153. protected:
  154.     unorderedLinkedList<Type> *u;
  155. };
  156.  
  157. template <class Type>
  158. priorityQueue<Type>::priorityQueue() {
  159.     u = new unorderedLinkedList<Type>();
  160. }
  161.  
  162. int main () {
  163.    
  164.     priorityQueue<string> *pQueue = new priorityQueue<string>();
  165.     pQueue->push_back("cat");
  166.     pQueue->push_back("dog");
  167.     pQueue->push_back("slug");
  168.     pQueue->push_back("spider");
  169.     pQueue->push_back("rock");
  170.     cout << pQueue->pop_front() << endl;  //Should display "cat"
  171.     cout << pQueue->pop_front() << endl;  //Should display "dog"
  172.     cout << pQueue->pop_front() << endl;  //Should display "slug"
  173.     pQueue->push_front("apple");
  174.     pQueue->push_front("orange");
  175.     pQueue->push_front("banana");
  176.     pQueue->push_front("lemon");
  177.     pQueue->push_front("lime");
  178.     pQueue->push_front("grape");
  179.     cout << pQueue->pop_back() << endl;  //Should display "rock"
  180.     cout << pQueue->pop_back() << endl;  //Should display "spider"
  181.     pQueue->push_back_with_priority("The president of the United States", 2);
  182.     pQueue->push_back_with_priority("The prime minister of the United Kingdom", 2);
  183.     pQueue->push_back_with_priority("An average WSU CS student", 1);
  184.     pQueue->push_back_with_priority("An average WSU marketing student", 0);
  185.     pQueue->push_back_with_priority("Brad Peterson", 3);
  186.     for (int i = 0; i < 11; i++) {
  187.         cout << pQueue->pop_first_highest_priority() << endl;
  188.     }
  189.     delete pQueue;
  190.     system("pause");
  191.     return 0;
  192. }
Add Comment
Please, Sign In to add comment