云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 数据结构实验二题目一栈和队列实验报告

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

  • 62 次阅读
  • 3 次下载
  • 2025/6/5 20:04:52

.

入队:判断若为队满,抛出异常,尾指针后移,指向入队元素。 出队:判断若为队空,抛出异常,头指针后移,输出队头元素。 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;

.

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

. 入队:判断若为队满,抛出异常,尾指针后移,指向入队元素。 出队:判断若为队空,抛出异常,头指针后移,输出队头元素。 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<

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com