发布时间 : 星期三 文章数据结构实验二题目一栈和队列实验报告更新完毕开始阅读
.
入队:判断若为队满,抛出异常,尾指针后移,指向入队元素。 出队:判断若为队空,抛出异常,头指针后移,输出队头元素。 b、 算法实现
void circlequeue::enqueue(int x)//进队操作
{if((rear+1)%queuesize==front) throw \上溢\异常处理 rear=(rear+1)%queuesize; data[rear]=x; }
void circlequeue::dequeue()//出队操作 {if(rear==front) throw \下溢\异常处理 front=(front+1)%queuesize; cout< void circlequeue::getfront()得到队头元素 {if(rear==front) throw \下溢\异常处理 cout< void circlequeue::getlength()//得到队列长度 {cout<<((rear-front+queuesize)%queuesize)< D、实现一个链队列 void linkqueue::enqueue(int x)//进队操作 {rear->next=new Node; rear=rear->next; rear->data=x; rear->next=NULL; } void linkqueue::dequeue()//出队操作 {Node*p=front->next; . . front->next=p->next; int x=p->data; delete p; if(!(front->next))rear=front; cout< void linkqueue::getfront()//得到头结点元素 {if(!(front->next)) throw \上溢\异常处理 cout<<(front->next->data)< linkqueue::~linkqueue()//析构 {while(front) {rear=front->next; delete front; front=rear; } } 2.3 其他 源代码 #include int data; struct Node*next; }; const int seqstack=100; class shareseqstack //定义共享栈 {public: shareseqstack(); void push(int x,int pushnumber); void pop(int popnumber); void gettop(int getnumber); private: int data[seqstack]; int top1; int top2; }; shareseqstack::shareseqstack()//构造 {top1=-1; top2=seqstack; . . } void shareseqstack::push(int x,int pushnumber)//进栈操作 {if(top1+1==top2)//异常处理 throw \上溢\ if(pushnumber==1)//判断栈1还是栈2 data[++top1]=x; if(pushnumber==2) data[--top2]=x; } void shareseqstack::pop(int popnumber)//出栈操作 {if(popnumber==1) //异常处理 { if(top1==-1) throw \下溢\ else cout< if(popnumber==2) //异常处理 {if(top2==seqstack) throw \下溢\else cout< void shareseqstack::gettop(int getnumber)//得到栈顶元素 {if(getnumber==1) //判断栈1还是栈2 {if(top1==-1) throw \下溢\异常处理 cout< {if(top2==seqstack) throw \下溢\cout< class linkqueue//定义链队列 {public: linkqueue(){front=rear=new Node; front->next=NULL;} ~linkqueue(); void enqueue(int x); void dequeue(); void getfront(); bool empty(){return front==rear? true:false;} private: Node*front; Node*rear; }; void linkqueue::enqueue(int x)//进队操作 {rear->next=new Node; rear=rear->next; . . rear->data=x; rear->next=NULL; } void linkqueue::dequeue()//出队操作 {Node*p=front->next; front->next=p->next; int x=p->data; delete p; if(!(front->next))rear=front; cout< void linkqueue::getfront()//得到头结点元素 {if(!(front->next)) throw \上溢\异常处理 cout<<(front->next->data)< linkqueue::~linkqueue()//析构 {while(front) {rear=front->next; delete front; front=rear; } } class linkstack//定义链栈 {public: linkstack(){top=NULL;} ~linkstack(); void push(int x); void pop(); void gettop(); bool empty(){return(NULL==top)? true:false;} private: struct Node*top; }; void linkstack::push(int x)//进栈操作 {Node*p=new Node;//定义新结点 p->data=x; p->next=top; top=p; } void linkstack::pop()//出栈操作 {if(empty()) throw \下溢\异常处理 int x=top->data; Node*p=new Node; //定义新结点 p=top; .