NODE *head;

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;}
int value;
while(!is_empty()) pop(value);
}//end of destructor.

};//end of STACK.

int main(void) {
int value;

for(int i=0; i<3; i++){
cout<<"Popped: "<<value<<endl;
}//end of for.

}//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.

Why are you taking pain by handling memory yourself. Use power of STL.

#include <vector>
#include <stdexcept>

template <typename T>
class Stack
    std::vector<T> elems;     // elements

    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

it is nice code regarding dyanamic implementation of the stack,full explanatory,simple easy to code

Why are you taking pain by handling memory yourself. Use power of STL.

Its ironic, because C++ has a stack implementation already. So why even do what you did above?

This article has been dead for over six months. Start a new discussion instead.