A brief introduction to stack

Background introduction

Recently, I read the big talk data structure again, and here I summarize Chapter 4 - stack.

Stack

By definition, a stack is a linear table restricted to insertion and deletion at the end of the table. One end of the insert and delete operation is called the top of the stack, the other end is the bottom of the stack, and no element is called an empty stack.
Abstract data type of stack:

Sequential storage structure of stack

The structure code is as follows:

typedef int SElemType; 	/* SElemType The type depends on the actual situation. It is assumed to be int */

/* Sequential stack structure */
typedef struct
{
        SElemType data[MAXSIZE];
        int top; 		/* Pointer for stack top */
}SqStack;		//sq means sequence, which means sequence, sqlist means sequence table, and SqStack means sequence stack.

It can be seen that the storage structure of SqList is not different from that of SqStack, except that the length in SqList is replaced by top, where top points to the element at the top of the stack. For example, when there is an element in the stack, that is, when data[0] exists, top is 0, and when there is no element in the stack, top is - 1
In addition, the stack also needs to create an empty stack L through the initialization function initstack (* l).
Various corresponding operation functions can be queried on the Internet, which will not be repeated here.

Tips of stack -- shared space between two stacks

Definition: simply put, the two stacks used share the same storage space.
The code is as follows:

/* Two stack shared space structure */
typedef struct 
{
        SElemType data[MAXSIZE];
        int top1;	/* Stack 1 stack top pointer */
        int top2;	/* Stack 2 stack top pointer */
}SqDoubleStack;

It can be seen that the structure of the two stacks is only one more top variable than the sequential stack SqStack.
Use the function InitStack to initialize two stacks

/*  Construct an empty stack S */
Status InitStack(SqDoubleStack *S)
{ 
        S->top1=-1;
        S->top2=MAXSIZE;
        return OK;
}

The stack is as follows:

Here, n is MAXSIZE. It can be seen that the structure of the two stacks is only one more top2 variable than the sequential stack SqStack. In the stack, stack 1 and stack 2 are actually the same stack and share the same storage space, that is, array data (SElemType data[MAXSIZE];), but it is artificially divided into two stacks (stack 1 and stack 2).
When an element enters stack 1, top1 is increased by one. When an element leaves stack 1, top1 is reduced by one, which has little to do with the insertion and deletion of the sequential stack SqStack. When an element enters stack 2, top2 is reduced by one. When an element leaves stack 2, top2 is increased by one. In short, stack 1 takes the left end of the array as the bottom of the stack, extends to the right, and stack 2 takes the right end of the array as the bottom of the stack, and extends to the left.
Attach the inserted code to deepen the understanding of dual stack.

/* Insert element e as the new stack top element */
//stackNumber, indicating that the inserted stack is 1 or 2
Status Push(SqDoubleStack *S,SElemType e,int stackNumber)
{
        if (S->top1+1==S->top2)	/* The stack is full and cannot push new elements */
                return ERROR;	
        if (stackNumber==1)			/* There are elements in stack 1 */
                S->data[++S->top1]=e; /* If it is stack 1, first top1+1 and then assign a value to the array element. */
        else if (stackNumber==2)	/* There are elements in stack 2 */
                S->data[--S->top2]=e; /* If it is stack 2, first top2-1 and then assign a value to the array element. */
        return OK;
}

Criteria for stack full:
It is easy to find that when top1+1=top2, the stack is full, and no more elements can be inserted at this time.

Chain storage structure of stack

The structure code is as follows:

/* Chain stack structure */
typedef struct StackNode
{
        SElemType data;
        struct StackNode *next;
}StackNode,*LinkStackPtr;


typedef struct
{
        LinkStackPtr top;
        int count;
}LinkStack;

It can be seen that there is not much difference between the chain stack and the single linked list LinkList. LinkStackPtr top is equivalent to the head pointer of the single linked list LinkList, but the head pointer of the single linked list LinkList is generally preceded by the list name, while top points to the tail node, and there is an additional counter count to form a chain stack structure linkstack. In addition, as an aside, there is a length in the sequence table SqList, which also plays a similar role.
Chain stack initialization:

/*  Construct an empty stack S */
Status InitStack(LinkStack *S)
{ 
        S->top = (LinkStackPtr)malloc(sizeof(StackNode));
        if(!S->top)
                return ERROR;
        S->top=NULL;
        S->count=0;
        return OK;
}

In addition, it is worth noting that the next pointer in the single chain table points to the next node, that is, the successor, but the node pointed to by the next pointer in the chain stack is actually the previous element, that is, the precursor.
A flow chart is attached

Attach the function code of the inserted element to understand:

/* Insert element e as the new stack top element */
Status Push(LinkStack *S,SElemType e)
{
        LinkStackPtr s=(LinkStackPtr)malloc(sizeof(StackNode)); 
        s->data=e; 
        s->next=S->top;	/* Assign the current stack top element to the direct successor of the new node, as shown in figure ① */
        S->top=s;         /* Assign the new node s to the stack top pointer, as shown in figure ② */
        S->count++;
        return OK;
}

Other types of corresponding operation functions can be queried online, which will not be repeated here.

Tags: data structure

Posted on Wed, 13 Oct 2021 23:53:09 -0400 by Mgccl