Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #ifndef LINKLIST_H_
  2. #define LINKLIST_H_
  3. /////////////////////////////////////////////////////////////////////////////
  4.  
  5. #include <iostream>
  6. #include "Node.h"
  7. using namespace std;
  8.  
  9. template<class T>
  10. class LinkList {
  11. public:
  12.     LinkList();
  13.     ~LinkList();
  14.  
  15.     void addfront(T _data);  //Done O(1)
  16.     T showfront();           //Done O(1)
  17.     void erasefront();          //Done O(1)
  18.  
  19.     void addend(T _data);    //Done O(1)
  20.     T showend();             //Done O(1)
  21.     void eraseend();            //Done O(n)
  22.  
  23.     bool find(T _data);      //Done O(n)
  24.     void erase(T _data);     //Done O(n)
  25.     bool isempty();             //Done O(1)
  26.  
  27.     void print();               //Done O(n)
  28.     void clear();               //Done O(n)
  29.  
  30. private:
  31.     Node<T> *head, *tail;
  32.     int sz;
  33. };
  34.  
  35. template<class T>
  36. LinkList<T>::LinkList() {
  37.     head = tail = 0;
  38.     sz = 0;
  39. }
  40.  
  41. template<class T>
  42. LinkList<T>::~LinkList() {
  43.     Node<T> *tp;
  44.     while (head != 0) {
  45.         tp = head->next;
  46.         delete head;
  47.         head = tp;
  48.     }
  49. }
  50.  
  51. /////////////////////////////////////////////////////////
  52. template<class T>
  53. void LinkList<T>::addfront(T _data) {
  54.     Node<T> *tp = new Node<T>(_data);
  55.     if (head == 0)
  56.         head = tp;
  57.     else {
  58.         tp->next = head;
  59.         head = tp;
  60.     }
  61.     if (tail == 0)
  62.         tail = tp;
  63.     sz++;
  64. }
  65.  
  66. template<class T>
  67. T LinkList<T>::showfront() {
  68.     if (head == 0)
  69.         return 0;
  70.     return head->data;
  71. }
  72.  
  73. template<class T>
  74. void LinkList<T>::erasefront() {
  75.     Node<T> *tp = head;
  76.     if (head == tail)
  77.         head = tail = 0, sz = 0;
  78.     else
  79.         head = head->next, sz--;
  80.     delete tp;
  81. }
  82.  
  83. template<class T>
  84. void LinkList<T>::addend(T _data) {
  85.     Node<T> *tp = new Node<T>(_data);
  86.     if (tail == 0) {
  87.         head = tp;
  88.         tail = tp;
  89.     } else {
  90.         tail->next = tp;
  91.         tail = tp;
  92.     }
  93.     sz++;
  94. }
  95.  
  96. template<class T>
  97. T LinkList<T>::showend() {
  98.     if (tail == 0)
  99.         return -1;
  100.     return tail->data;
  101. }
  102.  
  103. template<class T>
  104. void LinkList<T>::eraseend() {
  105.     Node<T> *tp;
  106.     for (tp = head; tp->next != tail; tp = tp->next)
  107.         ;
  108.     delete tail;
  109.     tail = tp;
  110.     tail->next = 0;
  111.     sz == 0 ? sz = 0 : sz--;
  112. }
  113.  
  114. template<class T>
  115. bool LinkList<T>::find(T _data) {
  116.     Node<T> *tp;
  117.     for (tp = head; tp->next != 0; tp = tp->next) {
  118.         if (tp->data == _data)
  119.             return true;
  120.     }
  121.     return (tail->data == _data);
  122. }
  123.  
  124. template<class T>
  125. void LinkList<T>::erase(T _data) {
  126.     if (head == tail)
  127.         head = tail = 0;
  128.     else if (tail->data == _data)
  129.         eraseend();
  130.     else if (head->data == _data)
  131.         erasefront();
  132.     else {
  133.         Node<T> *tp;
  134.         int cnter1 = 0, cnter2 = 0;
  135.         for (tp = head; tp->next != 0; tp = tp->next) {
  136.             if (tp->data == _data)
  137.                 break;
  138.             cnter1++;
  139.         }
  140.         Node<T> *tp1;
  141.         for (tp1 = head; tp1->next != 0; tp1 = tp1->next) {
  142.             cnter2++;
  143.             if (cnter1 == cnter2)
  144.                 break;
  145.         }
  146.         tp1->next = tp->next;
  147.         delete tp;
  148.         sz--;
  149.     }
  150. }
  151.  
  152. template<class T>
  153. bool LinkList<T>::isempty() {
  154.     if (head == 0)
  155.         return true;
  156.     return false;
  157. }
  158.  
  159. template<class T>
  160. void LinkList<T>::print() {
  161.     if (head == 0)
  162.         return;
  163.     Node<T> *tp;
  164.     for (tp = head; tp->next != 0; tp = tp->next) {
  165.         cout << tp->data << " ";
  166.     }
  167.     if (sz > 1)
  168.         cout << tail->data << endl;
  169. }
  170.  
  171. template<class T>
  172. void LinkList<T>::clear() {
  173.     Node<T> *tp;
  174.     while (head != 0) {
  175.         tp = head->next;
  176.         delete head;
  177.         head = tp;
  178.     }
  179.     head = tail = 0;
  180. }
  181.  
  182.  
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185. #endif /* LINKLIST_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement