- 0
- 0
- 约8.86千字
- 约 46页
- 2017-08-03 发布于河南
- 举报
Chapter 3 Stacks and Queues;3.1 Stacks;3.1 Stacks; We have learned that it is a very heavy and complicated work to cope with the operations of middle nodes on a linear list, especially the insertion and deletion. But this problem can be predigested if we confine the operations to the head and tail on a linear list. Therefore, the limitative lists——stacks and queues are introduced. Unlike general linear list, stack and queue are two kinds of important ADT that their basic operations are subset of linear list’s. These two data structures are utilized in many cases, especially in dynamic lists.;3.1 Stacks;3.1.2 Terms;3.1.3 ADT;3.1.4 Characters of Stacks;3.1.5 Representation and Implementation; Given that an array s[M] supplied the space for stack, M is a constant given in advance, and an integer variable top also defined. In general, an empty stack means top=0, but in C language, we can appoint top the unused place above the top element——the position for new element. When we push an element, the operation input is prior to moving the pointer. Once top=M, the stack is full and any other request of PUSH will lead to overflow. In turn, when we pop an element, the pointer is moved firstly and then the value of element is returned. When top=0, the stack is empty and any other request of POP will lead to underflow.; Overflow may lead to the loss of information and have to avoid, but underflow is usually used as condition for program transfer or as the flag of ending a program.;#include stdio.h
#define M 10
typedef struct stack
{ int s[M], top; } ST;;3.1.6 Discussion About The Popped Sequence;Example 1:
Given a pushed sequence as 1, 2, 3 and 4. We may obtain the popped sequence 4321 if we push all of them and then pop all out, but the popped sequence may changed to 1234 if each element is pushed and popped immediately. And if 3th is popped immediately after its push and the 4th pushed, and then the rest are popped, we will get the sequence 3421.
However,
原创力文档

文档评论(0)