fork download
  1. /****************************************************
  2.  * Breon Thibodeaux
  3.  * 2/11/14
  4.  * Assignment 5.
  5.  *
  6.  * <a simple, short program/class description>
  7.  ****************************************************/
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. const int MAX_SIZE = 30;
  13. template <typename T>
  14. class Stack
  15. {
  16. private:
  17. struct Node
  18. {
  19. Node *head;
  20. Node *tail;
  21. Node *curr;
  22. int num_items;
  23. T data;
  24. Node *link;
  25. };
  26.  
  27. Node *top;
  28. Node *curr;
  29. int num_items;
  30.  
  31. public:
  32.  
  33. Stack()
  34. {
  35. top = NULL;
  36. curr = NULL;
  37. num_items = 0;
  38. }
  39.  
  40. Stack(const Stack& s)
  41. {
  42. *this = s;
  43. }
  44.  
  45.  
  46. void operator=(const Stack& s)
  47. {
  48. if (s.top == NULL)
  49. top = NULL;
  50. else
  51. {
  52. top = new Node;
  53. top->data = s.top->data;
  54. Node* newP = top;
  55. num_items = 0;
  56. for(Node* curr = s.top->link; curr != NULL; curr = curr->link)
  57.  
  58. {
  59. cout << curr->data << endl;
  60. if(num_items != MAX_SIZE)
  61. {
  62. newP->link = new Node;
  63. newP = newP->link;
  64. newP->data = curr->data;
  65. ++num_items;
  66. }
  67. }
  68. }
  69. }
  70.  
  71.  
  72. void Push(T item)
  73. {
  74. if(num_items != MAX_SIZE)
  75. {
  76. Node *p = new Node;
  77. p -> data = item;
  78. p->link = top;
  79. top = p;
  80. num_items++;
  81. }
  82. }
  83.  
  84. // navigates to the end of the list
  85. // the end of the list does not necessarily correspond to its maximum size; it's just at the last existing element
  86. T Pop()
  87. {
  88. Node* temp = top;
  89. if(IsEmpty())
  90. {
  91. return NULL;
  92. }
  93. top = temp->link;
  94. num_items--;
  95. return temp->data;
  96. }
  97.  
  98.  
  99. T Peek()
  100. {
  101. if(IsEmpty())
  102. {
  103. return -1;
  104. }
  105. return top->data;
  106. }
  107.  
  108. bool IsEmpty()
  109. {
  110. return top == NULL;
  111. }
  112.  
  113. bool IsFull()
  114. {
  115. return num_items == MAX_SIZE;
  116. }
  117. int Size()
  118. {
  119. return num_items;
  120. }
  121.  
  122.  
  123. Stack operator+(const Stack& s) const
  124. {
  125. // copy the first list
  126. Stack t = *this;
  127. Stack u = *this;
  128. Node *n = s.top;
  129.  
  130. // iterate through the second list and copy each element to the new list
  131. while (n != NULL && !t.IsFull())
  132. {
  133. t.Push(n->data);
  134. u.Push(n->data);
  135. n = n->link;
  136. }
  137.  
  138. return u;
  139. }
  140.  
  141. // returns if two lists are equal (by value)
  142. bool operator==(const Stack& s) const
  143. {
  144. // the lists are not equal if they're of different sizes
  145. if (num_items != s.num_items)
  146. return false;
  147.  
  148. Node *p = top;
  149. Node *q = s.top;
  150.  
  151. // iterate through each list
  152. while (p != NULL)
  153. {
  154. // if any pair of elements differ, the lists are not equal
  155. if (p->data != q->data)
  156. return false;
  157. p = p->link;
  158. q = q->link;
  159. }
  160.  
  161. return true;
  162. }
  163.  
  164. // returns if two lists are not equal (by value)
  165. bool operator!=(const Stack& s) const
  166. {
  167. return !(*this == s);
  168. }
  169.  
  170. // returns a string representation of the entire list (e.g., 1 2 3 4 5)
  171. // the string "NULL" should be returned for an empty list
  172. friend ostream& operator<<(ostream& out, const Stack &s)
  173. {
  174. // "NULL" if the list is empty
  175. if (s.top == NULL)
  176. out << "NULL";
  177. else
  178. {
  179. Node *n = s.top;
  180.  
  181. // otherwise iterate through the list and display each element separated by a space
  182. while (n != NULL)
  183. {
  184. out << n->data << " ";
  185. n = n->link;
  186. }
  187. }
  188.  
  189. return out;
  190. }
  191. };
  192.  
  193. /*****************************************
  194.  * YOU MAY NOT CHANGE ANYTHING BELOW THIS!
  195.  *****************************************/
  196. int main()
  197. {
  198. Stack<int> s1;
  199.  
  200. cout << "*declare stack s1\ns1=" << s1 << endl; // stack initially set to 0
  201. cout << "s1.Size()=" << s1.Size() << endl;
  202. cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
  203. cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
  204. cout << "s1.Peek()=" << s1.Peek() << endl;
  205. cout << endl;
  206.  
  207. for (int i=0; i<100; i++) // push 1000 times with i+100
  208. s1.Push(i+100);
  209. cout << "*Push(i+100) 1000 times\ns1=" << s1 << endl;
  210. cout << "s1.Size()=" << s1.Size() << endl;
  211. cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
  212. cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
  213. cout << "s1.Peek()=" << s1.Peek() << endl;
  214. cout << endl;
  215.  
  216. for (int i=0; i<10; i++) // pop 10 times
  217. s1.Pop();
  218. cout << "*pop 10 times\ns1=" << s1 << endl;
  219. cout << "s1.Size()=" << s1.Size() << endl;
  220. cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
  221. cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
  222. cout << "s1.Peek()=" << s1.Peek() << endl;
  223. cout << endl;
  224.  
  225. for (int i=0; i<1000; i++) // pop 1000 times
  226. s1.Pop();
  227. cout << "*pop 1000 times\ns1=" << s1 << endl;
  228. cout << "s1.Size()=" << s1.Size() << endl;
  229. cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
  230. cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
  231. cout << "s1.Peek()=" << s1.Peek() << endl;
  232. cout << endl;
  233.  
  234. for (int i=0; i<10; i++) // push 10 times with i*i
  235. s1.Push(i*i);
  236. cout << "*push 10 times with i*i\ns1=" << s1 << endl;
  237. cout << "s1.Size()=" << s1.Size() << endl;
  238. cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
  239. cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
  240. cout << "s1.Peek()=" << s1.Peek() << endl;
  241. cout << endl;
  242.  
  243. Stack<int> s2(s1); // s2 declared as a copy of s1
  244. cout << "*declare s2 as a copy of s1 (stack s2(s1))\ns2=" << s2 << endl;
  245. cout << "s2.Size()=" << s2.Size() << endl;
  246. cout << "s2.IsEmpty()=" << ((s2.IsEmpty()) ? "T" : "F") << endl;
  247. cout << "s2.IsFull()=" << ((s2.IsFull()) ? "T" : "F") << endl;
  248. cout << "s2.Peek()=" << s2.Peek() << endl;
  249. cout << endl;
  250.  
  251. Stack<int> s3;
  252. s3 = s3 + s2;
  253. cout << "*declare s3 as a copy of s2 (stack s3 = s2)\ns3=" << s3 << endl; // copy constructor (=)
  254. cout << "s3.Size()=" << s3.Size() << endl;
  255. cout << "s3.IsEmpty()=" << ((s3.IsEmpty()) ? "T" : "F") << endl;
  256. cout << "s3.IsFull()=" << ((s3.IsFull()) ? "T" : "F") << endl;
  257. cout << "s3.Peek()=" << s3.Peek() << endl;
  258. cout << endl;
  259.  
  260. s2 = Stack<int>();
  261. cout << "*reset s2 to 0\ns2=" << s2 << endl; // reset stack to 0
  262. cout << "s2.Size()=" << s2.Size() << endl;
  263. cout << "s2.IsEmpty()=" << ((s2.IsEmpty()) ? "T" : "F") << endl;
  264. cout << "s2.IsFull()=" << ((s2.IsFull()) ? "T" : "F") << endl;
  265. cout << "s2.Peek()=" << s2.Peek() << endl;
  266. cout << endl;
  267.  
  268. cout << "s1=" << s1 << "\ns2=" << s2 << "\ns3=" << s3 << endl;
  269. cout << "s1 == s2; =" << ((s1 == s2) ? "T" : "F") << endl; // test ==
  270. cout << "s1 == s3; =" << ((s1 == s3) ? "T" : "F") << endl;
  271. cout << "s1 != s3; =" << ((s1 != s3) ? "T" : "F") << endl;
  272. cout << endl;
  273.  
  274. s1 = s1 + s2;
  275. cout << "*s1 = s1 + s2\ns1=" << s1 << endl; // test +
  276. cout << "s1.Size()=" << s1.Size() << endl;
  277. cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
  278. cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
  279. cout << "s1.Peek()=" << s1.Peek() << endl;
  280. cout << endl;
  281.  
  282. for (int i=0; i<5; i++)
  283. s1.Pop();
  284. cout << "*pop s1 5 times\n";
  285. cout << "s1=" << s1 << "\ns2=" << s2 << "\ns3=" << s3 << endl;
  286. cout << endl;
  287.  
  288. s1 = s1 + s3;
  289. cout << "*s1 = s1 + s3\ns1=" << s1 << endl;
  290. cout << "s1.Size()=" << s1.Size() << endl;
  291. cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
  292. cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
  293. cout << "s1.Peek()=" << s1.Peek() << endl;
  294. cout << endl;
  295.  
  296. s1 = Stack<int>();
  297. s1 = s1 + s3;
  298. cout << "*set s1 to 0 then s1 = s1 + s3\ns1=" << s1 << endl;
  299. cout << endl;
  300. s1 = s1 + s3;
  301. cout << "s1 += s3;\ns1=" << s1 << endl; // test +=
  302. cout << "s1.Size()=" << s1.Size() << endl;
  303. cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
  304. cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
  305. cout << "s1.Peek()=" << s1.Peek() << endl;
  306. cout << endl;
  307.  
  308. cout << "s1=" << s1 << "\ns2=" << s2 << "\ns3=" << s3 << endl;
  309. cout << "s3.Peek()=" << s3.Peek() << endl;
  310. cout << endl;
  311. for (int i=8; i>0; i--)
  312. s3.Push(i);
  313. cout << "*push s3 8 times with i (backwards)\ns3=" << s3 << endl;
  314. cout << "s3.Peek()=" << s3.Peek() << endl;
  315. cout << endl;
  316. s3 = s3 + s1;
  317. cout << "*s3 += s1\ns3=" << s3 << endl;
  318. cout << "s3.Size()=" << s3.Size() << endl;
  319. cout << "s3.IsEmpty()=" << ((s3.IsEmpty()) ? "T" : "F") << endl;
  320. cout << "s3.IsFull()=" << ((s3.IsFull()) ? "T" : "F") << endl;
  321. cout << "s3.Peek()=" << s3.Peek() << endl;
  322. cout << endl;
  323.  
  324. Stack<char> s4;
  325. for (char c='a'; c<='z'; c++)
  326. s4.Push(c);
  327. cout << "s4=" << s4 << endl;
  328. cout << "s4.Size()=" << s4.Size() << endl;
  329. cout << "s4.IsEmpty()=" << ((s4.IsEmpty()) ? "T" : "F") << endl;
  330. cout << "s4.IsFull()=" << ((s4.IsFull()) ? "T" : "F") << endl;
  331. cout << "s4.Peek()=" << s4.Peek() << endl;
  332.  
  333. return 0;
  334. }
  335.  
  336.  
Success #stdin #stdout 0s 3436KB
stdin
1
2
10
42
11
stdout
*declare stack s1
s1=NULL
s1.Size()=0
s1.IsEmpty()=T
s1.IsFull()=F
s1.Peek()=-1

*Push(i+100) 1000 times
s1=129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 
s1.Size()=30
s1.IsEmpty()=F
s1.IsFull()=T
s1.Peek()=129

*pop 10 times
s1=119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 
s1.Size()=20
s1.IsEmpty()=F
s1.IsFull()=F
s1.Peek()=119

*pop 1000 times
s1=NULL
s1.Size()=0
s1.IsEmpty()=T
s1.IsFull()=F
s1.Peek()=-1

*push 10 times with i*i
s1=81 64 49 36 25 16 9 4 1 0 
s1.Size()=10
s1.IsEmpty()=F
s1.IsFull()=F
s1.Peek()=81

64
49
36
25
16
9
4
1
0
*declare s2 as a copy of s1 (stack s2(s1))
s2=81 64 49 36 25 16 9 4 1 0 
s2.Size()=9
s2.IsEmpty()=F
s2.IsFull()=F
s2.Peek()=81

1
4
9
16
25
36
49
64
81
*declare s3 as a copy of s2 (stack s3 = s2)
s3=0 1 4 9 16 25 36 49 64 81 
s3.Size()=9
s3.IsEmpty()=F
s3.IsFull()=F
s3.Peek()=0

*reset s2 to 0
s2=NULL
s2.Size()=9
s2.IsEmpty()=T
s2.IsFull()=F
s2.Peek()=-1

s1=81 64 49 36 25 16 9 4 1 0 
s2=NULL
s3=0 1 4 9 16 25 36 49 64 81 
s1 == s2; =F
s1 == s3; =F
s1 != s3; =T

64
49
36
25
16
9
4
1
0
64
49
36
25
16
9
4
1
0
64
49
36
25
16
9
4
1
0
*s1 = s1 + s2
s1=81 64 49 36 25 16 9 4 1 0 
s1.Size()=9
s1.IsEmpty()=F
s1.IsFull()=F
s1.Peek()=81

*pop s1 5 times
s1=16 9 4 1 0 
s2=NULL
s3=0 1 4 9 16 25 36 49 64 81 

9
4
1
0
9
4
1
0
64
49
36
25
16
9
4
1
0
16
9
4
1
0
*s1 = s1 + s3
s1=81 64 49 36 25 16 9 4 1 0 16 9 4 1 0 
s1.Size()=14
s1.IsEmpty()=F
s1.IsFull()=F
s1.Peek()=81

64
49
36
25
16
9
4
1
0
*set s1 to 0 then s1 = s1 + s3
s1=81 64 49 36 25 16 9 4 1 0 

64
49
36
25
16
9
4
1
0
64
49
36
25
16
9
4
1
0
64
49
36
25
16
9
4
1
0
81
64
49
36
25
16
9
4
1
0
s1 += s3;
s1=81 64 49 36 25 16 9 4 1 0 81 64 49 36 25 16 9 4 1 0 
s1.Size()=19
s1.IsEmpty()=F
s1.IsFull()=F
s1.Peek()=81

s1=81 64 49 36 25 16 9 4 1 0 81 64 49 36 25 16 9 4 1 0 
s2=NULL
s3=0 1 4 9 16 25 36 49 64 81 
s3.Peek()=0

*push s3 8 times with i (backwards)
s3=1 2 3 4 5 6 7 8 0 1 4 9 16 25 36 49 64 81 
s3.Peek()=1

2
3
4
5
6
7
8
0
1
4
9
16
25
36
49
64
81
2
3
4
5
6
7
8
0
1
4
9
16
25
36
49
64
81
64
81
0
1
4
9
16
25
36
49
64
81
1
2
3
4
5
6
7
8
0
1
4
9
16
25
36
49
64
81
*s3 += s1
s3=49 64 81 0 1 4 9 16 25 36 49 64 81 1 2 3 4 5 6 7 8 0 1 4 9 16 25 36 49 64 81 
s3.Size()=30
s3.IsEmpty()=F
s3.IsFull()=T
s3.Peek()=49

s4=z y x w v u t s r q p o n m l k j i h g f e d c b a 
s4.Size()=26
s4.IsEmpty()=F
s4.IsFull()=F
s4.Peek()=z