第一范文网 - 专业文章范例文档资料分享平台

数据结构实验二题目一栈和队列实验报告

来源:用户分享 时间:2025/8/18 21:32:12 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

.

入队:判断若为队满,抛出异常,尾指针后移,指向入队元素。 出队:判断若为队空,抛出异常,头指针后移,输出队头元素。 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 using namespace std; struct Node //定义结点 {

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;

top=top->next;

.

数据结构实验二题目一栈和队列实验报告.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2x6xt2yk1b3pebe0io3703gjy5zd2f00lvd_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top