[Graphical data structure] A comprehensive summary of the stack

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

I. Introduction

learning target:

  • Master the characteristics of the abstract data type stack, and correctly apply the relevant codes in the corresponding practical problems
  • Master sequential stack, dual stack, stack call

2. Basic Concepts

  • Definition : A linear list that only allows insertion or deletion at one end
  • top of the stack : the end that allows insertion or deletion
  • Bottom: the end corresponding to the top of the stack
  • Features: first in, last out

3. Representation and implementation of stack

1. Sequential stack

  • Definition : A group of storage units with consecutive addresses store data elements from the bottom of the stack to the top of the stack in turn

  • top : used to indicate the position of the top element of the stack

    • top==-1 means empty stack
    • top==NULL means the stack does not exist
    • top>stacksize element overflows

Structure definition:

#define n 500//定义顺序栈大小
struct stack
{
    int top;//栈顶指针
    int a[n];//顺序栈数组
}SeqStack;
复制代码

2. Chain stack

Structure definition:

typedef struct node
{
    int data;//定义数据类型
    struct node*next;//定义链栈

}ChaiinStack;
复制代码

Features:

  •  The chain stack has no stack full problem, and the size can be expanded at any time
  • Insertion and deletion are performed at the top of the stack
  • The top of the chain stack is at the head of the chain
  • Consistent with the storage structure of the singly linked list and the logical structure of the sequential list

Fourth, the common algorithm of sequential stack

dynamic picture:

Algorithm explanation:

  • push onto the stack, the pointer top moves up
  • pop pops the stack, the pointer top moves down

1. Initialization

void InitStack(SeqStack *S)
{	/*构造一个空栈S*/
  	S->top = -1;
}
复制代码

2. Empty

/*顺序栈判栈空*/
int IsEmpty(SeqStack *S)
 {     /*判断栈S为空栈时返回值为真,反之为假*/
	 return(S->top==-1?TRUE:FALSE);
 }
复制代码

3. Full sentence

/*顺序栈判栈满*/
int IsFull(SeqStack *S)	
{   /*判断栈S为满栈时返回值为真,反之为假*/
	return(S->top==Stack_Size-1?TRUE:FALSE);
}
复制代码

4. Sequential stack fetches the top element of the stack

int GetTop(SeqStack *S,StackElementType *x)
{  	/* 将栈S的栈顶元素弹出,放到x所指的存储空间中,但栈顶指针保持不变 */
	if(S->top == -1)      /*栈为空*/
		return(FALSE);
	else
	{
  		*x = S->elem[S->top];
  		return(TRUE);
	}	
}
复制代码

5. Sequential stack push

/*顺序栈出栈*/
int Pop(SeqStack *S,StackElementType *x)
{  
	/* 将栈S的栈顶元素弹出,放到x所指的存储空间中 */
	if(S->top == -1)    /*栈为空*/
		return(FALSE);
	else
	{
  		*x = S->elem[S->top];
		S->top++;    /* 修改栈顶指针 */
  		return(TRUE);
	}
}
复制代码

6. Sequential stack pop

/*顺序栈出栈*/
int Pop(SeqStack *S,StackElementType *x)
{  
	/* 将栈S的栈顶元素弹出,放到x所指的存储空间中 */
	if(S->top == -1)    /*栈为空*/
		return(FALSE);
	else
	{
  		*x = S->elem[S->top];
		S->top--;    /* 修改栈顶指针 */
  		return(TRUE);
	}
}
复制代码

Five, double stack

 

Algorithm details:

  • Two sequential stacks stack1, stack2
  • Sequence stack 1 is pushed onto the stack from left to right, top1++
  • Sequence stack 1 is pushed into the stack from right to left, top2--
  • Stack full condition : top1+1==top2

1. Double-ended sequential stack push operation

/*双端顺序栈进栈操作。*/
int Push(DqStack *S, StackElementType x, int i)
{	/*把数据元素x压入i号堆栈*/
	if(S->top[0]+1==S->top[1])    /*栈已满*/
		return(FALSE);
	switch(i)
	{
	case 0:
		S->top[0]++;	S->Stack[S->top[0]]=x;	break;
	case 1:
		S->top[1]--;	S->Stack[S->top[1]]=x;	break;
	default:  /*参数错误*/
        return(FALSE)
 	}
	return(TRUE);
}
复制代码

2. Double-ended sequential stack pop operation

/*双端顺序栈出栈操作。*/
int Pop(DqStack *S,StackElementType *x,int i)
{	/* 从i 号堆栈中弹出栈顶元素并送到x中 */
	switch(i)
	{
	case 0:
		if(S->top[0]==-1)  return(FALSE);
		*x=S->Stack[S->top[0]];   S->top[0]--;	break;
	case 1:
		if(S->top[1]==M)  return(FALSE);
		*x=S->Stack[S->top[1]];	S->top[1]++;	break;
	default:
		return(FALSE);
	}
	return(TRUE);
}
复制代码

6. Summary and improvement

  • For programming in C++, the above sequence stack's empty, full, insertion, deletion, etc. series of codes do not need you to fully master, the C++ STL standard library has prepared functions for you to call.

C++stack header file:

#include<stack>
//#include<bits/stdc++.h>或者万能头文件
using namespace std;
复制代码

C++stack specific operations:

  • Use stack to define the s class (you can define anything, as long as you turn s into a defined letter, you can call the function in C++), the specific usage method is:
function usage
s.empty() Determine if the stack is empty, return 1 if it is not empty, return 0 if it is empty
s.size() Returns the number of elements in the stack
s.top() Returns the top element of the stack without removing the element
s.pop() Return the top element of the stack, delete the element
s.push() insert element into stack

Guess you like

Origin juejin.im/post/7079939578777305095