Data structure: Stack

Stack design and implementation

Stack basic concepts

A stack is a special kind of linear list

A stack can only operate on one end of a linear list

The top of the stack (Top): the end of the allowed operation

Bottom: The end of the stack that does not allow operations

Common operations of Stack

create stack

destroy stack

empty stack

push the stack

pop

Get the top element of the stack

Get the size of the stack

 

C language description ====="stack design and realization of accumulation of wealth in life

#ifndef _MY_STACK_H_

#define _MY_STACK_H_

 

typedef void Stack;

 

Stack* Stack_Create();

 

void Stack_Destroy(Stack* stack);

 

void Stack_Clear(Stack* stack);

 

int Stack_Push(Stack* stack, void* item);

 

void* Stack_Pop(Stack* stack);

 

void* Stack_Top(Stack* stack);

 

int Stack_Size(Stack* stack);

 

#endif //_MY_STACK_H_

 

Design and Implementation of Sequential Storage of Stack

1. Basic Concepts

2. Design and implementation

head File

#ifndef __MY_SEQLIST_H__

#define __MY_SEQLIST_H__

 

typedef void SeqList;

typedef void SeqListNode;

 

SeqList* SeqStack_Create(int capacity);

 

void SeqStack _Destroy(SeqStack * list);

 

void SeqStack _Clear(SeqStack * list);

 

int SeqStack _Length(SeqStack * list);

 

int SeqStack _Capacity(SeqStack * list);

 

int SeqStack _Insert(SeqStack * list, SeqListNode* node, int pos);

 

SeqListNode* SeqList_Get(SeqList* list, int pos);

 

SeqListNode* SeqList_Delete(SeqList* list, int pos);

 

#endif //__MY_SEQLIST_H__

 

Stack chain storage design and implementation

1. Basic Concepts

 

 

2. Design and implementation

head File

#ifndef _MY_LINKSTACK_H_

#define _MY_LINKSTACK_H_

 

typedef void LinkStack;

 

LinkStack* LinkStack_Create();

 

void LinkStack_Destroy(LinkStack* stack);

 

void LinkStack_Clear(LinkStack* stack);

 

int LinkStack_Push(LinkStack* stack, void* item);

 

void* LinkStack_Pop(LinkStack* stack);

 

void* LinkStack_Top(LinkStack* stack);

 

int LinkStack_Size(LinkStack* stack);

 

#endif //_MY_LINKSTACK_H_

 

stack application

Application 1: Nearest Match

Almost all compilers have the ability to detect if parentheses match

How to implement symbol pair detection in the compiler?

#include <stdio.h> int main() { int a[4][4]; int (*p)[4]; p = a[0]; return 0;

Algorithm ideas

Scan from the first character

Ignored when a normal character is encountered, pushed onto the stack when a left symbol is encountered

Pop the top symbol from the stack when the right symbol is encountered and match

Successful match: continue reading the next character

Matching failure: stop immediately and report an error

end:

Success: all characters are scanned and the stack is empty

Failed: Match failed or all characters scanned but stack is not empty

When you need to detect things that appear in pairs but are not adjacent to each other

You can use the "last in first out" feature of the stack

Stacks are ideal for situations where "nearest matches" are required

 

The essential job of a computer is to do mathematical operations, so the computer can read strings

"9 + (3 - 1) * 5 + 8 / 2" and calculate the value?

 

 

Application 2: Infix Suffix

The essential job of a computer is to do mathematical operations, so the computer can read strings

"9 + (3 - 1) * 5 + 8 / 2" and calculate the value?

postfix expression ==? Compliant with computer operations

Polish scientists in the 1950s proposed a suffix expression that places operators after numbers.

The mathematical expressions we are used to are called infix expressions === "in line with human thinking habits

Example:

5 + 4=> 5 4 +

1 + 2 * 3 => 1 2 3 * +

8 + ( 3 – 1 ) * 5 => 8 3 1 – 5 * +

Infix expressions conform to human reading and thinking habits

The postfix expression conforms to the computer's "operational habit"

How to convert an infix expression to a postfix expression?

Infix to suffix algorithm:

Traverse numbers and symbols in infix expressions

For numbers: direct output

For symbols:

Left parenthesis: push onto the stack

Operator symbols: precedence comparison with top-of-stack symbols

If the stack top symbol has a low priority: this match is pushed onto the stack (the default stack top is a left parenthesis, and the left parenthesis has the lowest priority)

If the priority of the symbol on the top of the stack is not low: pop and output the symbol on the top of the stack, and then push it into the stack

Right parenthesis: pop the symbol on the top of the stack and output until it matches the opening parenthesis

End of traversal: pop all symbols in the stack and output

infix to suffix

How do computers calculate based on postfix expressions?

8 3 1 – 5 * +

Traverse numbers and symbols in postfix expressions

For numbers: push the stack

For symbols:

Pop the right operand from the stack

Pop left operand from stack

operate on symbols

push the result of the operation onto the stack

End of traversal: the only number in the stack is the result of the calculation

The magic of stacks!

Infix expressions are the way people are used to expressions

Postfix expressions are the way computers prefer

The infix form can be easily converted to the postfix form through the stack

The calculation process of the infix expression is similar to the process of compiling and running the program

Extension: gives you a string, computes the result

"1+2*(66/(2*3)+7)"

1

String parsing

Lexical Syntax Analysis

priority analysis

Data structure selection === "Stack or tree?

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325189444&siteId=291194637