Introduction to stack:
Stack is a linear list or linked list that can only be inserted or deleted at one end. The end of the list that allows insertion and deletion is called the top of the stack, and the other end of the table is called the bottom of the stack
When there is no element in the stack, it is called an empty stack. The insertion of the stack is usually called entering the stack or entering the stack, and the deletion of the stack is usually called leaving the stack or leaving the stack.
Stack features:
The stack is characterized by last in first out (LIFO), that is, last in first out, and the later elements come out of the stack first
Type of stack
The stack with linear storage structure is called sequential stack, and the stack with chain storage structure is called chain stack
Basic operation of stack:
The chain stack is used here, and the sequence stack will be explained in detail in the following articles
//Initialization stack Status InitStack(LinkSqStack *Stack); //Destroy stack Status DestoryStack(LinkSqStack *Stack); //Air judgment Status EmptyStack(LinkSqStack *Stack); //Enter the stack Status push(LinkSqStack *Stack,ElemType e); //Out of stack Status Pop(LinkSqStack *Stack,ElemType *e); //Get stack top element Status GetPop(LinkSqStack *Stack,ElemType *e); //Point the pointer to the last element Status pTail(LinkSqStack *Stack); //Find the length of the stack Status LengthStack(LinkSqStack *Stack); //Stack traversal Status TraverseStack(LinkSqStack *Stack);
Implementation of basic operation of stack:
state invariant
It is convenient for subsequent changes after the header definition. Status is the alias of int, which is also convenient for subsequent changes
#include <stdlib.h> #include <stdio.h> #define OK 1 #define ERROR 0 #define OVERFLOW –2 #define TRUE 1 #define FALSE 0 typedef int Status;
Structure definition of stack
ElemType defines the type of the stack node. Alias is used for subsequent changes
typedef int ElemType; typedef struct LinkSqNode{ ElemType data; struct LinkSqNode *next; }LinkSqStack;
Initialization stack
The initialization stack is too simple and will not be described in detail here
Status InitStack(LinkSqStack *Stack){ Stack= (LinkSqStack*)malloc(sizeof(LinkSqStack)); if (!Stack) { return FALSE; } Stack->next = NULL; return OK; }
Destroy stack
Destroy the stack, because the nodes are created one by one when they are created, so they should be destroyed one by one when they are destroyed. Two auxiliary pointers are needed. One pointer points to the first element node and the other pointer points to the next node of the first element node. Then, the stack can be destroyed by using the two pointers to cycle in turn, and the head node will be released later
Status DestoryStack(LinkSqStack *Stack){ LinkSqStack *p = Stack,*q = Stack->next; while (q) { p = q; q = q->next; free(p); } free(q); printf("Stack destroyed\n"); return OK; }
Empty judgment of stack
If the following of the header node is empty, the stack is empty
Status EmptyStack(LinkSqStack *Stack){ if (Stack->next == NULL) { printf("Stack empty\n"); return FALSE; }else{ printf("Stack is not empty\n"); return OK; } }
Stack or stack operation
Generally, Push is used to represent the stack operation, create a new node, transfer the data to be stacked to this node, and then insert this node into the top of the stack (here, the top of the stack is in the front, and the initial node is the top of the stack, which is to insert and delete at the front of the linked list)
Status push(LinkSqStack *Stack,ElemType e){ LinkSqStack *p = (LinkSqStack*)malloc(sizeof(LinkSqStack)); p->data = e; p->next = Stack->next; Stack->next = p; return OK; }
Out of stack or out of stack operation
First, judge whether the stack is empty, then move an auxiliary pointer to the first element node (Note: the value must be taken out before deleting the node), let the pointer field of the head node record the pointer field of the first element node, and then release the first element node
Status Pop(LinkSqStack *Stack,ElemType *e){ if (Stack->next == NULL) { printf("Stack empty\n"); return FALSE; } LinkSqStack *p; p = Stack->next; *e = p->data; Stack->next = p->next; free(p); return OK; }
Get stack top element
First, judge whether the stack is empty, and then take the value directly without deleting the node
Status GetPop(LinkSqStack *Stack,ElemType *e){ if (Stack->next == NULL) { printf("Stack empty\n"); return FALSE; } *e = Stack->next->data; return OK; }
Find the length of the stack
The operation is too simple to explain here
Status LengthStack(LinkSqStack *Stack){ int count = 0; while (Stack) { Stack = Stack->next; count++; } printf("The capacity of the stack is%d",count); return OK; }
Stack traversal
First, move the pointer to the first element node because the head node does not maintain the data field, and then use the while loop to traverse the stack until the pointer points to the bottom element of the stack, and the pointer field of the bottom element of the stack is empty
Status TraverseStack(LinkSqStack *Stack){ Stack = Stack->next; if (Stack->next == NULL) { printf("Stack empty\n"); return FALSE; } while (Stack) { printf("%d\t",Stack->data); Stack =Stack->next; } return OK; }
The following is the stack test code
int main(void){ LinkSqStack Stack; ElemType e; InitStack(&Stack); for (int i = 0; i < 9; i++) { push(&Stack, i); } EmptyStack(&Stack); LengthStack(&Stack); printf("\n"); TraverseStack(&Stack); printf("\n"); GetPop(&Stack, &e); printf("Stack top element%d",e); printf("\n"); printf("Element out of stack (the first three elements out of stack)\n"); for (int j = 0; j < 3; j++) { Pop(&Stack, &e); printf("%d\t",e); } printf("\n"); DestoryStack(&Stack); }
test result