• 数据结构-编程实现队列的入队、出队、测长、打印、出队


    1:代码如下:

    #include "stdafx.h"
    #include<malloc.h>
    #include <iostream>
    #include <assert.h>
    using namespace std;
    typedef struct _Node
    {
        int data;
        struct _Node *next;//指向链表下一个指针
    }node;
    
    typedef struct _Queue
    {
        node *front;//队头
        node *rear;//队尾
    }MyQueue;
    
    //构造空的队列
    MyQueue *CreateMyQueue()//空队列给个地址,队首队尾置空
    {
        MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));
        q->front = NULL;//把队首指针置空
        q->rear = NULL;//把队尾指针置空
        return q;
    }
    
    //入队,从队尾一端插入节点
    MyQueue *enqueue(MyQueue *q, int data)
    {
        node *newP = NULL;
        newP = (node *)malloc(sizeof(node));//新建节点
        newP->data = data;//复制节点数据
        newP->next = NULL;
        if (q->rear == NULL)//若队列为空,则新节点即使队首又是队尾,队尾为空,肯定为空
        {
            q->front = q->rear = newP;
        }
        else//若队列不为空,则新节点放到队尾,队尾指针指向新节点
        {
            q->rear->next = newP;//新节点放到队尾
            q->rear = newP;//将新节点作为新的队尾
        }
        return q;
    }
    
    /*队列的打印*/
    void PrintMyQueue(MyQueue *q)
    {
        node *pnode = q->front;
        if (pnode == NULL)//队列为空,空队列的队首队尾皆为空
        {
            printf("Empty Queue!
    ");
            return;
        }
        printf("data:");
        while (pnode != q->rear)//遍历队列
        {
            printf("%d", pnode->data);//打印节点数据
            pnode = pnode->next;
        }
        printf("%d", pnode->data);//打印队尾节点数据
    }
    
    //队列的测长
    int GetLength(MyQueue *q)
    {
        int nlen = 0;
        node *pnode = q->front;//指向对头
        if (pnode != NULL)//队列不为空
        {
            nlen = 1;
        }
        while (pnode != q->rear)//遍历队列
        {
            pnode = pnode->next;
            nlen++;
        }
        return nlen;
    }
    
    //出队,从队头一端删除节点
    MyQueue *dequeue(MyQueue *q)
    {
        node *pnode = NULL;
        pnode = q->front;//指向队头
        if (pnode == NULL)//队列为空
        {
            printf("Empty queue!
    ");
        }
        else
        {
            q->front = q->front->next;//新对头
            if (q->front == NULL)//当删除后队列为空时,对rear置空
            {
                q->rear = NULL;
            }
            free(pnode);
        }
        return q;
    }
    
    int main()
    {
        int nlen = 10;
        MyQueue *hp = CreateMyQueue();//建立队列
        enqueue(hp, 1);//入队1 2 3 4
        enqueue(hp, 2);
        enqueue(hp, 3);
        enqueue(hp, 4);
        nlen = GetLength(hp);//获得队列的长度
        printf("nlen=%d
    ", nlen);
        PrintMyQueue(hp);
        dequeue(hp);
        cout << "出队后:" << endl;
        PrintMyQueue(hp);
        return 0;
    }
    View Code

    运行结果:

  • 相关阅读:
    卿学姐与魔法(优先队列)
    H国的身份证号码(搜索)
    钓鱼(贪心,优先队列)
    Communication System(动态规划)
    最长连续01字符串
    魔法跳舞链 (最小生成树)
    括号匹配(线段树)
    bzoj 1042: [HAOI2008]硬币购物
    bzoj 1057: [ZJOI2007]棋盘制作
    bzoj 1452: [JSOI2009]Count
  • 原文地址:https://www.cnblogs.com/lovemi93/p/7605483.html
Copyright © 2020-2023  润新知