链队列(C++实现)


#include<iostream>
using namespace std;

typedef struct Qnode
{
    int data;
    struct Qnode * next;
}Node;

typedef struct node
{
    Node * front;
    Node * rear;
}NodePointer;

void InitQueue(NodePointer & q)         //初始化链队列的双指针
{
    q.front = (Node *)malloc(sizeof(Node));
    q.rear = (Node *)malloc(sizeof(Node));
    q.front = q.rear;
    q.front->next = nullptr;
    q.rear->next = nullptr;
    if(!q.front || !q.rear)
    {
        cout << "\n\t\t内存分配失败,程序退出" << endl;
        exit(-1);
    }
    cout << "\n\t\t初始化链队列成功!!" << endl;
    return;
}

void TestKeyValue(int & key)        //测试输入数字的合理性
{
    if(key == 1 || key ==0)
    {
        return;
    }
    else
    {
        cout << "\n\t\t你的输入有误!" << endl;
        cout << "\n\t\t请重新输入  ";
        cin >> key;
        TestKeyValue(key);
    }
}

void TestKeyValue2(char & key2)        //测试输入数字的合理性
{
    if(key2 == '1' || key2 == '2')
    {
        return;
    }
    else
    {
        cout << "\n\t\t你的输入有误!" << endl;
        cout << "\n\t\t请重新输入:";
        cin >> key2;
        TestKeyValue2(key2);
    }
}

void PushNode(NodePointer & q,int elemt)    //插入元素
{
    Node * node  = (Node *)malloc(sizeof(Node));
    if(!node)
    {
        cout << "\n\t\t内存分配失败,程序退出" << endl;
        exit(-1);
    }
    node->next = NULL;
    node->data = elemt;
    if(q.front == NULL)         //采用q.front->next 的原因是q.front 保存了 q.rear的初始内存地址,保存直接从第二个节点开始
    {
        q.front = node;
        q.rear = node;          //由于q.front已经被保存了q.rear ,所以该if条件不执行
    }
    else
    {
        q.rear->next = node;
        q.rear = node;
    }
    cout << "\n\t\t插入元素成功!" << endl;
    return;
}

bool PabNodeHead(NodePointer & q,int & data)     //弹出对头元素
{
    if(q.front== nullptr || q.rear==nullptr)
    {
        return false;
    }
    Node * p = q.front->next;
    data = p->data;
    q.front->next = p->next;        //采用q.front->next 的原因是q.front 保存了 q.rear的初始内存地址,保存直接从第二个节点开始
    delete(p);
    return true;
}

bool PabNodeBack(NodePointer & q,int & data)        //弹出队尾元素
{
    if(q.front==NULL || q.rear==NULL)
    {
        return false;
    }
    Node * head = q.front->next;
    while(head->next != q.rear)
    {
        head = head->next;
    }
    Node * p = q.rear;
    data  = p->data;
    q.rear = head;
    delete(p);
    return true;
}

bool getQueueHeadNode(NodePointer & q,int & data)   //获取队列的头元素
{
    if(q.front == NULL)
    {
        cout << "\n\t\t队列为空!" << endl;
        return false;
    }
    data = q.front->next->data;
    return true;
}

void ShowQueue(NodePointer & q)
{
    if(q.front == NULL)
    {
        cout << "\n\t\t队列为空!" << endl;
        return;
    }
    cout << "\n\t\t队列的输出如下!" << endl;
    Node * p = q.front->next;
    cout << "\n\t\t";
    while(p != q.rear)
    {
        printf("%6d",p->data);
        p = p->next;
    }
    printf("%6d",p->data);
    return;
}

void ShowMenu()
{
    printf("\n\t\t*****************************************************");
    printf("\n\t\t*          1--------进     队                      *");
    printf("\n\t\t*          2--------出     队                      *");
    printf("\n\t\t*          3--------读对头元素                      *");
    printf("\n\t\t*          4--------显     示                      *");
    printf("\n\t\t*          5--------双向的队列                      *");
    printf("\n\t\t*          0--------退     出                      *");
    printf("\n\t\t*****************************************************");
}

#define QUEUEMAXLEN 20
int queue[QUEUEMAXLEN];     //设置队列的最大长度
int front=-1;
int rear=-1;

void InQueue(int val)
{
    rear = (rear+1)%QUEUEMAXLEN;
    if(front == rear)
    {
        cout << "\n\t\t队列已满!" << endl;
    }
    else
    {
        queue[rear] = val;
    }
}

int OutPutElemtBack()           //获取尾部元素
{
    int temp;
    if(front == rear)
    {
        cout << "\n\t\t队列为空!" << endl;
    }
    temp = queue[rear--];
    if(rear<0 && front!=-1)
    {
        rear = QUEUEMAXLEN-1;
    }
    return temp;
}

int OutPutElemtHead()           //获取队头元素
{
    int temp;
    temp = queue[++front];
    if(front == QUEUEMAXLEN)
    {
        front=0;
    }
    return temp;
}

void DQ()//输入限制性双向队列
{
    char choice;
    int out[5];
    int in[5]={5,4,3,2,1};//队列中预先输入5个数据
    int t,pos=0,i;
    for(i=0;i<5;i++)
        InQueue(in[i]);
    printf("\n\t\t初始数据顺序是:");
    for(i=0;i<5;i++)
        printf("[%d]",in[i]);
    printf("\n\n\t\t 1------队头输出  2------队尾输出\n\n");
    while(front!=rear)
    {
        printf("\n\t\t请输入选择(1 或 2):");
        cin >> choice;
        TestKeyValue2(choice);
        switch(choice)
        {
            case '1':
                t=OutPutElemtHead();
                out[pos++]=t;
                break;

            case '2':
                t=OutPutElemtBack();
                out[pos++]=t;
                break;
        }
    }
    printf("\n\t\t数据输出的顺序是:");
    for(i=0;i<5;i++)
        printf("[%d]",out[i]);
    printf("\n");
    getchar();
}



int main(void)
{
    bool loop = true;
    NodePointer q;
    InitQueue(q);       //初始化队列
    do
    {
        ShowMenu();
        cout << "\n\t\t请输入你的选择(0-5):";
        char key;
        cin >> key;
        switch(key)
        {
            case '1':
                cout << "\n\t\t[  进      队  ]" << endl;
                cout << "\n\t\t请你输入入队元素  ";
                int num;
                cin >> num;
                PushNode(q,num);
                break;

            case '2':
                cout << "\n\t\t[ 出      队 ]" << endl;
                cout << "\n\t\t弹出元素 1-头部元素 0-尾部元素  ";
                int key2;
                cin >> key2;
                TestKeyValue(key2);
                int data;                       //获取数据
                if(key2 == 1)
                {
                    if(PabNodeHead(q,data))
                    {
                        cout << "\n\t\t" << data << endl;
                        cout << "\n\t\t弹出头部元素成功" << endl;
                    }
                    else
                    {
                        cout << "\n\t\t队列为空!" << endl;
                    }
                }
                else
                {
                    if(PabNodeBack(q,data))
                    {
                        cout << "\n\t\t" << data << endl;
                        cout << "\n\t\t弹出队尾元素完毕" << endl;
                    }
                    else
                    {
                        cout << "\n\t\t队列为空!" << endl;
                    }
                }
                break;

            case '3':
                cout << "\n\t\t[  读对头元素  ]" << endl;
                int FirstData;
                if(getQueueHeadNode(q,FirstData))
                {
                    cout << "\n\t\t" << FirstData << endl;
                }
                else
                {
                    cout << "\n\t\t队列为空!" << endl;
                }
                break;

            case '4':
                cout << "\n\t\t展示队列" << endl;
                ShowQueue(q);
                break;

            case '5':
                cout << "\n\t\t[  双向的队列   ]" << endl;
                DQ();
                break;

            case '0':
                cout << "\n\t\t[  退      出  ]" << endl;
                int key3;
                cout << "\n\t\t是否确定退出  1-是 0-否  ";
                cin >> key3;
                TestKeyValue(key3);
                if(key3 == 1)
                {
                    loop = false;
                    cout << "\n\t\t程序退出!" << endl;
                }
                else
                {
                    cout << "\n\t\t程序继续运行!" << endl;
                }
                break;

            default:
                cout << "\n\t\t输入有误,请重新选择!" << endl;
                break;
        }
    }while(loop);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值