Dynamic stack in c++

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

Join Date: Dec 2007
Posts: 2
Reputation: cessna172 is an unknown quantity at this point 
Solved Threads: 0
cessna172 cessna172 is offline Offline
Newbie Poster

Dynamic stack in c++

 
0
  #1
Feb 22nd, 2008
  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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Dynamic stack in c++

 
0
  #2
Feb 22nd, 2008
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Dynamic stack in c++

 
0
  #3
Feb 22nd, 2008
Why are you taking pain by handling memory yourself. Use power of STL.
  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. }
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC