一、实验目的
1、
熟练掌队列的结构特点,掌握队列的顺序存储和链式存储结构和实现。
2、
学会使用队列解决实际问题。
二、实验内容
1、
自己确定结点的具体数据类型和问题规模:
建立链队列,实现队列的入队和出队操作。
// 链队列.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int da=0,Node *p=NULL){
this->data=da;
this->next=p;
}
};
template <typename DT>
class LinkQueue
{public:
LinkQueue();
void EnQueue(DT x);
DT DeQueue();
DT GetFirst();
int Empty();
private:
Node *front ,*rear;
};
template<typename DT>
LinkQueue<DT>::LinkQueue()
{ Node*s=new Node;
s->next=NULL;
front=rear=s;
}
template<typename DT>
void LinkQueue<DT>::EnQueue(DT x)
{Node*s=new Node;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
template <typename DT>
DT LinkQueue<DT>::DeQueue()
{if(rear==front)cout<<"下溢";
Node*p=front->next;
DT x=p->data;
front->next=p->next;
if(p->next==NULL)rear=front;
delete p;
return x;
}
template <typename DT>
int LinkQueue<DT>::Empty()
{cout <<"队列是否为空?"<<endl;
if(front==rear)return 1;
else return 0;
}
int main()
{ LinkQueue<int> Q;
int a[5]={86,79,89,97,93};
cout<<Q.Empty()<<endl;
for(int i=0;i<5;i++){Q.EnQueue(a[i]);}
cout<<"输出所有元素:";
for (int j=0;j<5;j++){cout<<Q.DeQueue()<<" ";}
return 0;
}