1. 程式人生 > >基於連結串列的佇列、基於順序表的佇列 C語言

基於連結串列的佇列、基於順序表的佇列 C語言

基於連結串列的佇列:

//LinkQueue.h
#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>

typedef char LinkQueueType;

typedef struct LinkQueueNode {
    LinkQueueType data;
    struct LinkQueueNode* next;
}LinkQueueNode;

typedef struct LinkQueue {
    LinkQueueNode* head;
    LinkQueueNode* tail;
}LinkQueue;

void
LinkQueueInit(LinkQueue* q); void LinkQueuePush(LinkQueue* q, LinkQueueType value);//入隊 void LinkQueuePop(LinkQueue* q);//出佇列 int LinkQueueFront(LinkQueue* q, LinkQueueType* value);//取隊首元素
//LinkQueue.c
#include"LinkQueue.h"

void LinkQueueInit(LinkQueue* q)
{
    if (q == NULL) {
        return;
    }
    q->
head = NULL; q->tail = NULL; } LinkQueueNode* CreateLinkQueueNode(LinkQueueType value) { LinkQueueNode* new_node = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); new_node->data = value; new_node->next = NULL; return new_node; } void DestoryLinkQueueNode(LinkQueueNode* ptr) { free(ptr); } void
LinkQueuePush(LinkQueue* q, LinkQueueType value) { if (q == NULL) { return; } LinkQueueNode* new_node = CreateLinkQueueNode(value); if (q->head == NULL) { q->head = q->tail = new_node; } q->tail->next = new_node; q->tail = q->tail->next; return; } void LinkQueuePop(LinkQueue* q) { if (q == NULL) { return; } if (q->head == NULL) { //空佇列 return; } if (q->head == q->tail) { DestoryLinkQueueNode(q->head); q->head = q->tail = NULL; } LinkQueueNode* to_delete = q->head; q->head = to_delete->next; DestoryLinkQueueNode(to_delete); return; } int LinkQueueFront(LinkQueue* q, LinkQueueType* value) { if (q == NULL || value == NULL) { return 0; } if (q->head == NULL) { return 0; } *value = q->head->data; return 1; }
//test.c
#include"LinkQueue.h"

#define TEST_HEADER printf("\n===============%s==============\n", __FUNCTION__)

void Test()
{
    TEST_HEADER;
    LinkQueue q;
    LinkQueueInit(&q);
    LinkQueuePush(&q, 'a');
    LinkQueuePush(&q, 'b');
    LinkQueuePush(&q, 'c');
    LinkQueuePush(&q, 'd');
    LinkQueuePush(&q, 'e');

    LinkQueueType tmp;
    LinkQueueFront(&q, &tmp);
    printf("tmp expect a, actual %c\n", tmp);
    LinkQueuePop(&q);   
    LinkQueueFront(&q, &tmp);
    printf("tmp expect b, actual %c\n", tmp);
    LinkQueuePop(&q);   
    LinkQueueFront(&q, &tmp);
    printf("tmp expect c, actual %c\n", tmp);
    LinkQueuePop(&q);
    LinkQueueFront(&q, &tmp);
    printf("tmp expect d, actual %c\n", tmp);
    LinkQueuePop(&q);   
    LinkQueueFront(&q, &tmp);
    printf("tmp expect e, actual %c\n", tmp);
}



int main()
{
    Test();
    system("pause");
    return 0;
}

基於順序表的佇列:

//SeqQueue.h
#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>


#define SeqQueueMaxSize 1000

typedef char SeqQueueType;

typedef struct SeqQueue {
    SeqQueueType data[SeqQueueMaxSize];
    size_t head;
    size_t tail;
    size_t size;
}SeqQueue;

void SeqQueueInit(SeqQueue* q);
void SeqQueueDestory(SeqQueue* q);
void SeqQueuePush(SeqQueue* q, SeqQueueType value);
void SeqQueuePop(SeqQueue* q);
int SeqQueueFront(SeqQueue* q, SeqQueueType* value);
//SeqQueue.c
#include"SeqQueue.h"

void SeqQueueInit(SeqQueue* q)
{
    if (q == NULL) {
        return;
    }
    q->head = q->tail = 0;
    q->size = 0;
}

void SeqQueueDestory(SeqQueue* q)
{
    if (q == NULL) {
        return;
    }
    q->head = q->tail = 0;
    q->size = 0;
}

void SeqQueuePush(SeqQueue* q, SeqQueueType value)
{
    if (q == NULL) {
        return;
    }
    if (q->size == SeqQueueMaxSize) {
        return;
    }
    q->data[q->tail++] = value;
    q->tail %= SeqQueueMaxSize;
    ++q->size;
}

void SeqQueuePop(SeqQueue* q)
{
    if (q == NULL) {
        return;
    }
    if (q->size == 0) {
        return;
    }
    ++q->head;
    q->head %= SeqQueueMaxSize;
    --q->size; 
}

int SeqQueueFront(SeqQueue* q, SeqQueueType* value)
{
    if (q == NULL || value == NULL) {
        return 0;
    }
    if (q->size == 0) {
        return 0;
    }
    *value = q->data[q->head];
    return 1;
}
//test.c
#include"SeqQueue.h"

void Test()
{
    SeqQueue q;
    SeqQueueInit(&q);
    SeqQueuePush(&q, 'a');
    SeqQueuePush(&q, 'b');
    SeqQueuePush(&q, 'c');
    SeqQueuePush(&q, 'd');
    SeqQueuePush(&q, 'e');

    char tmp;
    SeqQueueFront(&q, &tmp);
    printf("tmp expect a, actual %c\n", tmp);
    SeqQueuePop(&q);
    SeqQueueFront(&q, &tmp);
    printf("tmp expect b, actual %c\n", tmp);
    SeqQueuePop(&q);
    SeqQueueFront(&q, &tmp);
    printf("tmp expect c, actual %c\n", tmp);
    SeqQueuePop(&q);
    SeqQueueFront(&q, &tmp);
    printf("tmp expect d, actual %c\n", tmp);
    SeqQueuePop(&q);
    SeqQueueFront(&q, &tmp);
    printf("tmp expect e, actual %c\n", tmp);
}

int main()
{
    Test();
    system("pause");
    return 0;
}