数据结构——队列

//只有一个尾指针的链队列
#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值