A stack is a linear table that can only be inserted or deleted at one end. (linear table with limited operation)
The stack can only insert or delete elements at the end of the table. The end of the table is the top of the stack, and the header is the bottom of the stack
Main features of stack: LIFO(last in first out) "last in first out"
The stack can adopt sequential storage structure (sequential stack) and chain storage structure (chain stack)
The following is the sequential stack implemented by sequential storage structure
Assuming that the maximum number of elements in the stack does not exceed the positive integer MAXSIZE, so the elements have the same data type (ElemType), use the following method to declare the type SQStack of the sequential stack:
#define MAXSIZE 128 typedef int ElemType; typedef struct { ElemType data[MAXSIZE];//Element value of storage stack int top;//Store the subscript of the top of stack element (top of stack pointer element) }SQStack;
There are four very important elements to a sequential stack
1. Stack empty condition: s.top==-1
2. Stack full condition: s.top==MAXSIZE-1
3. Stack operation: s.top=s.top+1;
s.data[s.top]=e;
or s.data[s.top+1]=e;
s.top=s.top+1;
4. Stack out operation: * e=s.data[s.top];
s.top=s.top-1;
or s.top=s.top-1;
*e=s.data[s.top+1];
1) Stack initialization
Create an empty stack with the top pointer pointing to - 1. The code is as follows:
SQStack InitStack() { SQStack s; s.top = -1; return s; }
Because the sequential stack is implemented by array, and the size of the array has been given, there is no need to apply for memory.
2) Destroy stack
There is no destruction operation for sequential storage, but we still provide the function of destroying stack for standardization, but there is no operation. The code is as follows:
/* There is no destroy operation for sequential storage */ void DestroyStack(SQStack *s) { }
3) Determine whether the stack is empty
To judge whether the stack is empty, you need to judge whether s.top is equal to - 1. The code is as follows:
int StackEmpty(SQStack s) { if(s.top == -1) { return 1; } else { return 0; } }
If you think the above code is too cumbersome, you can also write it like this:
int StackEmpty(SQStack s) { return (s.top == -1); }
4) push
Stack entry is also called stack entry. To perform this operation, you need to judge whether the stack is full to avoid stack overflow. The code is as follows:
/* Full: s.top==MAXSIZE-1 Stack operation: s.top=s.top+1; s.data[s.top]=e; If the stack is insufficient, perform relevant operations and return 1; Otherwise, prompt and return 0 */ int Push(SQStack *s,ElemType e) { if(s->top != MAXSIZE-1) { s->top = s->top + 1; s->data[s->top] = e; return 1; } else { return 0; //Stack overflow when stack is full } }
5) Pop
Stack exit is also called stack exit. To perform this operation, you need to first judge whether the stack is empty to avoid stack underflow. The code is as follows:
/* Stack out operation: *e=s.data[s.top]; s.top=s.top-1; If the stack is not empty, perform relevant operations and return 1; Otherwise, prompt and return 0 */ int Pop(SQStack *s,ElemType *e) { if(s->top != -1) { *e = s->data[s->top]; s->top = s->top - 1; return 1; } else { return 0; //Stack empty, i.e. stack underflow } }
6) Get stack top element
It is also necessary to determine whether the stack is empty to ensure the legitimacy of the operation. The code is as follows:
/* Operation of fetching stack top element: *e=s.data[s.top]; If the stack is not empty, perform relevant operations and return 1; Otherwise, prompt and return 0 */ int GetTop(SQStack s,ElemType *e) { if(s.top != -1) { *e = s.data[s.top]; s.top = s.top - 1; return 1; } else { return 0; } }
7) Output stack (from bottom to top)
/* From bottom to top of stack 0---s.top */ void display(SQStack s) { int i; printf("The elements of the stack are:"); for(i=0;i<=s.top;i++) { printf("%d ",s.data[i]); } printf("\n"); }
The following are the operations of entering and exiting the stack, and the code is as follows:
int main() { SQStack s; s = InitStack(); int i; for(i=1;i<=7;i++) { Push(&s,i); //Stack 1 -- 7 at a time } ElemType *e; Pop(&s,&e); //Push the top element out of the stack display(s); //Output stack elements (from bottom to top) return 1; }
The test results are as follows:
Finally, attach the complete code:
#include <stdio.h> #define MAXSIZE 128 typedef int ElemType; typedef struct { ElemType data[MAXSIZE];//Element value of storage stack int top;//Store the subscript of the top element of the stack }SQStack; /* Empty: s.top==-1 Full: s.top==MAXSIZE-1 Stack entry: s.top=s.top+1; s.data[s.top]=e; ----------------- s.data[s.top+1]=e; s.top=s.top+1; Out of stack: *e=s.data[s.top]; s.top=s.top-1; ----------------- s.top=s.top-1; *e=s.data[s.top+1]; Stack top element: *e=s.data[s.top]; Output: 0 --- s.top */ /* 1.initialization Empty: s.top==-1 */ SQStack InitStack() { SQStack s; s.top = -1; return s; } /* 2.Destroy There is no destroy operation for sequential storage */ void DestroyStack(SQStack *s) { } /* 3.Determine whether the stack is empty If it is empty, return 1; Otherwise, return 0 */ int StackEmpty(SQStack s) { return (s.top == -1); } /* 4.Enter the stack Full: s.top==MAXSIZE-1 s.top=s.top+1; s.data[s.top]=e; If the stack is insufficient, perform relevant operations and return 1; Otherwise, prompt and return 0 */ int Push(SQStack *s,ElemType e) { if(s->top != MAXSIZE-1) { s->top = s->top + 1; s->data[s->top] = e; return 1; } else { return 0; } } /* 5.Out of stack *e=s.data[s.top]; s.top=s.top-1; If the stack is not empty, perform relevant operations and return 1; Otherwise, prompt and return 0 */ int Pop(SQStack *s,ElemType *e) { if(s->top != -1) { *e = s->data[s->top]; s->top = s->top - 1; return 1; } else { return 0; } } /* 6.Get stack top element *e=s.data[s.top]; If the stack is not empty, perform relevant operations and return 1; Otherwise, prompt and return 0 */ int GetTop(SQStack s,ElemType *e) { if(s.top != -1) { *e = s.data[s.top]; s.top = s.top - 1; return 1; } else { return 0; } } /* 7.output From bottom to top of stack 0---s.top */ void display(SQStack s) { int i; printf("The elements of the stack are:"); for(i=0;i<=s.top;i++) { printf("%d ",s.data[i]); } printf("\n"); } int main() { SQStack s; s = InitStack(); int i; for(i=1;i<=7;i++) { Push(&s,i); //Stack 1 -- 7 at a time } ElemType *e; Pop(&s,&e); //Push the top element out of the stack display(s); //Output stack elements (from bottom to top) return 1; }