数据结构 堆栈

堆栈的抽象数据类型描述

堆栈(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

 

转载于:https://www.cnblogs.com/ch122633/p/8595395.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值