Sequential stack
1. Concept
The stack is one kind 仅在表尾进行插入或者删除操作的线性表
. And the beginning and the end of the table have special meanings: the head of the table is generally called 栈底
, the end of the table (the position of the last element) is called 栈顶
, and 栈底=栈顶
the stack without elements or is called the stack 空栈
.
Therefore, the stack is a 先进后出
table structure. This situation is very similar to our train dispatching station.
2. Stack structure definition
Because the internal storage structure of the stack can use sequential structure or chain structure, but the storage method in this part is similar to the previous linear list and singly linked list, so this article adopts the sequential storage method to achieve.
1. Stack definition
For a stack, we have to define three parts, one is to base
store our data as our basic "container", and then we have to have a top
stack top pointer to point to our current stack top element, because it is a sequence table, so We also have to have a maximum length maxlen
to store base
, so as not to cross the boundary. top
It can be a reshaping base+top
method to get the pointer to the top of the stack. Of course, you can also directly top
declare it as the base
same class as the one and store the pointer at the top of the stack. Because it is originally a sequence table, the simpler the better, so it is stored in a plastic way.
2. Initialize the stack
For the initialization of the stack, we only need to apply for a space with a default length, assign it to it base
, and then maxlen
update to the length we initialized, and finally top=0
, it is pointed base
.
3. Destroy the stack
free
Drop us directlybase
4. Is the stack empty
We directly judgetop==0
5. Stack length
Because it is a sequential storage structure, there is no need to traverse the entire table and return directly top
. The top pointer of the stack is the length of our stack.
6. The top element of the stack
If the stack is not empty, then the returned *(base+top)
value will do
7. Push into the stack
We only need to update the pointer position at the top of the stack and assign the value we want to push, so that the element is pushed onto the stack.
Of course, we have to judge whether base
the storage space is enough or not realloc base
. According to my habit, I like to check and expand after being put into the stack, and be ready for the master.
8. Pop
Return the value of the address pointed to by the current stack pointer, and then add 栈顶指针-1
.
Three. Code implementation
As long as you know the principle, the implementation is relatively simple, so there is no need to add comments.
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define DEFAULT_LEN 20
#define EXTEND_LEN 5
typedef struct elemType{
int data;
}elemType;
typedef struct Stack{
elemType *base;
int top;
int maxlen;
}Stack;
Stack* StackInit(void);
int GetTop(Stack *S, elemType *elem);
int Push(Stack* S, elemType elem);
elemType Pop(Stack* S);
int IsEmpty(Stack *S);
int main()
{
Stack* s = StackInit();
elemType test;
test.data = 123;
Push(s,test);
test.data = 456;
Push(s,test);
test.data = 789;
Push(s,test);
while(IsEmpty(s) == 0)
{
printf("%d\n", Pop(s).data);
}
return 0;
}
Stack* StackInit(void)
{
Stack *S = (Stack*)malloc(sizeof(Stack));
if (S == NULL)
{
return ERROR;
}
S->base = (elemType *)malloc(sizeof(elemType)*DEFAULT_LEN);
S->top = 0;
S->maxlen = DEFAULT_LEN;
if (S->base != NULL)
{
return S;
}
else
{
exit(1);
}
}
int GetTop(Stack *S, elemType *elem)
{
if (S->base != NULL && S->top != 0)
{
elem = S->base + S->top;
return OK;
}
else
{
return ERROR;
}
}
int Push(Stack *S, elemType elem)
{
if (S->top < S->maxlen)
{
S->top += 1;
*(S->base+S->top) = elem;
if (S->top >= S->maxlen)
{
S->base = (elemType*)realloc(S->base, sizeof(elemType)*(S->maxlen + EXTEND_LEN));
if (S->base != NULL)
{
return OK;
}
else
{
exit(1);
}
}
}
else
{
return ERROR;
}
}
elemType Pop(Stack* S)
{
elemType *elem;
if (S->top > 0)
{
elem = S->base+S->top;
S->top -= 1;
return *elem;
}
else
{
exit(2);
}
}
int IsEmpty(Stack *S)
{
if (S->top > 0)
{
return 0;
}
else
{
return 1;
}
}