Dynamic Stack Implementation - Templated Class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 20
Reputation: bleonard989 is an unknown quantity at this point 
Solved Threads: 1
bleonard989 bleonard989 is offline Offline
Newbie Poster

Dynamic Stack Implementation - Templated Class

 
0
  #1
Feb 26th, 2008
I am pretty new to c++, and have never worked with stack or templated classes before. I have started this program, but I am not done, I was wondering if I am at least on the right track, because I get a ton of compile errors.

The assignment:
Implement the stack data structure as a templated class using a dynamically allocated array. The top of the stack should be stored in a private data member that will either contain the subscript of the node currently on the top of the stack or "-1" if the stack is empty. You should fully test all methods implemented for your stack class using a menu driven test-driver program.

Any help and/or information you can give will be greatly appreciated... Thanks

Stack.h file - this code was given by my professor
  1. #ifndef STACK_H
  2. #define STACK_H
  3.  
  4. const int MAX_SIZE = 10;
  5.  
  6. class FullStack
  7. {};
  8.  
  9. class EmptyStack
  10. {};
  11.  
  12. template <class ItemType>
  13.  
  14. class StackType
  15. {
  16. public:
  17. StackType (int size = MAX_SIZE);
  18. ~StackType();
  19. StackType (const StackType &right);
  20. void Clear ();
  21. bool IsFull () const;
  22. bool IsEmpty () const;
  23. void Push (ItemType item);
  24. ItemType Pop ();
  25. ItemType Top ();
  26. void Print ();
  27. StackType& operator = (const StackType &right);
  28.  
  29. private:
  30. ItemType *items;
  31. int stackSize;
  32. int top;
  33. };
  34. #endif

Stack.cpp - What I have done so far...
  1. #include "Stack.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template<class ItemType>
  6. StackType<ItemType>::StackType(int size)
  7. {
  8. top = -1;
  9. size = MAX_SIZE;
  10. stackSize = size;
  11. items = new int [stackSize];
  12. }
  13.  
  14. template<class ItemType>
  15. StackType<ItemType>::~StackType ()
  16. {
  17. delete [] items;
  18. }
  19.  
  20. template<class ItemType>
  21. StackType& StackType<ItemType>::operator = (const StackType &right)
  22. {
  23. if (&right != this)
  24. {
  25. if (stackSize != right.stackSize)
  26. {
  27. delete [] items;
  28. stackSize = right.stackSize;
  29. items = new ItemType[stackSize];
  30. }
  31. top = right.top;
  32.  
  33. for (int x = 0; x < stackSize; x++)
  34. {
  35. items[x] = right.items[x];
  36. }
  37. }
  38. return *this;
  39. }
  40.  
  41. template<class ItemType>
  42. StackType::StackType<ItemType>(const StackType &right)
  43. {
  44. stackSize = right.stacksize;
  45. top = right.top;
  46. items = new int [stackSize];
  47.  
  48. for (int x = 0; x < stackSize; x++)
  49. {
  50. items[x] = right.items[x];
  51. }
  52. }
  53.  
  54. template<class ItemType>
  55. void StackType<ItemType>::Clear()
  56. {
  57. top = -1;
  58. }
  59.  
  60. template<class ItemType>
  61. bool StackType<ItemType>::IsEmpty() const
  62. {
  63. return (top == -1);
  64. }
  65.  
  66. template<class ItemType>
  67. bool StackType<ItemType>::IsFull() const
  68. {
  69. return (top == stackSize-1);
  70. }
  71.  
  72. template<class ItemType>
  73. void StackType<ItemType>::Push(item)
  74. {
  75. if (IsFull())
  76. {
  77. throw FullStack();
  78. }
  79. else
  80. {
  81. top++;
  82. items[top] = item;
  83. }
  84. }
  85.  
  86. template<class ItemType>
  87. ItemType StackType<ItemType>::Pop()
  88. {
  89. if (IsEmpty())
  90. {
  91. throw EmptyStack();
  92. }
  93. else
  94. {
  95. top--;
  96. }
  97. }
  98.  
  99. template<class ItemType>
  100. ItemType StackType<ItemType>:: Top() const
  101. {
  102. if (IsEmpty())
  103. {
  104. throw EmptyStack();
  105. }
  106. return items[top];
  107. }
  108.  
  109. template<class ItemType>
  110. void StackType<ItemType>::Print()
  111. {
  112. int count;
  113. count = stackSize;
  114.  
  115. if (StackSize >= -1)
  116. {
  117. cout << "This stack contains: ";
  118. while (count >= -1)
  119. {
  120. cout << items[count]<< " ";
  121. }
  122. cout << endl;
  123. }
  124. else
  125. {
  126. throw EmptyStack();
  127. }
  128. }

** -Note ** I had no clue about the Copy constructor function StackType (const StackType& right) So I just copied what was in our book just for a filler

Client Code- where most of the compile errors are from
  1. #include <iostream>
  2. #include "Stack.h"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. StackType stack;
  8.  
  9. float item;
  10. int num;
  11.  
  12.  
  13. cout << "How many items would you like in this stack (1 - 10): ";
  14. cin >> size;
  15.  
  16. if (size > 0 && size <= 10)
  17. size = size;
  18. else
  19. {
  20. cout << endl << "Not a valid number, using default of 10";
  21. size = MAX_SIZE;
  22. }
  23.  
  24. while (num != 0)
  25. {
  26. cout << "Please select from the following options:" << endl;
  27. cout << '\t' << "1 - Push an item" << endl;
  28. cout << '\t' << "2 - Pop an item" << endl;
  29. cout << '\t' << "3 - Get the top item" << endl;
  30. cout << '\t' << "4 - Determine if stack is full" << endl;
  31. cout << '\t' << "5 - Determine if stack is empty" << endl;
  32. cout << '\t' << "6 - Clear all items from stack" << endl;
  33. cout << '\t' << "7 - Print the stack" << endl;
  34. cout << '\t' << "8 - Copy stack" << endl;
  35. cout << '\t' << "9 - Assign one stack to another" << endl;
  36. cout << '\t' << "0 - Quit program" << endl;
  37. cout << endl;
  38. cout << '\t' << "Selection: ";
  39. cin >> num;
  40. cout << endl;
  41.  
  42. if (num == 1)
  43. {
  44. cout << "Enter an integer to push into the stack: ";
  45. cin >> item;
  46. try
  47. {
  48. stack.Push (item);
  49. cout << endl << item << " successfully pushed" << endl;
  50. }
  51. catch (FullStack exceptionObject)
  52. {
  53. cerr << endl << "Stack is full - unable to push item onto stack" << endl;
  54. }
  55. }
  56.  
  57. else if (num == 2)
  58. {
  59. try
  60. {
  61. stack.Pop ();
  62. cout << endl << top << " successfully popped" << endl;
  63. }
  64. catch (EmptyStack exceptionObject)
  65. {
  66. cerr << endl << "Stack is empty - unable to pop an item" << endl;
  67. }
  68. }
  69.  
  70. else if (num == 3)
  71. {
  72. try stack.Top ();
  73. {
  74. cout << endl << items[top] << " is currently on top of the stack" << endl;
  75. }
  76. catch (EmptyStack exceptionObject)
  77. {
  78. cout << endl;
  79. cerr << "Cannot retrieve integer from top of list - list is currently empty" << endl;
  80. }
  81. }
  82.  
  83. else if (num == 4)
  84. {
  85. if (stack.IsFull() == true)
  86. {
  87. cout << endl << "The stack is currently full" << endl;
  88. }
  89. else
  90. {
  91. cout << endl << "The stack is NOT currently full" << endl;
  92. }
  93. }
  94.  
  95. else if (num == 5)
  96. {
  97. if (stack.IsEmpty () == true)
  98. {
  99. cout << endl << "The stack is currently empty" << endl;
  100. }
  101. else
  102. {
  103. cout << endl << "The stack is NOT currently empty" << endl;
  104. }
  105. }
  106.  
  107. else if (num == 6)
  108. {
  109. stack.Clear();
  110. cout << endl << "The stack was successfully cleared" << endl;
  111. }
  112.  
  113. else if (num == 7)
  114. {
  115. try
  116. {
  117. cout << endl:
  118. stack.Print ();
  119. cout << endl;
  120. }
  121. catch (EmptyStack exceptionObject)
  122. {
  123. cerr << endl << "There are no items in this stack" << endl;
  124. }
  125. }
  126.  
  127. else if (num == 8)
  128. {
  129. StackType (const StackType &right);
  130. int y = x;
  131.  
  132. cout << endl << "Old Stack: ";
  133. while (x >= -1)
  134. {
  135. cout << items[x] << " ";
  136. x--;
  137. }
  138. cout << endl << "New Stack: ";
  139. while (y >= -1)
  140. {
  141. cout << right.items[y] << " ";
  142. y--;
  143. }
  144. cout << endl;
  145. }
  146.  
  147. else if (num == 9)
  148. {
  149. StackType operator = (const StackType &right)
  150.  
  151. int y = x;
  152.  
  153. cout << endl << "Old Stack: ";
  154. while (x >= -1)
  155. {
  156. cout << items[x] << " ";
  157. x--;
  158. }
  159. cout << endl << "New Stack: ";
  160. while (y >= -1)
  161. {
  162. cout << right.items[y] << " ";
  163. y--;
  164. }
  165. cout << endl;
  166. }
  167.  
  168. else if (num < 0 || num > 9)
  169. {
  170. cout << endl << "Invalid input, please try again" << endl;
  171. }
  172. }
  173. return 0;
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Dynamic Stack Implementation - Templated Class

 
0
  #2
Feb 26th, 2008
It is faster/easier to locate the error spots in the code if you post the compiler errors too.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC