| | |
Dynamic stack in c++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
NODE *head; public: void push(int value){ NODE *temp = (NODE*) new NODE; temp->value = value; temp->link = head; head = temp; }//end of push. void pop(int&value) { NODE *temp = head; value = head->value; head = head->link; delete temp; }//end of pop. int is_empty(void){ return head == NULL; }//end of is_empty. STACK (void) {head = NULL;} ~STACK(){ int value; while(!is_empty()) pop(value); }//end of destructor. };//end of STACK. int main(void) { STACK S; int value; S.push(5); S.push(10); S.push(15); for(int i=0; i<3; i++){ S.pop(value); cout<<"Popped: "<<value<<endl; }//end of for. return(0); }//end of main.
Can anyone please tell me briefly what this program actually does and how it works?
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
Why are you taking pain by handling memory yourself. Use power of STL.
C++ Syntax (Toggle Plain Text)
#include <vector> #include <stdexcept> template <typename T> class Stack { private: std::vector<T> elems; // elements public: void push(T const&); // push element void pop(); // pop element T top() const; // return top element bool empty() const // return whether the stack is empty { return elems.empty(); } }; template <typename T> void Stack<T>::push (T const& elem) { elems.push_back(elem); // append copy of passed elem } template<typename T> void Stack<T>::pop () { if (elems.empty()) { throw std::out_of_range("Stack<>::pop(): empty stack"); } elems.pop_back(); // remove last element } template <typename T> T Stack<T>::top () const { if (elems.empty()) { throw std::out_of_range("Stack<>::top(): empty stack"); } return elems.back(); // return copy of last element }
![]() |
Similar Threads
- deleting dynamic memory (C++)
- Malloc/Calloc Dynamic Memory Allocation. (C++)
- Stack implementation using function pointers in C (C)
- Stack conversion (C++)
- C++ Basics: Object Instantiation vs. Dynamic Memory Allocation (C++)
- Why use Dynamic and Static Memory Allocation... (C)
- Sorting Not In Order (C)
- TCP/IP stack whacked by malware; no DNS resolution (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Stacks, Queues, and a Maze??
- Next Thread: Help Please New to C++
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






