zoukankan      html  css  js  c++  java
  • 队列的链式存储---链表实现(有头结点)

    /* 队列的链式存储 */
    /* with header */
    /* with no typedef */
    struct Node{
        ElementType Ele;
        struct Node *Next;
    };
    struct LinQ{
        struct Node *rear;
        struct Node *front;
    };
    struct LinQ *
    CreateQ( void )
    {
        struct LinQ *Ptr;
        struct Node *header;
        
        Ptr = malloc(sizeof(struct LinQ));
        if(Ptr == NULL )
            Error("out of space ");
            
        header = malloc( sizeof( struct Node ) );
        if(header == NULL )
            error("out of space ");
            
        header->Next = NULL;
        Ptr->front = Ptr->rear = header;
    }
    void
    AddQ( struct LinQ *PtrQ, ElementType X )
    {
        struct Node *Ptr;
        Ptr = malloc(sizeof(struct Node ));
        if(Ptr == NULL)
            Error("out of space ");
        Ptr->Ele = X;
        Ptr->Next = NULL;
        PtrQ->rear->Next = Ptr;
        PtrQ->rear = Ptr; 
    }
    int
    IsEmpty( struct LinQ * PtrQ )
    {
        return PtrQ->front->Next == NULL;
    }
    
    ElementType
    DeleteQ( struct LinQ * PtrQ )
    {
        struct Node * TmpCell;
        if( IsEmpty( PtrQ ) )
            Error("empty Queue");
        Tmpcell = PtrQ->front->Next;
        PtrQ->front->Next = TmpCell->Next;
        free( TmpCell );
    }
    View Code

    CreateQ旨在创造一个front和rear都指向头结点header的LinQ结构

    头结点处Next设为NULL,在判断是否空队时可用

    front一直指到头结点

    新增结点的Next一定改为NULL

    结论:可以用typedef的地方就用,可以用头结点的地方就用

  • 相关阅读:
    c++Primer再学习(1)
    c++Primer再学习练习Todo
    感悟(一)
    新目标《C++程序设计原理与实践》
    C++Primer再学习(4)
    开篇
    C++Primer再学习(3)
    C++实现的单例模式的解惑
    使用springboot缓存图片
    springboot h2 database
  • 原文地址:https://www.cnblogs.com/gabygoole/p/4620571.html
Copyright © 2011-2022 走看看