链栈,写起来还是不熟
1 #include <iostream> 2 #include <cstdlib> 3 using namespace std; 4 typedef struct node 5 { 6 int data; 7 struct node* next; 8 }stacknode,*linkstack; 9 void initstack(linkstack *s) 10 { 11 *s=new stacknode; 12 (*s)->next=NULL; 13 } 14 bool stackempty(linkstack s) 15 { 16 if(s->next==NULL) 17 return 1; 18 return 0; 19 } 20 void pushstack(linkstack s,int e) 21 { 22 linkstack p; 23 p=new stacknode; 24 p->data=e; 25 p->next=s->next; 26 s->next=p; 27 } 28 void popstack(linkstack s) 29 { 30 linkstack p; 31 p=s->next; 32 if(stackempty(s)) 33 cout<<"栈空"<<endl; 34 else 35 { 36 s->next=p->next; 37 free(p); 38 } 39 } 40 void getstack(linkstack s) 41 { 42 if(stackempty(s)) 43 cout<<"空栈"; 44 else 45 { 46 cout<<(s->next)->data<<" "; 47 } 48 } 49 int main() 50 { 51 int i; 52 linkstack s; 53 initstack(&s); 54 for(i=0;i<5;i++) 55 pushstack(s,i); 56 for(i=0;i<5;i++) 57 { 58 getstack(s); 59 popstack(s); 60 } 61 }