zoukankan      html  css  js  c++  java
  • 用链表实现队列的功能

    链表不限定元素的长度,可以动态分配元素并添加,另外经常的增删是链表优于其他数据结构的特点.

    今天我们用链表来实现一个队列.

    linkList.h

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    #define new(type) (type *)malloc(sizeof(type))
    #define FREE(p) 
            if (p != NULL) {
                    free(p);
                    p = NULL;
                }
    typedef struct Node{
        int data;
        struct Node *next;
    }ListNode, *pListNode;
    
    typedef struct _Queue{
        int size;
        pListNode headLink;
        pListNode tailLink;
    }Queue, *pQueue;
    
    pQueue CreatedQueue(void);
    pListNode CreateNode(int value);
    pListNode popQueue(pQueue);
    void pushQueue(pQueue queue, pListNode node);
    void DestroyQueue(pQueue *queue);
    void DestroyListNode(pListNode *node);
    int LengthOfQueue(pQueue queue);
    void ShowQueue(pQueue queue);

    这里引进size对队列进行计数,

    api中并没有判断empty 或者 full,直接用这个size即可.

    linkList.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <assert.h>
    #include "linkList.h"
    
    //创建队列时,头尾指针均指向data域为0的节点.
    pQueue CreatedQueue(void){
        pQueue pq = new(Queue);
        assert(pq != NULL);
        //pListNode pn = CreateNode(0);
        //assert(pn != NULL);
        pq->size = 0;
        pq->headLink = NULL; //pn;
        pq->tailLink = NULL; //pn;
        return pq;
    }
    
    pListNode CreateNode(int value){
        pListNode pn= new(ListNode);
        assert(pn != NULL);
        pn->data = value;
        pn->next = NULL;
        return pn;
    }
    
    //删除节点是删除headLink指向的节点,改变headLink指向
    pListNode popQueue(pQueue queue){
        assert(queue != NULL);
        if(queue->size == 0)
            return NULL;
        pListNode pn = queue->headLink;
        queue->headLink = pn->next;
        pn->next = NULL;
        queue->size --;
        if(queue->size ==0)
            queue->tailLink = NULL;
        return pn;
    }
    
    //增加节点放在队尾,改变tailLink指向,添加第一个元素headLink和tailLink均指向这个节点
    void pushQueue(pQueue queue, pListNode node){
        assert(queue != NULL);
        assert(node != NULL);
        if(queue->size == 0){
            queue->headLink = node;
            queue->tailLink = node;
        }
        else{
            queue->tailLink->next = node;
            queue->tailLink = node;
        }
        queue->size++;
    }
    
    void DestroyQueue(pQueue *queue){
        assert(*queue != NULL);
        while((*queue)->size--!=0){ //清空所有节点
            pListNode pn = popQueue(*queue);
            DestroyListNode(&pn);
        }
        //FREE(queue->headLink);
        //FREE(queue->tailLink);
        FREE(*queue);
    }
    
    void DestroyListNode(pListNode *node){
        assert(*node != NULL);
        (*node)->next = NULL;
        FREE(*node);
    }
    
    int LengthOfQueue(pQueue queue){
        assert(queue != NULL);
        assert(queue->size ==0 || queue->size > 0);
        return queue->size;
    }
    
    void ShowQueue(pQueue queue){
        pListNode pn = queue->headLink;
        if(pn == NULL)
            return ;
        printf("ShowQueue Order ");
        int length = queue->size;
        while(length--!=0){
            printf(" [%d]", pn->data);
            pn = pn->next;
        }
        printf("
    ");
    }

    测试程序的主函数main.c

    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<string.h>
    #include"linkList.h"
    
    int main()
    {
        pQueue pq = CreatedQueue();
        printf("Push circularQueue 1,2,3,4,5,6,7..
    ");
        CreateNode(1);
        pListNode pn = CreateNode(1);
        DestroyListNode(&pn);
    
        pn = CreateNode(2);
        pushQueue(pq, pn);
        ShowQueue(pq);
        pn = CreateNode(3);
        pushQueue(pq, pn);
        pn = CreateNode(4);
        pushQueue(pq, pn);
        pn = CreateNode(5);
        pushQueue(pq, pn);
        pn = CreateNode(6);
        pushQueue(pq, pn);
    
        ShowQueue(pq);
    
        popQueue(pq);
        ShowQueue(pq);
        popQueue(pq);
        ShowQueue(pq);
        popQueue(pq);
        ShowQueue(pq);
        popQueue(pq);
        ShowQueue(pq);
        popQueue(pq);
        ShowQueue(pq);
        popQueue(pq);
        ShowQueue(pq);
        popQueue(pq);
        ShowQueue(pq);
    
        DestroyQueue(&pq);
    
        pq = CreatedQueue();
        printf("Push circularQueue 1,2,3,4,5,6,7..
    ");
        CreateNode(1);
        pn = CreateNode(1);
        DestroyListNode(&pn);
    
        pn = CreateNode(2);
        pushQueue(pq, pn);
        ShowQueue(pq);
        popQueue(pq);
        ShowQueue(pq);
        pn = CreateNode(3);
        pushQueue(pq, pn);
        ShowQueue(pq);
        pn = CreateNode(4);
        pushQueue(pq, pn);
        ShowQueue(pq);
    
        return 0;
    }
    View Code

    输出结果如下:

    Push circularQueue 1,2,3,4,5,6,7..
    ShowQueue Order  [2]
    ShowQueue Order  [2] [3] [4] [5] [6]
    ShowQueue Order  [3] [4] [5] [6]
    ShowQueue Order  [4] [5] [6]
    ShowQueue Order  [5] [6]
    ShowQueue Order  [6]
    Push circularQueue 1,2,3,4,5,6,7..
    ShowQueue Order  [2]
    ShowQueue Order  [3]
    ShowQueue Order  [3] [4]
  • 相关阅读:
    基础网络技术--学习网络的的道路漫长啊
    华为nova8se和vivoS7e的区别哪个好
    Java.awt实现一个简单的围棋
    HashMap put原理详解(基于jdk1.8)
    Paper Pal:一个中英文论文及其代码大数据搜索平台
    【u116】最短路计数
    【u108】取数游戏
    【u106】3D模型
    【topcoder SRM 652 DIV2 250】ValueOfString
    【u103】绘制二叉树
  • 原文地址:https://www.cnblogs.com/biglucky/p/4651254.html
Copyright © 2011-2022 走看看