zoukankan      html  css  js  c++  java
  • 7.队列的链表实现

    fatal.h

    #include <stdio.h>
    #include <stdlib.h>
    
    #define Error(Str)        FatalError(Str)
    #define FatalError(Str)   fprintf(stderr, "%s
    ", 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
    ", Front(Q));
            Dequeue(Q);
        }
        for (i = 0; i < 10; i++)
            Enqueue(i, Q);
    
        while (!IsEmpty(Q))
        {
            printf("%d
    ", Front(Q));
            Dequeue(Q);
        }
    
        DisposeQueue(Q);
        return 0;
    }
    
  • 相关阅读:
    有关于* daemon not running.starting it now on port 5037 *ADB
    在android中调用jni,出现ReferenceTable overflow (max=1024)
    15款优秀移动APP产品原型设计工具
    基于Jenkins+git+gradle的android持续集成,jenkinsgradle
    java代码分析及分析工具
    Android客户端SQLite数据库升级方案
    基于Android SQLite的升级
    Android唯一识别码
    Android中获取设备信息的方法
    Android Ubuntu 安装问题FAQ
  • 原文地址:https://www.cnblogs.com/typewriter/p/6213463.html
Copyright © 2011-2022 走看看