zoukankan      html  css  js  c++  java
  • 数据结构 堆栈

    堆栈的抽象数据类型描述

    堆栈(Stack):具有一定操作约束的线性表

    • 只在一端(栈顶,Top)做插入、删除
    • 后入先出:Last In First Out(FIFO)

    类型名称:堆栈(stack)

    数据对象集:一个有0个或多个元素的又穷线性表。

    操作集:长度为MaxSize的堆栈S∈Stack,堆栈元素item∈ElementType

    1、Stack CreateStack(int MaxSize):生成空堆栈,其最大长度为MaxSzie;

    2、int IsFull(Stack S, int MaxSize):判断堆栈S是否已满

    3、void Push(Stack S, ElementType item):将元素item压入堆栈

    4、int IsEmpty(Stack S):判断堆栈S是否为空

    5、ElementType Pop(Stack S):删除并返回栈顶元素

    栈的顺序存储实现

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

    #define MAXSIZE 100
    
    typedef struct SNode *Stack;
    struct SNode{
        ElementType Data[MAXSIZE];
        int Top;
    }
    定义

    入栈

    void Push(Stack PtrS, ElementType item)
    {
        if(PtrS->Top == MAXSIZE - 1) {
            printf("Stack full");
            return;
        } else {
            PtrS->Data[++(PtrS->Top)] = item;
            return; 
        }
    }
    push

    出栈

    ElementType Pop(Stack PtrS)
    {
        if(PtrS->Top == -1) {
            printf("Stack Empty");
            return ERROR;
        } else
            return(PtrS->Data[(PtrS->Top)--]);
    }
    pop

    例子:请用要给数组实现两个堆栈,要求最大地利用数组空间,使数组只要有空间入栈操作就可以成功。

     1 #define MAXSIZE 100
     2 struct DStack{
     3     ElementType Data[MAXSIZE];
     4     int Top1;
     5     int Top2;
     6 }S;
     7 S.Top1 = -1;
     8 S.Top2 = MAXSIZE;
     9 
    10 void Push(struct DStack *PtrS, ElementType item, int Tag)
    11 {
    12     if(PtrS->Top2 - PtrS->Top1 == 1) {
    13         printf("stack full");
    14         return;
    15     }
    16     if(Tag == 1)
    17         PtrS->Data[++(PtrS->Top1)] = item;
    18     else
    19         PtrS->Data[--(PtrS->Top2)] = item;
    20 }
    21 
    22 ElementType Pop(stuct DStack *PtrS, int Tag)
    23 {
    24     if(Tag == 1) {
    25         if(PtrS->Top == -1) {
    26             ptintf("Stack1 Empty");
    27             return;
    28         } else {
    29             return(PtrS->Data[(PtrS->Top1)--])
    30         }
    31    } else {
    32        if(PtrS->Top2 == MAXSIZE) {
    33            printf("Stack2 Empty");
    34            return;
    35        } else {
    36            return PtrS->Data[(PtrS->Top2)--];
    37        }
    38    }
    39 }
    例子

    堆栈的链式存储实现

      站的链式存储结构实际上就是一个单链表,叫做链栈。插入和删除操作智能在链栈的栈顶进行。栈顶指针Top应该在链表的哪一头?

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

    堆栈初始化

    Stack CreateStack()
    {
        Stack = s;
        S = (Stack)malloc(sizeof(struct SNode));
        S->Next = NULL;
        return S;
    }
    CreateStack

    判断堆栈S是否为空

    int IsEmpty(Stack S)
    {
        return (S->Next == NULL);
    }
    IsEmpty

    push操作

    void Push(ElementType item, Stack S)
    {
        struct SNode *TmpCell;
        TmpCell = (SNode)malloc(sizeof(struct SNode));
        TmpCell->Data = item;
        TmpCell->Next = S->Next;
        S->Next = TmpCell;
    }
    push

    pop操作

    ElementType Pop(Stack S)
    {
        struct SNode *FirstCell;
        ElementType TopElem;
        if(IsEmpty(S)) {
            printf("Stack Empty");
            return NULL;
        } else {
            FirstCell = S->Next;
            S->Next = FirstCell->Next;
            TopElem = FirstCell->Data;
            free(FirstCell);
            return TopElem;
        }
    }
    pop
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    UVA 10608 Friends
    UVA 10806 Dijkstra, Dijkstra.
    HDU 3715 Go Deeper
    poj1315
    poj1383
    poj1650
    poj1265
    poj1523
    RedHat9.0虚拟机安装
    注册DirectShow filter时应该注意中文路径
  • 原文地址:https://www.cnblogs.com/ch122633/p/8595395.html