文档文本内容
栈
(stack)
www.cskaoyan.com 王道论坛stack:a pile of objects 翻译:一垛物品
维基百科: 堆栈(英语:stack)又称为栈
或堆叠,先进后出(First In Last Out)
FILO栈:只允许在一端进行插入或删除操作的线性表
栈顶( )
Top栈的基本操作
A1 A2 A3 A4 An A A1 A2 A3 A4 An
n+1
栈顶
栈底 栈顶
栈底
元素入栈
元素出栈顺序存储实现栈
typedef struct{
Elemtype data[50];
int top;
}SqStack;
SqStack S;
1 2 3 4
0 1 2 3 4 5 6 7
S.top
S. data[++S. top]=4;
元素入栈1 2 3 4
0 1 2 3 4 5 6 7
S.top
x=S. dat a[S. t op--]
元素出栈0 1 2 3 4 5 6 7
S.top
S. t op=-1时栈为空1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
S.top
S. t op等于MaxSi ze-1时
栈满链式存储实现栈(相对不重要,考的概率极低)
LiStack Lhead=(LiStack)malloc(sizeof(struct Linknode))
Lhead->next=NULL;
LiStack top=NULL;
Lhead 1 ^
top=(LiStack)malloc(sizeof(struct Linknode));
top->next=NULL;
top->data=1;
top->next=Lhead->next;
Lhead->next=top;元素出栈
Lhead 3 2 1 ^
c=top->data;
Lhead->next=top->next;
c:3
free(top);
top=Lhead->next;栈空栈满
Lhead ^
Lhead->next == NULL 为真,则栈为空
只要存在剩余内存,那么栈就可以继续添加元素