《数据结构》实验三: 栈和队列实验 补

前端之家收集整理的这篇文章主要介绍了《数据结构》实验三: 栈和队列实验 补前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_301_5@2、自己选择顺序或链式存储结构,定义一个空栈队列,并定义入栈、出栈、取栈元素基本操作。然后在主程序中对给定的N个数据进行验证,输出各个操作结果。@H_301_5@@H_301_5@

文件@H_301_5@

# ifndef LinkQueue_H@H_301_5@

# define LinkQueue_H@H_301_5@

template<class DataType>@H_301_5@
@H_301_5@

struck Node@H_301_5@@H_301_5@

{@H_301_5@@H_301_5@

DataType data;@H_301_5@@H_301_5@

Node<DataType> *next;@H_301_5@@H_301_5@

};@H_301_5@@H_301_5@

template<class DataType>@H_301_5@
@H_301_5@@H_301_5@

classLinkQueue@H_301_5@@H_301_5@
@H_301_5@@H_301_5@@H_301_5@

{@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

public:@H_301_5@

LinkQueue();@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

~@H_301_5@LinkQueue();@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

voidEnQueue(DataType x);@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

DataType DeQueue();@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

DataType GetQueue();@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

int Empty();@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

pirvate:@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

Node<DataType> *front,*rear;@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

};@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

#endif;@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@@H_301_5@

源程序文件@H_301_5@

# include"LinkQueue.h"
template<class DataType>
LinkQueue<DataType>::LinkQueue()
{
@H_301_5@Node<DataType> *s=NULL;
@H_301_5@s=new Node<DataType>;
@H_301_5@s->next=NULL;
@H_301_5@front=rear=s;
}
template<class DataType>
LinkQueue<DataType>::~LinkQueue()
{
@H_301_5@Node<DataType> *p=NULL;
@H_301_5@while(front!=NULL)
@H_301_5@{
@H_301_5@p=front->next;
@H_301_5@delete front;
@H_301_5@front=p;
@H_301_5@}
}
template<class DataType>
void LinkQueue<DataType>::EnQueue(DataType x)
{
@H_301_5@Node<DataType> *s=NULL;
@H_301_5@s=new Node<DataType>;
@H_301_5@s->data=x;
@H_301_5@s->next=NULL;
@H_301_5@rear->next=s;rear=s;
}
template<class DataType>
DataType LinkQueue<DataType>::DeQueue()
{
@H_301_5@Node<DataType> *p=NULL;
@H_301_5@int x;
@H_301_5@if(rear==front)throw"下溢";
@H_301_5@p=front->next;
@H_301_5@x=p->data;
@H_301_5@front->next=p->next;
@H_301_5@if(p->next==NULL)rear=front;
@H_301_5@delete p;
@H_301_5@return x;
}
template<class DataType>
DataType LinkQueue<DataType>::GetQueue()
{
@H_301_5@if(front!=rear)
@H_301_5@return front->next->data;
}
template<class DataType>
int LinkQueue<DataType>::Empty()
{
@H_301_5@if(front==rear)
@H_301_5@return 1;
@H_301_5@else
@H_301_5@return 0;
}


@H_301_5@

函数文件@H_301_5@

#include<iostream> using namespace std; # include"LinkQueue.cpp" void main() { LinkQueue<int>Q; if(Q.Empty()) cout<<"队列为空"<<endl; else cout<<"队列非空"<<endl; cout<<"元素10和15执行入队操作:"<<endl; try { Q.EnQueue(10); Q.EnQueue(15); } catch(char* wrong) { cout<<wrong<<endl; } cout<<"查看队头元素:"<<endl; cout<<Q.GetQueue()<<endl; cout<<"执行出队操作:"<<endl; try { Q.DeQueue(); } catch(char*wrong) { cout<<wrong<<endl; } cout<<"查看队头元素:"<<endl; cout<<Q.GetQueue()<<endl; }@H_301_5@

猜你在找的数据结构相关文章