Init StackS Destroy StackS Stack LengthS Stack Emptys
- Slides: 71
Init. Stack(&S) Destroy. Stack(&S) Stack. Length(S) Stack. Empty(s) Get. Top(S, &e) Clear. Stack(&S) Push(&S, e) Pop(&S, &e) Stack. Travers(S, visit())
void conversion () { Init. Stack(S); scanf ("%d", N); while (N) { Push(S, N % 8); N = N/8; } while (!Stack. Empty(S)) { Pop(S, e); printf ( "%d", e ); } } // conversion
Status matching(string& exp) { int state = 1; while (i<=Length(exp) && state) { switch of exp[i] { case 左括弧: {Push(S, exp[i]); i++; break; } case”)”: { if(NOT Stack. Empty(S)&&Get. Top(S)=“(“ {Pop(S, e); ii++; } else {state = 0; } break; } … … } if (Stack. Empty(S)&&state) return OK; …. . .
while (ch != EOF) { //EOF为全文结束符 while (ch != EOF && ch != 'n') { switch (ch) { case '#' : Pop(S, c); break; case '@': Clear. Stack(S); break; // 重置S为空栈 default : Push(S, ch); break; } ch = getchar(); // 从终端接收下一个字符 } 将从栈底到栈顶的字符传送至调用过程的 数据区; Clear. Stack(S); // 重置S为空栈 if (ch != EOF) ch = getchar();
void transform(char suffix[ ], char exp[ ] ) { Init. Stack(S); Push(S, # ); p = exp; ch = *p; while (!Stack. Empty(S)) { if (!IN(ch, OP)) Pass( Suffix, ch); else { … … } if ( ch!= # ) { p++; ch = *p; } else { Pop(S, ch); Pass(Suffix, ch); } } // while } // Crt. Exptree
switch (ch) { case ( : Push(S, ch); break; case ) : Pop(S, c); while (c!= ( ) { Pass( Suffix, c); Pop(S, c) } break; defult : while(Gettop(S, c) && ( precede(c, ch))) { Pass( Suffix, c); Pop(S, c); } if ( ch!= # ) Push( S, ch); break; } // switch
void hanoi (int n, char x, char y, char z) { 1 if (n==1) 2 move(x, 1, z); 3 else { 4 hanoi(n-1, x, z, y); 5 move(x, n, z); 6 hanoi(n-1, y, x, z); 7 } 8} 57 5 8 1 2 3 返址 n ac a a x ba c b y bc b c z
类似于线性表的顺序映象实现, 指向表尾的指针可以作为栈顶指针。 //----- 栈的顺序存储表示 ----#define STACK_INIT_SIZE 100; #define STACKINCREMENT 10; typedef struct { SElem. Type *base; SElem. Type *top; int stacksize; } Sq. Stack;
Status Init. Stack (Sq. Stack &S) {// 构造一个空栈S S. base=(Elem. Type*)malloc(STACK_INIT_SIZE* sizeof(Elem. Type)); if (!S. base) exit (OVERFLOW); //存储分配失败 S. top = S. base; S. stacksize = STACK_INIT_SIZE; return OK; }
Status Push (Sq. Stack &S, SElem. Type e) { if (S. top - S. base >= S. stacksize) {//栈满,追加存储空间 S. base = (Elem. Type *) realloc ( S. base, (S. stacksize + STACKINCREMENT) * sizeof (Elem. Type)); if (!S. base) exit (OVERFLOW); //存储分配失败 S. top = S. base + S. stacksize; S. stacksize += STACKINCREMENT; } *S. top++ = e; return OK; }
Status Pop (Sq. Stack &S, SElem. Type &e) { // 若栈不空,则删除S的栈顶元素, // 用e返回其值,并返回OK; // 否则返回ERROR if (S. top == S. base) return ERROR; e = *--S. top; return OK; }
队列的基本操作: Init. Queue(&Q) Destroy. Queue(&Q) Queue. Empty(Q) Queue. Length(Q) Get. Head(Q, &e) Clear. Queue(&Q) En. Queue(&Q, e) De. Queue(&Q, &e) Queue. Travers(Q, visit())
链队列——链式映象 typedef struct QNode {// 结点类型 QElem. Type data; struct QNode *next; } QNode, *Queue. Ptr;
typedef struct { // 链队列类型 Queue. Ptr front; // 队头指针 Queue. Ptr rear; // 队尾指针 } Link. Queue; 空队列 … a 1 Q. front Q. rear ∧ an ∧
Status Init. Queue (Link. Queue &Q) { // 构造一个空队列Q Q. front = Q. rear = (Queue. Ptr)malloc(sizeof(QNode)); if (!Q. front) exit (OVERFLOW); //存储分配失败 Q. front->next = NULL; return OK; }
Status En. Queue (Link. Queue &Q, QElem. Type e) { // 插入元素e为Q的新的队尾元素 p = (Queue. Ptr) malloc (sizeof (QNode)); if (!p) exit (OVERFLOW); //存储分配失败 p->data = e; p->next = NULL; Q. rear->next = p; Q. rear = p; return OK; }
Status De. Queue (Link. Queue &Q, QElem. Type &e) { // 若队列不空,则删除Q的队头元素, //用 e 返回其值,并返回OK;否则返回ERROR if (Q. front == Q. rear) return ERROR; p = Q. front->next; e = p->data; Q. front->next = p->next; if (Q. rear == p) Q. rear = Q. front; free (p); return OK; }
Status Init. Queue (Sq. Queue &Q) { // 构造一个空队列Q Q. base = (Elem. Type *) malloc (MAXQSIZE *sizeof (Elem. Type)); if (!Q. base) exit (OVERFLOW); // 存储分配失败 Q. front = Q. rear = 0; return OK; }
Status En. Queue (Sq. Queue &Q, Elem. Type e) { // 插入元素e为Q的新的队尾元素 if ((Q. rear+1) % MAXQSIZE == Q. front) return ERROR; //队列满 Q. base[Q. rear] = e; Q. rear = (Q. rear+1) % MAXQSIZE; return OK; }
Status De. Queue (Sq. Queue &Q, Elem. Type &e) { // 若队列不空,则删除Q的队头元素, // 用e返回其值,并返回OK; 否则返回ERROR if (Q. front == Q. rear) return ERROR; e = Q. base[Q. front]; Q. front = (Q. front+1) % MAXQSIZE; return OK; }
利用循环队列计算二项式的过程: 假设只计算四行,则队列的最大容量为 5。 0 1 1 0 q. front 0 1 1 0 1 q. rear 2 1 0 1 1 q. rear q. front 2 1 0 0 1 q. rear q. front 2 1 1 0 1 q. rear q. front
do { 2 1 0 1 1 q. rear q. front De. Queue(Q, s); Get. Head(Q, e); if (e!=0) printf (“%d”, e); 2 1 0 1 3 q. rear q. front 3 1 0 1 3 q. rear q. front En. Queue(Q, s+e); } while (e!=0); 3 1 0 1 1 q. rear q. front
- Grafcet init
- Glut init
- Myactity
- Wireshark
- Failed to init ageia physics sdk
- Init seven
- Glut init
- Stack characteristics
- Prinsip stack
- Stack smashing vs buffer overflow
- Stacks internet
- Five ocean basins
- Data structures using java
- 6 stacks
- Wave cut platform diagram
- Java stack exercises
- Java stacks and queues
- Speed stacks spreads nationally in 1998.
- 2 stack pushdown automata
- Angle stacks
- What are stacks
- Types of stacks
- .
- Empty python
- Stacks+routined
- Xm destroy oracle vm
- John 10 10 nkjv
- Destroy the family
- To pull down to root out
- Marigolds author
- "missionaries destroy culture"
- How do people destroy natural resources
- Passive in present perfect continuous
- The devil seeks to steal kill and destroy
- Verb 3 of repair
- Drinks destroy teeth
- Affected ignorance example
- How do cfcs destroy ozone
- Bitterness will destroy you
- Sex boutique london
- Compound shapes area and perimeter
- Measuring segments
- Arc lengths maze
- Area of sector formula
- Chapter 10 properties of circles answers
- Converting metric lengths
- Rstu is a kite, , and . find .
- Angle measures and segment lengths
- Year 3 subtracting lengths
- Proportional lengths section 7-6
- Doublet lengths to avoid
- 9-7 circles and lengths of segments worksheet answers
- Special segments in circles
- 11-6 arc lengths and areas of sectors answer key
- Spectrum of light waves
- To estimate the length of a lake caleb
- Segment lengths in circles formulas
- How to find segment lengths in circles
- Eurocode reinforcement detailing
- Proportional lengths section 7-6
- How to know if its a right triangle based on side lengths
- The lengths chords
- 12-6 segment relationships in circles
- Cosine formula
- Finding segment lengths find the value of x
- Using algebra with segment lengths
- Hpe azure stack
- Stack pointer nedir
- Aer strategy grid
- Fit patrick scale
- Smashing stack for fun and profit
- Network stack adalah