6_56.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
- #include<iostream.h>
- struct sqstack
- {
- char data;
- sqstack* top;
- };
- class stack
- {
- sqstack * st;
- public:
- void init()
- {
- st=NULL;
- }
- void push(char );
- char pop();
- };
- void stack::push(char k)
- {
- sqstack * newst=new sqstack;
- newst->data=k;
- newst->top=st;
- st=newst;
- }
- char stack::pop()
- {
- char value;
- sqstack* t;
- value=st->data;
- t=st;
- st=st->top;
- delete t;
- return value;
- }
- void main()
- {
- stack A;
- A.init();
- int n;
- cout<<"请输入字符个数n:";
- cin>>n;
- char *arr=new char[n];
- cout<<"请输入n个字符:"<<endl;
- for(int i=0;i<n;i++)
- cin>>arr[i];
- cout<<endl;
- cout<<"入栈顺序:";
- for ( i=0;i<n;i++)
- {
- cout<<arr[i]<<" ";
- A.push(arr[i]);
- }
- cout<<endl<<"Out! ";
- for( i=0;i<n;i++)
- cout<<A.pop()<<" ";
- cout<<endl;
- delete arr;
- }