c++學了類 老師就讓寫了這個、、、
#include運行結果:#include using namespace std; class Stack { public: void push(int x); void init(); int pop(); struct stack { int num; stack *next ,*pre; }*head; }; //初始化棧頂 void Stack::init() { head=(struct stack *)malloc(sizeof(struct stack)); head->num=-1; head->next=NULL; head->pre=NULL; } //入棧 void Stack::push(int x) { stack *p; p=(struct stack *)malloc(sizeof(struct stack)); head->next=p; p->pre=head; head=p; head->num=x; } //出棧 int Stack::pop() { if(head->pre!=NULL) { int x=head->num; head=head->pre; head->next=NULL; return x; } else { return 0x3fffffff; } } int main() { Stack s; s.init(); while(true) { cout<<"向棧中添加元素請按1,取出棧頂元素請按2,退出請按3"< >x; if(x==1) { int y; cout<<"請輸入入棧的元素:"; cin>>y; s.push(y); cout<<"入棧成功!!"<