Home > Back-end >  Data structure - stack is used to implement the parentheses matching test. Code does not know where
Data structure - stack is used to implement the parentheses matching test. Code does not know where

Time:12-14

Using DEVC++ environment
 # include & lt; stdio.h> 
#include

# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10
# define the ERROR - 1
Typedef struct {//define the sequential storage structure of stack
Char * base;
Char * top;
int stacksize;
} SqStack;

Void InitStack (SqStack * s)//build an empty stack
{
s-> The base=(char *) malloc (STACK_INIT_SIZE * sizeof (char));
if(! s-> Base) exit (1);
s-> Top=s - & gt; The base;
s-> Stacksize=STACK_INIT_SIZE;
}

Int GetTop (SqStack * s, char * e)
{
If (s - & gt; Top==s - & gt; Base) return the ERROR;
* e=* (s - & gt; Top - 1);
return 1;
}

Void push SqStack * s, char (e)
{
If ((s - & gt; Top - s - & gt; Base) & gt;=s - & gt; Stacksize)//stack is full, redistribution of storage
{
s-> The base=(char *) realloc (s - & gt; Base, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof (char));
if(! s-> Base) exit (1);
s-> Top=s - & gt; Base + s - & gt; Stacksize;
s-> Stacksize +=STACKINCREMENT;
}
* s - & gt; Top++=e;
}

Pop (SqStack * s is an int, char * e)
{
If (s - & gt; Top==s - & gt; Base) return 0;
=* * e - s - & gt; Top;
return 1;
}

Int StackEmpty (SqStack * s)
{
If (s - & gt; Top==s - & gt; Base) return 1;//the stack is empty, the return value is 1
else return 0;
}

Int BracketsMatch (SqStack * s, char [] b)
{
InitStack(s);
int i=0;
Char * e;
While (b [I]!='#')
{
The switch (b [I])
{
Case '(' : push (s, b [I]); break;
Case '{' : push (s, b [I]); break;
Case '[' : push [I] (s, b); break;
Case ') :
If (StackEmpty (s)) return 0;
Pop (s, e);
If (* e! [I]=b) return 0;
break;
Case '] ':
If (StackEmpty (s)) return 0;
Pop (s, e);
If (* e! [I]=b) return 0;
break;
Case '} ':
If (StackEmpty (s)) return 0;
Pop (s, e);
If (* e! [I]=b) return 0;
break;
}
++i;
}
If (StackEmpty (s)) return 1;//the stack is empty, the matching
else return 0;//otherwise, does not match the
}

Int main ()
{
SqStack * s;
Char b [100], a;
Int I=0, j;
Do {
Printf (" please enter a bracket (#) to end the characters: \ n ");
The scanf (" % c ", & amp; B [I]);
[I] a=b;
i++;
} while (a!='#');
J=BracketsMatch (s, b);
If (j==1) printf (" matching brackets! \ n ");
Else printf (" brackets don't match! \ n ");
return 0;
}



But running, the loop statement is always play out three times, and the input end # cycle but no response to exit the program directly,
Don't know what went wrong in logic, before had a circle of printf statements each print three times, what is going on here?
Each great god give advice or comments please!!!!!!
There is, in BracketsMatch function, only the stack pointer s, actually like GetTop function parameters do not need to use * s, however,
The start to use in the BracketsMatch * s to create a stack, if not, just want to in the form of parameters, how should do? To define a
Stack type variable, and then the * s Stack is assigned to it?
  • Related