#include <cstdlib> #include <iostream> using namespace std; typedef struct _NODE { int value; struct _NODE *next; _NODE(int value) : value(value), next(NULL){}; }NODE, *PTRNODE; void push(PTRNODE &top, int val) { if(top == NULL) top = new NODE(val); else { PTRNODE newNode = new NODE(val); newNode->next = top; top = newNode; } } int pop(PTRNODE &top) { if(top == NULL) return -1; int res = 0; PTRNODE temp = top; res = temp->value; top = top->next; return res; } void display(PTRNODE top) { if(top == NULL) { cout << "empty" << endl; return; } PTRNODE temp = top; while(temp != NULL) { cout << temp->value << "->"; temp = temp->next; } cout << endl; } void clear(PTRNODE &top) { if(top == NULL) return; PTRNODE curNode = top; while(curNode != NULL) { PTRNODE temp = curNode; curNode = curNode->next; delete temp; } top == NULL; } int main(int argc, char *argv[]) { PTRNODE top = NULL; for(int i=0; i<10; i++) push(top, i); cout << "init stack: " << endl; display(top); int newValue; cout << "please input a new value: "; cin >> newValue; push(top, newValue); cout << "after push: " << endl; display(top); cout << "pop stack: " << endl; cout << "pop value is: " << pop(top) << endl; display(top); clear(top); system("PAUSE"); return EXIT_SUCCESS; }