The path to Data Structure Learning-Chapter 3: The Path to Data Structure Learning
[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]
Preface:
Stack and queue are two very important data structures. From the perspective of data structure, they are also linear tables, but they are different from general linear tables, because we are restricted in stack and queue operations, because of the importance and particularity of stack and queue, the book also uses a chapter to introduce them. Next, let's start with the most basic sequence stack.
Note:
This article only represents some simple opinions of the bloggers themselves. You are welcome to review and learn to create a good environment together.
The bloggers will try to answer some questions as much as possible. However, if any questions are not answered in a timely manner, I hope other enthusiastic people can help solve them. I am very grateful.
Sequential Stack
Stack is a linear table that is limited to insert or delete operations at the end of a table. Therefore, for the stack, the end of the table has a special meaning, called the top of the stack, and the header is called the bottom of the stack ). Empty Tables without elements are called empty stacks.
Stack is also called a linear table of the last-in-first-out (LIFO.
1. Storage Structure
According to the stack definition, we need to define the pointer between the stack top and the stack bottom, and then we need to record the maximum capacity of the stack at this time.
Typedef struct {SElemType * base; // stack bottom pointer. The base value is NULL SElemType * top before and after stack construction and destruction; // The stack top pointer int stacksize; // currently allocated storage space, in the unit of elements} SqStack; // sequential Stack
2. Empty stack Construction
First of all, we should allocate the memory, then let the top and bottom of the stack point to the same position, and finally allocate the capacity to stacksize.
Status InitStack (SqStack * S) {// operation result: An empty stack S (* S) is constructed ). base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof (SElemType); if (! (* S ). base) exit (OVERFLOW); // storage allocation failed (* S ). top = (* S ). base; (* S ). stacksize = STACK_INIT_SIZE; return OK ;}
3. insert and delete
Stack insertion and deletion can only be performed at the top of the stack. During the insertion process, you must determine whether the size has exceeded the allocated capacity. Once the size exceeded, you must increase the new capacity. You must also check whether the stack is empty at this time. If it is empty, the stack cannot be deleted.
Tatus Push (SqStack * S, SElemType e) {// Operation Result: insert element e as the new stack top element if (* S ). top-(* S ). base> = (* S ). stacksize) // stack full, append storage space {(* S ). base = (SElemType *) realloc (* S ). base, (* S ). stacksize + STACKINCREMENT) * sizeof (SElemType); if (! (* S ). base) exit (OVERFLOW); // storage allocation failed (* S ). top = (* S ). base + (* S ). stacksize; (* S ). stacksize + = STACKINCREMENT;} * (* S ). top ++) = e; return OK;} Status Pop (SqStack * S, SElemType * e) {// Operation Result: If the stack is not empty, delete the stack top element of S, return the value with e and OK; otherwise, ERROR if (* S) is returned ). top = (* S ). base) return ERROR; * e = (* (-- (* S ). top); return OK ;}
4. Other operations
Status DestroyStack (SqStack * S) {// operation result: Destroy stack S, S no longer exists free (* S ). base); (* S ). base = NULL; (* S ). top = NULL; (* S ). stacksize = 0; return OK;} Status ClearStack (SqStack * S) {// operation result: Set S to empty stack (* S ). top = (* S ). base; return OK;} Status StackEmpty (SqStack S) {// Operation Result: if Stack S is empty, TRUE is returned; otherwise, FALSE return S is returned. top = S. base;} int StackLength (SqStack S) {// Operation Result: returns the number of elements of S, that is, the stack length return S. top-S.base;} Status GetTop (SqStack S, SElemTyp E * e) {// Operation Result: if the stack is not empty, use e to return the top element of S and Return OK; otherwise, return ERROR if (S. top> S. base) {* e = * (S. top-1); return OK;} return ERROR;} Status StackTraverse (SqStack S, Status (* visit) (SElemType) {// operation result: from the bottom of the stack to the top of the stack, visit () is called for each element in the stack (). Once visit () fails, the operation fails while (S. top> S. base) visit (* S. base ++); printf ("\ n"); return OK ;}
5. Test
# Include "my. h "// contains all required header files # define STACK_INIT_SIZE 100 # define STACKINCREMENT 10 typedef int SElemType; // the initial distribution of the bucket typedef int Status; // bucket allocation incremental typedef struct {SElemType * base; // stack bottom pointer. The base value is NULL SElemType * top before and after stack construction; // stack top pointer int stacksize; // currently allocated storage space, in element units} SqStack; // sequential stack Status InitStack (SqStack * S) {// operation result: Construct an empty stack S (* S ). base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof (SElemType); if (! (* S ). base) exit (OVERFLOW); // storage allocation failed (* S ). top = (* S ). base; (* S ). stacksize = STACK_INIT_SIZE; return OK;} Status DestroyStack (SqStack * S) {// operation result: Destroy stack S, S no longer exists free (* S ). base); (* S ). base = NULL; (* S ). top = NULL; (* S ). stacksize = 0; return OK;} Status ClearStack (SqStack * S) {// operation result: Set S to empty stack (* S ). top = (* S ). base; return OK;} Status StackEmpty (SqStack S) {// Operation Result: if Stack S is empty, TRUE is returned; otherwise, FALSE return S is returned. top = S. base ;} Int StackLength (SqStack S) {// Operation Result: returns the number of elements of S, that is, the stack length return S. top-S.base;} Status GetTop (SqStack S, SElemType * e) {// Operation Result: If the stack is not empty, use e to return the stack top element of S and Return OK; otherwise, ERROR if (S. top> S. base) {* e = * (S. top-1); return OK;} return ERROR;} Status Push (SqStack * S, SElemType e) {// operation result: insert element e as the new stack top element if (* S ). top-(* S ). base> = (* S ). stacksize) // stack full, append storage space {(* S ). base = (SElemType *) realloc (* S ). base, (* S ). stacksize + STACKINCREMENT) * siz Eof (SElemType); if (! (* S ). base) exit (OVERFLOW); // storage allocation failed (* S ). top = (* S ). base + (* S ). stacksize; (* S ). stacksize + = STACKINCREMENT;} * (* S ). top ++) = e; return OK;} Status Pop (SqStack * S, SElemType * e) {// Operation Result: If the stack is not empty, delete the stack top element of S, return the value with e and OK; otherwise, ERROR if (* S) is returned ). top = (* S ). base) return ERROR; * e = (* (-- (* S ). top); return OK;} Status StackTraverse (SqStack S, Status (* visit) (SElemType) {// operation result: from the bottom of the stack to the top of the stack, visit () is called for each element in the stack (). Once visit () fails, the operation fails while (S. top> S. base) visit (* S. base ++); printf ("\ n"); return OK;} Status visit (SElemType c) {printf ("% d", c); return OK ;} int main () {int j; SqStack s; SElemType e; if (InitStack (& s) = OK) for (j = 1; j <= 12; j ++) push (& s, j); printf ("the elements in the stack are:"); StackTraverse (s, visit); Pop (& s, & e ); printf ("pop-up top stack Element e = % d \ n", e); printf ("Stack null No: % d (1: NULL 0: No) \ n ", stackEmpty (s); GetTop (s, & e); printf ("Stack top Element e = % d stack length: % d \ n", e, stackLength (s); ClearStack (& s); printf ("Empty Stack: % d (1: Empty 0: No) \ n ", stackEmpty (s); DestroyStack (& s); printf ("after the stack is destroyed, s. top = % u s. base = % u s. stacksize = % d \ n ", s. top, s. base, s. stacksize); return 0 ;}
Summary:
Stack is a very useful data structure. In-depth understanding and learning to use the stack is also very helpful in the future. Do you know if you have learned useful knowledge?
The length of the stack sequence is very short. Now it is over. Next, we will detail the instances provided in books to deepen our understanding and use of the stack.
Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source