您的位置:首页 > 理论基础 > 数据结构算法

数据结构——队列

2015-12-19 20:50 459 查看
//只有一个尾指针的链队列
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<iostream>
#include<stdlib.h>
using namespace std;

#define ERROR 0
#define OK 1
#define OVERFLOW 0
//*********************************************************结构类型
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
//QueuePtr front;
QueuePtr rear;
}LinkQueue;
//***********************************************************函数定义
int Init(LinkQueue &Q);
int EnQueue(LinkQueue &Q,int e);
int DeQueue(LinkQueue &Q,int &e);
int GetHead(LinkQueue Q,int &e);
int Empty(LinkQueue Q);
int Length(LinkQueue Q);
int Show(LinkQueue Q);
int Destory(LinkQueue &Q);

int main()
{
LinkQueue Q;
while(1)
{

int e;
cout<<"*************链式队列****************\n";
cout<<"1:Init\n";
cout<<"2:EnQueue\n";
cout<<"3:DeQueue\n";
cout<<"4:GetHead\n";
cout<<"5:Empty\n";
cout<<"6:Length\n";
cout<<"7:Show Q\n";
cout<<"8:Destroy\n";
cout<<"0:Break\n";
cout<<"*************************************\n";
cout<<endl;
int in;
cout<<"you choose:";
cin>>in;
if(in==0)break;
else if(in==1)Init(Q);
else if(in==2)
{
int times,x;
cout<<"Input times:";
cin>>times;
while(times>0)
{
cout<<"e:";
cin>>x;
EnQueue(Q,x);
times--;
}
}
else if(in==3)
{
DeQueue(Q,e);
cout<<"Delete elem:"<<e<<"\n";
}
else if(in==4)
{
GetHead(Q,e);
cout<<"The head is "<<e<<"\n";
}
else if(in==5)
{
if(Empty(Q))cout<<"Queue empty!\n";
else cout<<"Queue is not empty!\n";
}
else if(in==6)
{
cout<<"Length of queue is "<<Length(Q)<<"\n";
}
else if(in==7)
{
if(!Show(Q))cout<<"Queue is empty!\n";
}
else if(in==8)
{
if(Destory(Q))cout<<"Destoryed!\n";
else cout<<"Error! Queue is empty!\n";
}
else if(in!=8||in!=7||in!=6||in!=5||in!=4||in!=2||in!=1||in!=0||in!=3)
{
cout<<"ERROR\n";
}
}
return 0;
}
//************************************************************函数声明
int Init(LinkQueue &Q)
{
Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.rear) exit(OVERFLOW);
Q.rear->next=Q.rear;
cout<<"初始化成功!\n";
return OK;
}
int Destory(LinkQueue &Q)
{
if(Q.rear->next==Q.rear)return ERROR;
QueuePtr p,q;
p=Q.rear->next->next;
while(p&&p!=Q.rear)
{
q=p;
p=q->next;
Q.rear->next->next=p;
free(q);
}
Q.rear->next=Q.rear;
//Q.rear=(QueuePtr)malloc(sizeof(QNode));
Q.rear->next=Q.rear;
free(p);
return OK;
}
int Empty(LinkQueue Q)
{
if(Q.rear==Q.rear->next)
return OK;
else
return ERROR;
}
int Length(LinkQueue Q)
{
int i=0;
QueuePtr p=Q.rear->next;
while(Q.rear!=p)
{ i++;
p=p->next;
}
return i;
}
int GetHead(LinkQueue Q,int &e)
{
if(Q.rear->next==Q.rear)return ERROR;
QueuePtr k;
k=Q.rear->next->next;
e=k->data;
return OK;
}
int EnQueue(LinkQueue &Q,int e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->data=e;
p->next=Q.rear->next;
Q.rear->next=p;
Q.rear=p;
}
int DeQueue(LinkQueue &Q,int &e)
{
QueuePtr p;
if(Q.rear->next==Q.rear){cout<<"No elem!\n";e=0;return ERROR;}
p=Q.rear->next->next;
e=p->data;
Q.rear->next->next=p->next;
if(Q.rear==p)
Q.rear=Q.rear->next;
free(p);
return OK;
}
int Show(LinkQueue Q)
{
if(Q.rear->next==Q.rear)return ERROR;
QueuePtr p;
p=Q.rear->next->next;
int j=0;
while(p!=Q.rear->next)
{
++j;
cout<<"第"<<j<<"个数据:"<<p->data<<"\n";
p=p->next;
}
cout<<"\n";
return OK;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: