Linear structure of data structure (stack)

This article is a linear structure (stack) of the data structure, and it is an integrated note based on the online course.

How does the computer evaluate expressions?

  • Expressions are composed of two types of objects:

    1. Operands, such as 2, 3, 4
    2. Operation symbols, such as +, -, *, /
  • Different operation symbols have different priorities

Postfix expression

  • Infix expression: the operator is located between the two operands. For example, a+b*cd/e

  • Postfix expression: the operator is located after the two operands. For example, abc*+de/-

[Example] 62 / 3-42 * + =?

后缀表达式求值策略: "Scan" from left to right, process operands and operation symbols one by one

1. What should I do if I encounter an operand? How to "remember" the number that is not currently involved in the operation?

2. What should I do if I encounter arithmetic symbols? What is the corresponding operand?

Enlightenment: There needs to be a storage method that can store the arithmetic in order and output it in "reverse order" when needed

Abstract data type description of the stack

Stack : a linear table with certain operation constraints

  • Insert and delete only one pile (top of stack, Top)

  • Insert data: Push

  • Delete data: Pop

  • Last In First Out Principle: Last In First Out (LIFO)

类型名称: Stack

数据对象集: A finite linear table with 0 or more elements.

操作集: Stack S<-Stack with a length of MaxSize, stack element item<-ElementType

1.Stack CreateStack(int MaxSize): Generate an empty stack, the maximum length of which is MaxSize;

2. int IsFull (Stack S, int MaxSize): Determine whether the stack S is full;

3.void Push(Stack S, ElementType item): Push the element item onto the stack

4.int IsEmpty(Stack S): Determine whether the stack S is empty;

5. ElementType Pop (Stack S): delete and return to the top element of the stack;

ps: Push and Pop can be interspersed alternately;

examples:

Insert picture description here

Sequential storage implementation of the stack

The sequential storage structure of the stack usually consists of a one-dimensional array and a variable that records the position of the top element on the stack.

#define MaxSize <储存数据元素的最大个数>
typedef struct SNode*Stack;
struct SNode{
    
    
	ElementType Data[MaxSize];
	int Top;
};

(1) Stacking

void Push(Stack PtrS, ElementType item)
{
    
    
	if(Ptrs->Top == MaxSize-1){
    
    
		printf("堆栈满")return}else{
    
    
		PtrS->Data[++(PtrS->Top)]=item;
		return;
	}
}

(2) Popping

ElementType Pop(Stack Ptrs)
{
    
    
	if(PtrS->Top==-1){
    
    
		printf("堆栈空");
		return ERROR;/*ERROR是ElementType的特殊值,标志错误*/
	}else
		return(PtrS->Data[(PtrS->Top)--]);
}

Chained storage implementation of the stack

The chained storage structure of the stack is actually a singly linked list, called a chain stack. Insert and delete operations can only be performed at the top of the chain stack.

typedef struct SNode*Stack;
struct SNode{
    
    
	ElementType Data;
	struct SNode *Next;
};

(1) Stack initialization (build empty stack)

(2) Determine whether the stack s is empty

Stack CreateStack()
{
    
    /*构建一个堆栈的头结点,返回指针*/
	Stack S;
	S = (Stack)malloc(sizeof(struct SNode));
	S->Next = NULL;
	return S;
}
int IsEmpty(Stack S)
{
    
    /*判断堆栈s是否为空,若为空函数返回整数1,否则返回0*/
	return (S->Next == NULL);
}
void Push(ElementType item,Stack S)
{
    
    /*将元素item压入堆栈s*/
	struct SNode *TmpCell;
	TmpCell=(struct SNode*)malloc(sizeof(struct SNode));
	TmpCell->Element = item;
	TmpCell->Next = S->Next;
	S->Next = TmpCell;
}
ElementType Pop(Stack S)
{
    
    /*删除并返回堆栈S的栈顶元素*/
	struct SNode *FirstCell;
	ElementType TopElem;
	if(IsEmpty (S)){
    
    
		printf("堆栈空")return NULL;
	}else{
    
    
		FirstCell = S->Next;
		S->Next = FirstCell->Next;
		TopElem = FirstCell->Element;
		free(FirstCell);
		return TopElem;
	}
}

Stack application

Expression evaluation: read the items (operators or operands) of the suffix expression from left to right

1. Operand: push into the stack

2. Operator: pop an appropriate number of operands from the stack, calculate and push the result into the stack;

3. Finally, the element on the top of the stack is the result value of the expression.
Insert picture description here

Infix expression evaluation

How to convert an infix expression to a postfix expression?

Enlightenment:

1. The relative order of operands remains unchanged

2. The order of operation symbols has changed

  • Need to store "waiting" arithmetic symbols (stack!)
  • To compare the current arithmetic symbol with the last arithmetic symbol "waiting"
    Insert picture description here

method:

Read each object of the infix expression from beginning to end, and treat different objects according to different situations.

  1. Operand: direct output;
  2. Left bracket: push into the stack;
  3. Right parenthesis: pop the operator at the top of the stack and output it until it encounters the left parenthesis (pop out of the stack, no output);
  4. Operators:
    – If the priority is greater than the operator on the top of the stack, push it on the stack;
    – If the priority is less than or equal to the operator on the top of the stack, pop the operator on the top of the stack and output; compare the new operator on the top of the stack, Until the UN operator is greater than the priority of the operator on the top of the stack, then push the changed operator onto the stack;
  5. If the processing of each object is completed, the operators stored in the stack are output together.

Examples:
Insert picture description here

other apps:

  • Function call and recursive implementation
  • Depth-first search
  • Backtracking algorithm
  • 。。。

Disclaimer: Part of the information comes from the Internet, if there is any infringement, please contact me to delete it!
If there are errors, confusions, etc. in the article, criticisms and corrections are welcome!

Guess you like

Origin blog.csdn.net/hxtxsdcxy/article/details/113748961