zoukankan      html  css  js  c++  java
  • 堆栈的存储结构及其基础操作

    今天复习了栈的内容,接下来更新相关应用

    /*栈的顺序存储实现*/
    /* 栈的顺序存储结构通常由一维数组和一个记录栈顶元素的位置的变量组成*/

    一、顺序栈

    1.结构体

    typedef struct SNode *Stack;
    struct SNode
    {
        ElementType Data[MaxSize];
        int Top;
    };

    2.入栈

    void Push(Stack PtrS,ElementType item)
    {
        if(PtrS->Top == MsxSize-1)
        {
            printf("堆栈满");return; 
        }
        else
        {
            PtrS->Data[++(PtrS->Top)] == item;
            return;
        }
    }

    3.出栈

    ElementType Pop(Stack PtrS)
    {
        if(PtrS->Top == -1)
        {
            printf("堆栈空");
            return ERROR;      /*ERROR 是ElementType的特殊值,标志错误 */ 
        }
        else
        {
            return PtrS->Data[PtrS->Top];
            PtrS->Top--;
        }
     } 
     

    二、双栈

    1.结构体

    typedef struct
    {
        ElementType Data[MaxSize];
        int top1;
        int top2;
        
     }S;
    S->top1 = -1;
    S->top2 = MaxSize;

    2.入栈

    void Push(S *PtrS,ElementType item,int Tag)    //Tag是区分两个栈的标志取值为1和2 
    {
        if(PtrS->top2 - PtrS->top1 == 1)
        {
            printf("堆栈满");
            return;
        }
        if(Tag == 1)   //对第一个栈操作 
            PtrS->Data[++(PtrS->top1)] = item;
        else
            PtrS->Data[--(PtrS->top2)] = item;
    }

    3.出栈

    ElementType Pop(S *PtrS,int Tag)
    {
        if(Tag == 1 )
        {
            if(PtrS->Top1 == -1)
            {
                    printf("栈1空");
                    return NULL;    
            }
            else return PtrS->Data[(PtrS->top1)--];    
        }
        else
        {
            if(PtrS->Top2 == MaxSize)
                {
                        printf("栈2空");
                        return NULL;    
                }
            else
            return PtrS->Data[(PtrS->top2)++ ];
        }
     } 

    三、链式存储

    //栈的链式存储结构实际上就是一个单链表,叫做链栈。插入和删除智能在栈的栈顶进行。

    1.结构体

    typedef struct SNode *Stack;
    struct SNode
    {
        ElementType Data;
        struct SNode *Next;
    };

    2.构造头节点

    Stack CreateStack()
    {  //构建一个堆栈的头节点,返回指针
        Stack S;
        S = (Stack)malloc(sizeof(struct SNode));
        S->Next = NULL;
        return S;    
    }

    3.判断栈是否为空

    int IsEmpty(Stack S)    /*判断是否为空 若为空  返回  1 否则返回1 */ 
    {
        return (S->Next == NULL);
    }

    4.入栈

    /*入栈*/
    void Push(ElementType item,Stack S)   /*将item压入栈*/ 
    {
        Stack TmpCell;             //申请一个空节点 
        TmpCell = (Stack)malloc(sizeof(struct SNode));
        TmpCell->Data = item;
        TmpCell->Next = S->Next;
        S->Next = TmpCeLL;
    } 

    5.出栈

    ElementType Pop(Stack S)
    {     //删除并返回栈顶S的元素 
        Stack FirstCell;
        ElemType TopElem;
        if(IsEmpty(S))
        {
            printf("堆栈空");return NULL; 
        }
        else
        {
            FirstCell = S->Next;
            S->Next = FirstCell->Next;
            TopElem = FirstCell->Data;
            free(FirstCell);
            return TopElem;
        }    
    }

    以上就是小白的总结,代码没有调试,如有问题请在下方留言哟~

  • 相关阅读:
    hdu 3342 Legal or Not 拓排序
    hdu 1596 find the safest road Dijkstra
    hdu 1874 畅通工程续 Dijkstra
    poj 2676 sudoku dfs
    poj 2251 BFS
    poj Prime Path BFS
    poj 3278 BFS
    poj 2387 Dijkstra 模板
    poj 3083 DFS 和BFS
    poj 1062 昂贵的聘礼 dijkstra
  • 原文地址:https://www.cnblogs.com/Pigsss/p/13219785.html
Copyright © 2011-2022 走看看