define STACKINITSIZE 100 define STACKINCREMENT 10 typedef struct

  • Slides: 58
Download presentation

– 顺序栈的存储 # define STACK_INIT_SIZE 100; //存储空间初始分配量 # define STACKINCREMENT 10; // 存储空间分配增量 typedef

– 顺序栈的存储 # define STACK_INIT_SIZE 100; //存储空间初始分配量 # define STACKINCREMENT 10; // 存储空间分配增量 typedef struct { SElem. Type * base; //在栈构造之 前和销毁之后, base的值为NULL SElem. Type * top; // 栈顶指针 Int stacksize; //当前已分配的 存储空间,以元素为单位 }Sq. Stack; 数据结构 Sq. Stacksize top base SElem. Type

4. 栈的主要算法 (1) 构造空顺序栈算法 Status Init. Stack ( Sq. Stack &S ) { //

4. 栈的主要算法 (1) 构造空顺序栈算法 Status Init. Stack ( Sq. Stack &S ) { // 构造一个空栈 S S. base = ( SElem. Type * ) malloc ( STACK_INIT_SIZE * sizeof ( SElem. Type ) ); if ( ! S. base ) exit ( OVERFLOW ); // 为栈分配存储空间 失败 S. top = S. base; // 令栈顶指针 = 栈底指针 S. stacksize = STACK_INIT_SIZE; // 设置栈的当前可使 用的最大容量 return OK; } // Init. Stack 数据结构

(4) 将元素压入顺序栈算法 Status Push ( Sq. Stack &S, SElem. Type e ) { //

(4) 将元素压入顺序栈算法 Status Push ( Sq. Stack &S, SElem. Type e ) { // 将元素 e 插入到栈 S 中,成为新的栈顶元素 if ( S. top - S. base > S. stacksize ) { // 如果栈满,则追加存储空间 S. base = ( SElem. Type * ) realloc ( S. base, ( S. stacksixe + STACKINCREMENT* sizeof ( SElem. Type ) ); if ( ! S. base ) exit ( OVERFLOW ); // 追加存储空间失败 S. top = S. base + S. stacksize; // 修改栈顶指针 S. stacksize += STACKINCREMENT; // 修改当前栈的存储空间 } *S. top ++= e; // 先将 e 送入栈顶,然后将栈顶指针加 1 return OK; } // Push 数据结构

队列的进队和出队示例 A front rear 空队列 A B front rear A进队 A B C D

队列的进队和出队示例 A front rear 空队列 A B front rear A进队 A B C D front rear B进队 front rear C, D进队 B C D front rear A退队 C D front rear B退队 C D E F G front rear E, F, G进队 C D E F G front rear 数据结构 H进队, 溢出

队列的链式存储结构 Typedef struct QNode{ QElem. Type data; struct Qnode *next; }Qnond, *Queue. Ptr; Typedef

队列的链式存储结构 Typedef struct QNode{ QElem. Type data; struct Qnode *next; }Qnond, *Queue. Ptr; Typedef struct{ Queue. Ptr front; Queue. Ptr rear; }Link. Queue; Queue. Ptr 数据结构 QNode data next 单链队列的结点类型 Link. Queue front rear Link. Queue类型

空队列 Q. front Q. rear Q. front ∧ 元素X入队列 X Q. rear Q. front

空队列 Q. front Q. rear Q. front ∧ 元素X入队列 X Q. rear Q. front ∧ 元素Y入队列 X Y ∧ Q. rear Q. front Q. rear 元素X出队列 数据结构

front rear 队空:front==rear 队满:front==rear J 5 J 4 4 5 0 J 6 3

front rear 队空:front==rear 队满:front==rear J 5 J 4 4 5 0 J 6 3 2 1 front 4 5 0 3 2 1 rear 出队 6 J , 5 J , J 4 J 7, J 8 , J 9入 队 J 5 J 4 4 5 0 J 6 3 2 1 J 7 J 9 初始状态 解决方案: 1. 另外设一个标志以区别队空、队满 2. 少用一个元素空间: 队空:front==rear 队满:(rear+1)%M==front 数据结构 front rear J 8

课堂练习 (2) void Demo 2( Seq. Stack *S, int m) {// 设Data. Type 为int

课堂练习 (2) void Demo 2( Seq. Stack *S, int m) {// 设Data. Type 为int 型 Seq. Stack T; int i; Init. Stack (&T); while (! Stack. Empty( S)) if(( i=Pop(S)) !=m) Push( &T, i); while (! Stack. Empty( &T)) { i=Pop(&T); Push(S, i); } } 数据结构

(1) Seq. Stack S 1, S 2, tmp; Data. Type x; . . .

(1) Seq. Stack S 1, S 2, tmp; Data. Type x; . . . //假设栈tmp和S 2已做过初始化 while ( ! Stack. Empty (&S 1)) { x=Pop(&S 1) ; Push(&tmp, x); } while ( ! Stack. Empty (&tmp) ) { x=Pop( &tmp); Push( &S 1, x); Push( &S 2, x); } 数据结构

(2)void Demo 3( Cir. Queue *Q) {// 设Data. Type 为int 型 int x; Seq.

(2)void Demo 3( Cir. Queue *Q) {// 设Data. Type 为int 型 int x; Seq. Stack S; Init. Stack( &S); while (! Queue. Empty( Q )) { x=De. Queue( Q); Push( &S, x); } while (! Stack. Empty( &s)) { x=Pop(&S); En. Queue( Q, x ); } }// Demo 3 数据结构 (3) Cir. Queue Q 1, Q 2; // 设 Data. Type 为int 型 int x, i , m = 0; . . . // 设Q 1已有内容, Q 2已初始化过 while ( ! Queue. Empty( &Q 1) ) { x=De. Queue( &Q 1 ) ; En. Queue(&Q 2, x); m++; } for (i=0; i< m; i++) { x=De. Queue(&Q 2) ; En. Queue( &Q 1, x) ; En. Queue( &Q 2, x); }