用链表实现队列的功能

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

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

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("\n");
}

测试程序的主函数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..\n");
    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..\n");
    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]

 

posted @ 2015-07-16 14:31  Magnum Programm Life  阅读(1699)  评论(0编辑  收藏  举报