7.队列的链表实现

fatal.h

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

#define Error(Str)        FatalError(Str)
#define FatalError(Str)   fprintf(stderr, "%s\n", Str), exit(1)

queueli.h

typedef int ElementType;

#ifndef _Queue_h
#define _Queue_h

struct Node;
typedef struct Node *PtrToNode;
struct LinkQueue;
typedef struct LinkQueue *Queue;

int IsEmpty(Queue Q);
Queue CreateQueue(void);
void DisposeQueue(Queue Q);
void MakeEmpty(Queue Q);
void Enqueue(ElementType X, Queue Q);
ElementType Front(Queue Q);
void Dequeue(Queue Q);
ElementType FrontAndDequeue(Queue Q);

#endif  

queueli.c

#include "queueli.h"
#include "fatal.h"
#include <stdlib.h>

struct Node
{
    ElementType Element;
    PtrToNode   Next;
};

struct LinkQueue
{
    PtrToNode Front;
    PtrToNode Rear;
};

int IsEmpty(Queue Q)
{
    return Q->Front->Next == NULL;
}

Queue CreateQueue(void)
{
    Queue Q;

    Q = malloc(sizeof(struct LinkQueue));
    if (Q == NULL)
        FatalError("Out of space!!!");

    Q->Front = Q->Rear = malloc(sizeof(struct Node));
    if (Q->Front == NULL)
        FatalError("Out of space!!!");
    Q->Front->Next = NULL;
    MakeEmpty(Q);
    return Q;
}

void DisposeQueue(Queue Q)
{
    MakeEmpty(Q);
    free(Q->Front);
    free(Q);
}

void MakeEmpty(Queue Q)
{
    if (Q == NULL)
        Error("Must use CreateQueue first");
    else
        while (!IsEmpty(Q))
            Dequeue(Q);
}

void Enqueue(ElementType X, Queue Q)
{
    PtrToNode LastCell;

    LastCell = malloc(sizeof(struct Node));
    if (LastCell == NULL)
        FatalError("Out of space!!!");
    else
    {
        LastCell->Element = X;
        LastCell->Next = NULL;
        Q->Rear->Next = LastCell;
        Q->Rear = LastCell;
    }
}

ElementType Front(Queue Q)
{
    PtrToNode FirstCell;

    FirstCell = Q->Front->Next;
    if (!IsEmpty(Q))
        return FirstCell->Element;
    Error("Empty queue");
    return 0;
}

void Dequeue(Queue Q)
{
    PtrToNode FirstCell;

    if (IsEmpty(Q))
        Error("Empty queue");
    else
    {
        FirstCell = Q->Front->Next;
        Q->Front->Next = FirstCell->Next;
        if (Q->Rear == FirstCell)
            Q->Rear = Q->Front;
        free(FirstCell);
    }
}

ElementType FrontAndDequeue(Queue Q)
{
    ElementType X = 0;
    PtrToNode FirstCell;

    if (IsEmpty(Q))
        Error("Empty queue");
    else
    {
        FirstCell = Q->Front->Next;
        Q->Front->Next = FirstCell->Next;
        if (Q->Rear == FirstCell)
            Q->Rear = Q->Front;
        X = FirstCell->Element;
        free(FirstCell);
    }
    return X;
}

testqueueli.c

#include <stdio.h>
#include "queueli.h"

int main()
{
    Queue Q;
    int i;

    Q = CreateQueue();

    for (i = 0; i < 10; i++)
        Enqueue(i, Q);

    while (!IsEmpty(Q))
    {
        printf("%d\n", Front(Q));
        Dequeue(Q);
    }
    for (i = 0; i < 10; i++)
        Enqueue(i, Q);

    while (!IsEmpty(Q))
    {
        printf("%d\n", Front(Q));
        Dequeue(Q);
    }

    DisposeQueue(Q);
    return 0;
}
posted @ 2016-12-23 04:10  typewriter  阅读(250)  评论(0编辑  收藏  举报