943,727 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7599
  • C++ RSS
Feb 22nd, 2008
0

Dynamic stack in c++

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. NODE *head;
  2. public:
  3.  
  4. void push(int value){
  5. NODE *temp = (NODE*) new NODE;
  6. temp->value = value;
  7. temp->link = head;
  8. head = temp;
  9. }//end of push.
  10.  
  11. void pop(int&value) {
  12. NODE *temp = head;
  13. value = head->value;
  14. head = head->link;
  15. delete temp;
  16. }//end of pop.
  17.  
  18. int is_empty(void){
  19. return head == NULL;
  20. }//end of is_empty.
  21.  
  22. STACK (void) {head = NULL;}
  23. ~STACK(){
  24. int value;
  25. while(!is_empty()) pop(value);
  26. }//end of destructor.
  27.  
  28. };//end of STACK.
  29.  
  30. int main(void) {
  31. STACK S;
  32. int value;
  33. S.push(5);
  34. S.push(10);
  35. S.push(15);
  36.  
  37. for(int i=0; i<3; i++){
  38. S.pop(value);
  39. cout<<"Popped: "<<value<<endl;
  40. }//end of for.
  41.  
  42. return(0);
  43. }//end of main.

Can anyone please tell me briefly what this program actually does and how it works?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cessna172 is offline Offline
2 posts
since Dec 2007
Feb 22nd, 2008
0

Re: Dynamic stack in c++

Have you ever heard of "debugging with printf"? A similar thing can be done with cout as well. You pepper this code with statements that display for you any items of interest while the program is running. It's a good way to see for yourself what is going on in code.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 22nd, 2008
0

Re: Dynamic stack in c++

Why are you taking pain by handling memory yourself. Use power of STL.
C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2. #include <stdexcept>
  3.  
  4. template <typename T>
  5. class Stack
  6. {
  7. private:
  8. std::vector<T> elems; // elements
  9.  
  10. public:
  11. void push(T const&); // push element
  12. void pop(); // pop element
  13. T top() const; // return top element
  14. bool empty() const // return whether the stack is empty
  15. {
  16. return elems.empty();
  17. }
  18. };
  19. template <typename T>
  20. void Stack<T>::push (T const& elem)
  21. {
  22. elems.push_back(elem); // append copy of passed elem
  23. }
  24.  
  25. template<typename T>
  26. void Stack<T>::pop ()
  27. {
  28. if (elems.empty())
  29. {
  30. throw std::out_of_range("Stack<>::pop(): empty stack");
  31. }
  32. elems.pop_back(); // remove last element
  33. }
  34.  
  35. template <typename T>
  36. T Stack<T>::top () const
  37. {
  38. if (elems.empty())
  39. {
  40. throw std::out_of_range("Stack<>::top(): empty stack");
  41. }
  42. return elems.back(); // return copy of last element
  43. }
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Stacks, Queues, and a Maze??
Next Thread in C++ Forum Timeline: Help Please New to C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC