Current location - Health Preservation Learning Network - Healthy weight loss - Design an algorithm to judge whether the left and right brackets in the expression are paired and what data structure is the best.
Design an algorithm to judge whether the left and right brackets in the expression are paired and what data structure is the best.
Use "stack"? This data structure.

Stack? (stack) is LIFO (last? Are you online? First of all? Off, LIFO) data structure.

The basic idea of the algorithm is: judge each character in the expression in turn. If it is a left parenthesis, put it on the stack; if it is a right parenthesis, judge whether it is empty; if it is empty, it means wrong; finally, when there is no character at the end of the expression, judge whether the stack is empty; if it is empty, it means matching; if it is not empty, it means mismatch.

Refer to the following code and address (/kkkkkxiaofei/article/details/8293980).

# Contains? & ltstdio.h & gt

# Contains? & ltmalloc.h & gt//malloc,realloc

# Contains? & ltmath.h & gt? //contains overflow

# Contains? & ltprocess.h & gt? //Exit ()

# Definition? S_SIZE? 100// space size of the stack

# Definition? STACKINCREAMENT? 10// Increase space

struct? SQL stack {

int? * base number; ? //Bottom of stack

int? * top? //stack top

int? Stacksize// The current storage space of the stack.

};

Invalid? Master ()

{//Sub-function declaration

Invalid? InitStack(SqStack? & amps); //Initialize an empty stack

int? StackEmpty(SqStack? s); //Empty sentence

Invalid? Push (SqStack? & amps,int? e); //into the stack

Invalid? pop(SqStack? & amps,int? & ampe); //Exit the stack

//The start of the main function

SqStack? s; //Initialize an empty stack

InitStack

Charles? ch[ 100],* p; int? e;

p = ch

Printf ("Enter a bracket expression, which means () [] {}: \ n");

gets(ch);

while(*p)

{?

Switch? (*p)

{

Case? '{':

Case? '[':

Case? '(':? push(s,* p++); Break; //As long as it is an opening parenthesis, it will be put on the stack.

Case? '}':

Case? ']':

Case? )’:pop(s,e);

? What if? ((e=='{ '? & amp& amp? *p=='} ')? ||(e=='['? & amp& amp? *p==']')? ||? (e== '('? & amp& amp? *p== ')'))

? p++;

? other

? {printf ("Parentheses don't match!" ); Exit (overflow); }

? Break;

Default? :p++; //Other characters move backward.

}

}

What if? (Stack Properties)

Printf ("brackets match successfully");

other

Printf ("Missing closing bracket!" );

printf(" \ n ");

}

Invalid? InitStack(SqStack? & amps)

{S.base=(int? *)malloc(S _ SIZE * sizeof(int));

S.stacksize = S _ SIZE

s . top = s . base; //Initialize an empty stack

}

int? StackEmpty(SqStack? s)

{

if(S.base==S.top)

Return? 1;

other

Return? 0;

}

Invalid? Push (SqStack? & amps,int? e)

{//Enter the stack

if(s . top-s . base & gt; =S.stacksize)

{S.base=(int? *)realloc(S.base,(s . stack size+stack increament)* sizeof(int));

s . top = s . base+s . stack size;

s . stack size+= stack increation; }

*(s . top)= e;

s . top++; ?

}

Invalid? pop(SqStack? & amps,int? & ampe)

{//Leave the stack

If (S.base! =S.top)

{ s . top-;

e = * S.top}

}