Advertisement
erick016

hello.cpp

Oct 10th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #ifndef hello_CPP
  2. #define hello_CPP
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. #include "hello.h"
  7.  
  8. hello::hello()
  9. {
  10. Front = NULL;
  11. Rear = NULL;
  12. Count = 0;
  13. }
  14.  
  15. hello::~hello()
  16. {
  17. }
  18.  
  19. bool hello::isEmpty()
  20. {
  21. if (Front==NULL&&Rear==NULL&&Count==0)return true;
  22. else return false;
  23. }
  24.  
  25.  
  26. void hello::displayAll()
  27. {
  28. cout << endl;
  29. if (isEmpty()) cout << "[ empty ]" << endl;
  30.  
  31. else
  32. {
  33. //cout << Front->Elem << endl;
  34. Node *p;
  35. p = Front;
  36. while(p!=NULL)
  37. {
  38. cout << p->Elem << endl;
  39. p = p->Next;
  40. }
  41. }
  42. }
  43.  
  44. void hello::addRear(el_t NewNum)
  45. {
  46. if (isEmpty())
  47. {
  48. Node *First;
  49. First = new Node;
  50. Front = First;
  51. Rear = First;
  52. Count++;//Count line
  53.  
  54. First->Elem = NewNum;
  55. }
  56. else
  57. {
  58. Rear->Next = new Node;
  59. Rear = Rear->Next;
  60. Rear->Elem = NewNum;
  61. Rear->Next = NULL;
  62. Count++;//Count line
  63.  
  64. }
  65. }
  66.  
  67. void hello::deleteFront(el_t& OldNum)
  68. {if (isEmpty()){throw Underflow();}
  69. else
  70. {
  71. OldNum = Front->Elem;
  72. Node *Second;
  73. Second = Front->Next;
  74. delete Front;
  75. Front = Second;
  76. if(Count==1)
  77. {
  78. Front = NULL;
  79. Rear = NULL;
  80. }
  81. Count--;//Count line
  82.  
  83. }
  84. }
  85.  
  86. void hello::addFront(el_t NewNum)
  87. {
  88. if (isEmpty())
  89. {
  90. addRear(NewNum);//References rear
  91. }
  92. else
  93. {
  94. Node *p;
  95. p = new Node;
  96. p->Next = Front;
  97. Front = p;
  98. Front->Elem = NewNum;
  99. Count++;//Count line
  100.  
  101. }
  102. }
  103.  
  104. void hello::deleteRear(el_t& OldNum)
  105. {if (isEmpty()){throw Underflow();}
  106. if (Count == 1) deleteFront(OldNum);
  107. else
  108. {
  109. OldNum = Rear->Elem;
  110. Rear = p;
  111. Rear->Next = NULL;
  112. Count--;//Count line
  113. }
  114. }
  115.  
  116. void hello::deleteIth(int I, el_t& OldNum)
  117. {
  118. int i;
  119. if (I<1||I>Count){throw OutOfRange();}
  120. else
  121. {
  122. if (I == 1) deleteFront(OldNum);
  123.  
  124. else
  125. {
  126. Node *p;
  127. Node *q;
  128. p = Front;
  129. q = Front;
  130. while (i<I)
  131. {
  132. p = p->Next;
  133. i++;
  134. }
  135. i = 0;
  136. while (i<(I-1))
  137. {
  138. q = q->Next;
  139. i++;
  140. }
  141. OldNum = p->Elem;
  142. //p->Elem = NULL;
  143. delete p;
  144. Rear = q;
  145. Rear -> Next = NULL;
  146. Count--;//Count line
  147. }
  148. }
  149. }
  150.  
  151.  
  152. void hello::addbeforeIth(int I, el_t newNum)
  153. {
  154. int i;
  155. i = 0;
  156.  
  157. if (!(I<1||I>Count+1))
  158. {
  159. if (isEmpty()||I==Count+1)
  160. {
  161. addRear(newNum);//References rear
  162. return;
  163. }
  164.  
  165. if (I == 1)
  166. {
  167. addFront(newNum);
  168. return;
  169. }
  170.  
  171. else
  172. {
  173. Node *p;
  174. Node *q;
  175. p = Front;
  176. q = Front;
  177.  
  178. while (i<I)
  179. {
  180. p = p->Next;
  181. i++;
  182. }
  183. i = 0;
  184. while (i<(I-1))
  185. {
  186. q = q->Next;
  187. i++;
  188. }
  189. Node *r;
  190. r = new Node;
  191. r->Elem = newNum;
  192. q->Next = r;
  193. r->Next = p;//qrp
  194. Count++;//Count line
  195. }
  196. }
  197. else{throw OutOfRange();}
  198.  
  199. }
  200.  
  201.  
  202. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement