/* author screen name Andromeda_Galaxy; chinese name 杨子俊 */ #include<bits/stdc++.h> using namespace std; struct position { int date=0; position *next; position *before; }; struct qstack { position p; // front; position q; // rear; }; void build(qstack &s) { s.q.date=0; s.q.before=NULL; s.q.next=&s.q; s.p.date=0; s.p.before=&s.q; s.p.next=NULL; } void make_stack(qstack &s,int x,int num) { if(x==1) { cout<<"前端插入元素 "<<num<<endl; position *x1=s.p.before; (*x1).next=new position; position *x2=(*x1).next; (*x2).date=num; (*x1).next=x2; (*x2).before=x1; (*x2).next=&s.p; s.p.before=x2; } else { cout<<"后端插入元素 "<<num<<endl; position *x1=s.q.next; (*x1).before=new position; position *x2=(*x1).before; (*x2).date=num; (*x1).before=x2; (*x2).next=x1; (*x2).before=&s.q; s.q.next=x2; } } void p_stack(qstack &s,int x) { if(x==1) { position *x2=s.p.before; cout<<"前端删除元素 "<<(*x2).date<<" "<<endl; position *x1=(*x2).before; (*x1).next=&s.p; s.p.before=x1; delete(x2); } else { position *x2=s.q.next; cout<<"后端删除元素 "<<(*x2).date<<" "<<endl; position *x1=(*x2).next; (*x1).before=&s.p; s.q.next=x1; delete(x2); } } void ffind(qstack &s,int x) { position *x2=&s.p; if(s.q.next==x2) { cout<<"error"<<endl; return ; } if(x==1) { position x1=*s.p.before; cout<<"查询前端元素 "<<x1.date<<endl; } else { position x1=*s.q.next; cout<<"查询后端元素 "<<x1.date<<endl; } } int main() { qstack s; cout<<"make s"<<endl; build(s); cout<<"build s"<<endl; make_stack(s,1,10); make_stack(s,1,100);// cout<<"make_stack OK"<<endl; cout<<"-------------------"<<endl; ffind(s,1); ffind(s,0); cout<<"-------------------"<<endl; make_stack(s,0,1); make_stack(s,0,2); cout<<"-------------------"<<endl; ffind(s,1); ffind(s,0); cout<<"-------------------"<<endl; p_stack(s,1); p_stack(s,0); cout<<"-------------------"<<endl; ffind(s,1); ffind(s,0); cout<<"-------------------"<<endl; p_stack(s,1); p_stack(s,0); cout<<"-------------------"<<endl; ffind(s,1); }
C++ 作业 (双向栈)
猜你喜欢
转载自www.cnblogs.com/Andromeda-Galaxy/p/9954527.html
今日推荐
周排行