[ Brief ] Data Structure - Stack
second60 20180422
1. Definition of stack
A stack is a linear list that can only be inserted or deleted at one end.
The end of the table that only allows insertion is called the top of the stack, and the other end is called the bottom of the stack.
The characteristics of the stack are: last in, first out.
2. The abstract number type definition of the stack
ADT Stack
{
Data object:
D={ai | i<=i <= n,n>=0,ai ElemType类型}
Data relationship:
R={<ai, ai+1> | ai,ai+1为D}
Basic operations:
InItStack(&s): Initialize the stack
ClearStack(&s): Destroy the stack
StackLength(s): stack length
StackEmpty(s): Is the stack empty
Push(&s,e): push the stack
Pop(&s,&e): pop the stack
GetTop(s,&e): Get the top element of the stack
DispStack(s): print stack
}ADT Stack
3. The sequential storage structure of the stack
typedef struct
{
ElemType data[MAXSIZE];
int top;
}SqStack;
The algorithm code is as follows, implemented in C language, and has been compiled and passed:
#include "stdio.h"
#define MAXSIZE 15
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int top;
}SqStack;
void InitStack(SqStack **s)
{
*s = (SqStack*)malloc(sizeof(SqStack));
(*s)->top = -1;
}
void ClearStack(SqStack **s)
{
if(s!= NULL && *s != NULL)
free(*s);
}
int StackLength(SqStack *s)
{
return (s->top +1);
}
int StackEmpty(SqStack *s)
{
return (s->top == -1);
}
int Push(SqStack **s,ElemType e)
{
if((*s)->top == MAXSIZE -1)
return 0;
(*s)->top ++;
(*s)->data[(*s)->top] = e;
return 1;
}
int Pop(SqStack **s,ElemType *e)
{
if((*s)->top == -1)
return 0;
*e = (*s)->data[(*s)->top];
(*s)->top --;
return 1;
}
int GetTop(SqStack *s,ElemType *e)
{
if(s->top == -1)
return 0;
*e = s->data[s->top];
return 1;
}
void DispStack(SqStack *s)
{
int i;
for(i=s->top; i>=0;i--)
printf("%d ",s->data[i]);
printf("\n");
}
intmain()
{
int value = 0;
SqStack *stack;
InitStack(&stack);
Push(&stack,1);
Push(&stack,2);
Push(&stack,3);
Push(&stack,4);
DispStack(stack);
GetTop(stack,&value);
printf("stack top=%d\n",value);
Pop(&stack,value);
printf("stack pop=%d\n", value);
DispStack(stack);
getchar();
return 1;
}
4. Chained storage structure of stack
data type definition
typedef struct linknode
{
ElemType data;
struct linknode *next;
} LiStack;
Algorithm implementation, C language, has passed the self-test:
#include <stdio.h>
#define MAXSIZE 15
typedef int ElemType;
typedef struct linknode
{
ElemType data;
struct linknode *next;
} LiStack;
void InitStack(LiStack **s)
{
*s = (LiStack*)malloc(sizeof(LiStack));
(*s)->next = NULL;
}
void ClearStack(LiStack **s)
{
LiStack *p = (*s)->next;
while( p != NULL)
{
free((*s));
*s = p ;
p = p->next;
}
free((*s));
}
int StackLength(LiStack *s)
{
int i = 0;
LiStack *p = s->next;
while(p!= NULL)
{
i++;
p = p->next;
}
return i;
}
int StackEmpty(LiStack *s)
{
return s->next == NULL;
}
void Push(LiStack **s,ElemType e)
{
LiStack * p;
p = (LiStack*)malloc(sizeof(LiStack));
p->data = e;
p->next = (*s)->next;
(*s)->next = p;
}
void Pop(LiStack **s,ElemType *e)
{
LiStack * p;
if((*s)->next == NULL)
return 0;
p = (*s)->next;
*e = p->data;
(*s)->next = p->next;
free(p);
return 1;
}
int GetTop(LiStack *s,ElemType *e)
{
if(s->next == NULL)
return 0;
*e = s->next->data;
return 1;
}
void DispStack(LiStack *s)
{
LiStack *p = s->next;
while(p != NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
intmain()
{
int value = 0;
LiStack *stack;
InitStack(&stack);
Push(&stack,10);
Push(&stack,20);
Push(&stack,30);
Push(&stack,40);
Push(&stack,50);
DispStack(stack);
GetTop(stack,&value);
printf("stack top=%d\n",value);
Pop(&stack,value);
printf("stack pop=%d\n", value);
DispStack(stack);
getchar();
return 1;
}
5. Use of stack
The stack is one of the most commonly used and one of the most important data structures. Basically everywhere.
E.g:
1. The assembly function call implementation uses the principle of the stack , saves the scene, and restores the scene (the most important thing)
2. Stacks are used in many algorithms, such as binary trees, recursive calls, etc.
3. Among various scheduling systems, it is very common in the linux kernel
6. Summary
I got up in the morning, drank a cup of tea, and reviewed the stack by the way. The code was typed by hand, but I didn’t feel tired. Learning is also a joy, a very important data structure. The next article, the queue, is equally important.