栈及其基本运算

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Houfeng Wang, ICL of PKU 3
栈又称为后进先出表(Last In First Out表, 简称LIFO表)或下推表
出栈 进栈
栈的特点 后进先出
栈顶
an-1
第一个进栈的元素在栈底, 最后一个进栈的元素在栈顶, 第一个出栈的元素为栈顶元素, 最后一个出栈的元素为栈底元素
栈底
a2 a1
Houfeng Wang, ICL of PKU
9
栈的溢出
当栈中已经有 MAXNUM个元素时,如果再作进 栈运算,则会产生溢出,称为上溢(Overflow); 而对空栈进行出栈运算时也会产生溢出,通常称 为下溢(Underflow).
Houfeng Wang, ICL of PKU
10
基本运算:创建一个空栈
Houfeng Wang, ICL of PKU 11
pastack
创建空栈
s
t
Houfeng Wang, ICL of PKU 12
基本运算:判断栈是否为空栈
2. int isEmptyStack_seq( PSeqStack pastack )
当pastack所指的栈为空栈时,则返回1,否则返回0
栈的实现
栈的顺序存储和实现 栈的链式存储和实现
Houfeng Wang, ICL of PKU
7
顺序表示
在定义上与一般线性表有何区别?
#define MAXNUM 100 /* 栈的最大容量*/ typedef int DataType; /* 栈元素的数据类型* / struct SeqStack int }; typedef struct SeqStack *PSeqStack; PSeqStack pastack; /*指向顺序栈的指针变量*/
Houfeng Wang, ICL of PKU 24
5. DataType top_link( PLinkStack plstack ) 当plstack所指的栈不空时,取栈顶元素的值, 栈保持不变
DataType top_link( PLinkStack plstack ) /* 对非空栈求栈顶元素 */ { return (plstack->top->info); }
递归的基本概念
自身的简单情况来定义自己的方式,称为递归 定义. 例: 1 n=0 n!= n*(n-1)! N>0
– 在n阶乘的定义中,当n为0时定义为1,它不再用递 归来定义,称为递归定义的出口,简称为递归出口.
Houfeng Wang, ICL of PKU
27
阶乘的递归计算
int fact( int n ) { if ( n == 0 ) return 1; else return ( n * fact( n – 1 ) ); }
栈的示意图
Houfeng Wang, ICL of PKU 4
栈的应用 函数嵌套调用时,按照"后调 用先返回"的原则进行. int main() { int m,n; ... first(m,n); 1: … } int first(int s, int t) { int i; … second(i); 2: … }
/* 当pastack所指的栈不为空栈时,求栈顶元素的值 */
{ return (pastack->s[pastack->t]); }
Houfeng Wang, ICL of PKU
16
栈的链式结构表示
plstack top info
ki+2 ki+1 ki
link
k0
Houfeng Wang, ICL of PKU
Houfeng Wang, ICL of PKU 14
基本运算:出栈
4. void pop_seq( PSeqStack pastack ) 出栈运算,表示从pastack所指的栈中删除(或称 弹出)一个元素. void pop_seq( PSeqStack pastack ) // 删除栈顶元素 { if (pastack->t == -1 ) printf( "Underflow!\n" ); else pastack->t = pastack->t - 1; }
判断栈st是否为空栈;
3. void push(Stack st, DataType x)
往栈中插入(或称推入)一个值为x的元素;
4. void pop(Stack st)
从栈中删除(或称弹出)一个元素;
5. DataTYpe top(Stack st)
求栈顶元素的值.
Houfeng Wang, ICL of PKU 6
Houfeng Wang, ICL of PKU 23
4. void pop_link( PLinkStack plstack ) 出栈运算,表示从plstack所指的栈中删除(或称弹 出)一个元素. 当栈不空时,直接修改栈顶指针,删除结点.
void pop_link( PLinkStack plstack ) { PNode p; if( isEmptyStack_link( plstack ) ) printf( "Empty stack pop.\n" ); else { p = plstack->top; plstack->top = plstack->top->link; free(p); } }
int second(int d) { int x,y; 3: …
}
Houfeng Wang, ICL of PKU
5
栈的基本运算
Stack st; DataType x; 1. Stack createEmptyStack(void)
创建一个空栈;
2. int isEmptyStack(Stack st)
Houfeng Wang, ICL of PKU 25
栈的应用*
栈与递归
– 递归功能最终是由栈实现的,如果程序设计 语言(如 C语言)提供了递归功能,那么, 该功能最终由编译器用栈实现. – 如果程序设计语言不具备递归功能,如何用 栈实现递归?
用栈实现表达式计算
Houfeng Wang, ICL of PKU 26

17
Fra Baidu bibliotek
栈的链接表示
栈的链接表示:结构定义
struct Node; // 单链表结点 typedef struct Node *PNode; // 指向结点的指针类型 struct Node // 单链表结点结构 { DataType info; PNode link; };
Houfeng Wang, ICL of PKU
18
栈结构封装
引入LinkStack:
struct LinkStack // 链接栈类型定义 { PNode top; // 指向栈顶结点 }; typedef struct LinkStack *PLinkStack;
/* 链接栈类型的指针类型 */
Houfeng Wang, ICL of PKU
19
假设plstack是PLinkStack类型的变量,则plstack->top 就是栈顶指针,plstack->top->info是栈顶元素, plstack top info
ki+2 ki+1 ki
link
k0
Houfeng Wang, ICL of PKU

20
链接表示 de 基本运算
1. PLinkStack createEmptyStack_link(void) 申请链栈结构空间,创建一空链接栈,返回指向空链接 栈的指针. PLinkStack createEmptyStack_link(void) { PLinkStack plstack; plstack = (PLinkStack )malloc( sizeof(struct LinkStack)); if (plstack != NULL) plstack->top = NULL; else printf("Out of space! \n"); return (plstack); }
Houfeng Wang, ICL of PKU 8
/* 顺序栈类型定义 */ /*栈顶*/
{ DataType s[MAXNUM]; t;
push(st, ′A′); push(st, ′B′) ; push(st, ′C ′); pop(st); C B A pop(st); pop(st);
Houfeng Wang, ICL of PKU 21
2. int isEmptyStack_link( PLinkStack plstack ) 判断plstack所指的栈是否为空栈,当plstack所 指的栈为空栈时,则返回1,否则返回0. int isEmptyStack_link( PLinkStack plstack ) { return (plstack->top == NULL); }
1. PSeqStack createEmptyStack_seq( void )
为栈结构申请空间,并将栈顶变量赋值为-1
PSeqStack createEmptyStack_seq( void ) { PSeqStack pastack; pastack = (PSeqStack)malloc(sizeof(struct SeqStack)) if (pastack==NULL) printf("Out of space!! \n"); else pastack->t=-1; //栈顶指针总是指向顶部元素 return (pastack); }
int isEmptyStack_seq( PSeqStack pastack ) { return ( pastack->t == -1 ); }
Houfeng Wang, ICL of PKU
13
基本运算:进栈
3. void push_seq( PSeqStack pastack, DataType x ) 往pastack所指的栈中插入(推入)一个值为x的元素. 当栈不满时,栈顶指针先加1,然后把元素x放入栈顶变 量所指的位置中. void push_seq( PSeqStack pastack, DataType x ) { if( pastack->t >= MAXNUM - 1 ) printf( "Overflow! \n" ); else { pastack->t = pastack->t + 1; pastack->s[pastack->t] = x; } }
Houfeng Wang, ICL of PKU 15
基本运算:取栈顶元素
5. DataType top_seq( PSeqStack pastack ) 取栈顶元素运算,当pastack所指的栈不为空栈时, 将栈顶元素取出,而栈本身未发生任何变化. DataType top_seq( PSeqStack pastack )
栈及其基本运算
top
E D C B
top top
F E D C B
bottom
A
bottom
Houfeng Wang, ICL of PKU
A
1

栈的基本概念 栈的顺序存储和实现 栈的链式存储和实现 栈的应用 —— 递归
Houfeng Wang, ICL of PKU
2
栈的基本概念
栈:一种特殊的线性表,它所有的插入和删 除都限制在表的同一端进行. 栈顶:表中允许进行插入,删除操作的一端 叫做栈的栈顶,栈顶的当前位置是动态变化 的,常用一个称为栈顶指针的变量来标识. 栈底:表的另一端则叫做栈的底. 空栈:当栈中没有元素时,称之为空栈. 进栈/出栈栈的插入运算通常称为进栈或入 栈,栈的删除运算通常称为退栈或出栈
Houfeng Wang, ICL of PKU
22
3.
void push_link( PLinkStack plstack, DataType x ) 往plstack所指的栈中插入(或称推入)一个值为x的元 素.首先申请结点空间,然后通过指针修改,将结点插 在栈顶指针后.
void push_link( PLinkStack plstack, DataType x ) { PNode p; p = (PNode)malloc( sizeof( struct Node ) ); if ( p == NULL ) printf("Out of space!\n"); else { p->info = x; p->link = plstack->top; plstack->top = p; } }
相关文档
最新文档