"Disclaimer: All rights reserved, please indicate the source of the reprint, do not use for commercial purposes. Contact mailbox: [Email protected] "
Objective:
Stacks and queues are two very important data structures, from a data structure point of view, they are also linear tables, but they are different from the normal linear table, because we are limited to the stack and queue operation, because the stack and queue of importance and particularity, so the book is also a chapter of the space to introduce, So next, let's start with the most basic sequential stacks.
Note:
This article is only representative of Bo's own some superficial views, welcome everyone to comment on learning, together to create a good environment
For some questions, bloggers will try to answer for everyone, but if there are questions not answered in a timely manner, but also hope that other enthusiastic people to help solve, I appreciate it.
Sequential stacks
A stack is a linear table that restricts insertion or deletion at the end of a table. Therefore, for the stack, the end of the table has its special meaning, called the top of the stack, the head end is called the bottom of the stack (bottom). Empty tables that do not contain elements are called empty stacks.
Stack is also known as a linear table of last in, first out (LIFO).
1. Storage structure
Based on the definition of the stack, we want to make clear the stack top and the bottom of the pointer, and then we have to record the maximum capacity of this stack
typedef struct{ Selemtype *base; The stack-bottom pointer, before and after the stack is constructed, has a value of base null selemtype *top; stack top pointer int stacksize; The currently allocated storage space, in elements of} Sqstack; Sequential stacks
2. Empty stack construction
First of all, of course, allocate memory, then let the top and bottom pointers point to the same location, and finally allocate capacity to stacksize
Status Initstack (Sqstack *s) { //operation result: Constructs 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;}
3. Insert Delete
The insertion and deletion of the stack can only be done at the top of the stack, in the process of inserting the attention to determine whether the allocated capacity is exceeded, once the new capacity will be added. Delete must also be aware that the stack is empty at this point, and if it is empty, it cannot be deleted.
Tatus Push (Sqstack *s,selemtype e) { //operation result: Insert Element e is the new stack top element if ((*s). top-(*s). base>= (*s). stacksize) // Stack full, append storage { (*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 top element of S, return its value with E, and return OK; otherwise return error if ((*s). top== (*s). Base) return ERROR; *e = (* (--(*s). top)); return OK;}
4. Other Operations
Status Destroystack (Sqstack *s) { //action 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: Place S as empty stack (*s). Top = (*s). Base; return OK;} Status Stackempty (Sqstack s) { //Operation result: Returns TRUE if Stack S is an empty stack, otherwise returns false return s.top = = s.base;} int Stacklength (Sqstack S) { //Operation result: Returns the number of elements of S, that is, the length of the stack return s.top-s.base;} Status GetTop (sqstack s,selemtype *e) { //operation result: If the stack is not empty, then use E to return the stack top element of S and return OK; return error if (s.top>s.base) { *e = * (s.top-1); return OK; } return ERROR;} Status Stacktraverse (Sqstack s,status (*visit) (Selemtype)) { //Operation result: function visit () is called on each element of the stack from the bottom of the stack to the top of the stack. Once visit () fails, the operation fails while (s.top>s.base) visit (*s.base++); printf ("\ n"); return OK;}
5. Specific tests
#include "my.h"//contains all required header files # define stack_init_size 100#define stackincrement 10typedef int selemtype; Storage space initial allocation typedef int STATUS; Storage space allocation increment typedef struct{SELEMTYPE *base; The stack-bottom pointer, before and after the stack is constructed, has a value of base null Selemtype *top; stack top pointer int stacksize; The currently allocated storage space, in elements of} Sqstack; Sequential stack status Initstack (Sqstack *s) {//Operation result: Constructs 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) {//action 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: Place S as empty stack (*s). Top = (*s). Base; return OK;} Status Stackempty (Sqstack s) {//Operation result: Returns TRUE if Stack S is an empty stack, otherwise returns false return s.top = = S.base;} int Stacklength (Sqstack S) {//Operation result: Returns the number of elements of S, that is, the length of the stack return s.top-s.base;} Status GetTop (sqstack s,selemtype *e) {//Operation knotIf the stack is not empty, then 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 Push (Sqstack *s,selemtype e) {//Operation result: Insert Element e is the new stack top element if ((*s). top-(*s). base>= (*s). stacksize)//full stack, additional 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 top element of S, return its value with E, and return OK; return error if ((*s). top== (*s). Base) return ERR or; *e = (* (--(*s). top)); return OK;} Status Stacktraverse (Sqstack s,status (*visit) (Selemtype)) {//Operation result: function visit () is called on each element of the stack from the bottom of the stack to the top of 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 sequentially:"); Stacktraverse (S,visit); Pop (&s,&e); printf ("Pop-up stack top element e=%d\n", e); printf ("Stack null No:%d (1: Empty 0: NO) \ n", Stackempty (s)); GetTop (s,&e); printf ("Stack top element e=%d stack length is%d\n", e,stacklength (s)); Clearstack (&s); printf ("Empty stack, stack empty No:%d (1: Empty 0: NO) \ n", Stackempty (s)); Destroystack (&s); printf ("Destroy Stack, s.top=%u s.base=%u s.stacksize=%d\n", S.top,s.base, s.stacksize); return 0;}
Summarize:
Stack is a very useful data structure, in-depth understanding and learn to use the stack for the future is also very helpful, do not know whether you have learned useful knowledge?
The length of the sequence stack is very short, so it is over, and then we will implement the examples given in detail in the book to deepen our understanding and use of the stack.
Copyright NOTICE: This article is the original blogger article, if reproduced, please indicate the source
The way of data structure learning-Chapter III: Sequential Stacks