文章图片标题

堆栈链表实现方式(C)

分类:算法与数据结构 作者:阳光倾城 评论:0 点击: 448 次 日期:2016-07-31

C实现:

    #include "stackli.h"
        #include "fatal.h"
        #include <stdlib.h>
        struct Node
        {
            ElementType Element;
            PtrToNode   Next;
        };
/* START: fig3_40.txt */
        int
        IsEmpty( Stack S )
        {
            return S->Next == NULL;
        }
/* END */
/* START: fig3_41.txt */
        Stack
        CreateStack( void )
        {
            Stack S;
            S = malloc( sizeof( struct Node ) );
            if( S == NULL )
                FatalError( "Out of space!!!" );
            S->Next = NULL;
            MakeEmpty( S );
            return S;
        }
        void
        MakeEmpty( Stack S )
        {
            if( S == NULL )
                Error( "Must use CreateStack first" );
            else
                while( !IsEmpty( S ) )
                    Pop( S );
        }
/* END */
        void
        DisposeStack( Stack S )
        {
            MakeEmpty( S );
            free( S );
        }
/* START: fig3_42.txt */
        void
        Push( ElementType X, Stack S )
        {
            PtrToNode TmpCell;
            TmpCell = malloc( sizeof( struct Node ) );
            if( TmpCell == NULL )
                FatalError( "Out of space!!!" );
            else
            {
                TmpCell->Element = X;
                TmpCell->Next = S->Next;
                S->Next = TmpCell;
            }
        }
/* END */
/* START: fig3_43.txt */
        ElementType
        Top( Stack S )
        {
            if( !IsEmpty( S ) )
                return S->Next->Element;
            Error( "Empty stack" );
            return 0;  /* Return value used to avoid warning */
        }
/* END */
/* START: fig3_44.txt */
        void
        Pop( Stack S )
        {
            PtrToNode FirstCell;
            if( IsEmpty( S ) )
                Error( "Empty stack" );
            else
            {
                FirstCell = S->Next;
                S->Next = S->Next->Next;
                free( FirstCell );
            }
        }
/* END */




声明: 除非注明,本文属( 阳光倾城 )原创,转载请保留链接: http://www.tomrrow.com/archives-7750.html