Problems: 1. When the application functions, judge may appear error, input the correct sequence will output "is not in conformity with the requirements,"
2. In the realization of basic operation of the stack, the main function of case1 inside the while loop expression StackLength (s) & gt; 0 cycle will end up out of thin air again, if change to StackLength (s) & gt; 1 results output would be right, do not know why,
The great spirit guide genuflect is begged, thank you, here is the code:
# include & lt; stdio.h>
# include & lt; stdlib.h>
# include & lt; The malloc. H>
# include
/* the exit () */
100//# define STACK_INIT_SIZE initial stack space allocation to store the ElementType element number
10//# define STACKINCREMENT stack space is not enough after use each time increase the storage capacity of
Typedef short Status;//return status value
Typedef struct {
Char value;
} ElementType;
//typedef char ElementType;
Typedef struct {
ElementType * base;//the bottom of the stack pointer;
ElementType * top;//the stack pointer.
Int stacksize;//the current stack can accommodate a total how many ElementType element, is the largest capacity
}SqStack;
//according to the above definition, complete the following functions (using sequential storage structure)
The Status InitStack (SqStack & amp; S)//constructs an empty stack, stack space distribution
{
S.b ase=(ElementType *) malloc (STACK_INIT_SIZE * sizeof (ElementType));
if(! S.b ase)
exit(0);
S.t op=S.b ase;
S.s tacksize=STACK_INIT_SIZE;
return 1;
}
The Status DestoryStack (SqStack & amp; S)//destroyed a stack allocated, recovery of stack space
{
Free (S.b ase);
S.b ase=NULL;
S.t op=NULL;
S.s tacksize=0;
return 1;
}
Status StackEmpty (SqStack S)//judge S whether the stack is empty, returns 1, otherwise it returns 0
{
If (S.b ase==S.t op)
return 1;
The else
return 0;
}
Int StackLength SqStack (S)//returns the number of elements in S
{
Return S.t op - S.b ase;
}
The Status GetTop (SqStack S, ElementType & amp; E) elements to e//return stack, stack function returns 1 if the empty, normal return 1
{
If (S.t op> S.b ase)
{
E=* (S.t op - 1);
Printf (" stack element is: % c \ n ", e.v alue);
return 1;
} the else
return -1;
}
The Status of Push (SqStack & amp; S, ElementType e)//insert e element for the new stack, if the stack space is not enough, automatic expansion (realloc function)
{
If (S.t op - S.b ase>=S.s tacksize)
{
S.b ase=(ElementType *) realloc (S.b ase, (S.s tacksize + STACK_INIT_SIZE));
if(! S.b ase)
exit(0);
S.t op=S.b ase + S.s tacksize;
S.s tacksize=S.s tacksize + STACKINCREMENT;
}
* (S.t op)=e;
S.t op++;
return 1;
}
The Status of Pop (SqStack & amp; S, ElementType & amp; E)//is removed from the stack stack elements, and placed in e, empty stack function returns 1, normal return 1
{
If (S.t op==S.b ase)
return 0;
The else
S.t op=S.t op - 1;
E=* (S.t op);
return 1;
}
The Status Judge (SqStack S, ElementType & amp; E)
{
While (S.t op> S.b ase)
{
If ((S.t op - 1) - & gt; value=https://bbs.csdn.net/topics/=S.b (ase) -> value)
{
S.t op=S.t op - 1;
S.b ase=S.b ase + 1;
}
The else
return 0;
}
If ((S.t op) - & gt; The value https://bbs.csdn.net/topics/=='& amp; '| | (S.b ase) - & gt; The value https://bbs.csdn.net/topics/=='& amp; ')
return 1;
The else
return 0;
}
Void print (SqStack S)
{
Printf (" at this time within the stack elements is: \ n ");
While (S.t op> S.b ase)
{
S.t op=S.t op - 1;
//printf (" at this time within the stack elements is: \ n ");
Printf (" % \ t "c, S.t op - & gt; Value);
}
printf("\n");
}
Int main ()
{
SqStack s;
ElementType x;
int a;
Int I;
InitStack (s);
Printf (" please select the test content (1 for experiment, 2 for lab 2) \ n ");
The scanf (" % d ", & amp; a);
The switch (a)
{
Case 1: {
//ElementType c;
Printf (" please enter the five characters: \ n ");
for(i=0; i<6; I++)
{
The scanf (" % c ", & amp; X.v alue);
Push (s, x);
}
//printf (" 111 ");
//print (s);
Do {
GetTop (s, x);
Print (s);
Pop (s, x);
} while (StackLength (s) & gt; 1);
Puts (" \ n ");
}; break;
Case 2:
{
Printf (" please enter an odd number of characters (for sequence 1 & amp; 2 in the form of input sequences, with middle & amp; Separated, of which the sequence 1 and 2 for reverse sequence) \ n ");
Int d;
Printf (" please input to input the number of characters: ");
The scanf (" % d ", & amp; d);
Printf (" please enter the characters: \ n ");
for(i=1; i<=d; I++)
{
The scanf (" % c ", & amp; X.v alue);
Push (s, x);
}
If (Judge (s, x))
Printf (" meets the requirements! \n");
The else
Printf (" do not conform to the requirements! \n");
//printf (" are in development... ");
}; break;
Default: printf (" is developing.. "); break;
}
}
CodePudding user response:
# include & lt; stdio.h>
# include & lt; stdlib.h>
# include & lt; The malloc. H>
# include & lt; unistd.h>
//# include
/* the exit () */
100//# define STACK_INIT_SIZE initial stack space allocation to store the ElementType element number
10//# define STACKINCREMENT stack space is not enough after use each time increase the storage capacity of
Typedef short Status;//return status value
Typedef struct {
Char value;
} ElementType;
//typedef char ElementType;
Typedef struct {
ElementType * base;//the bottom of the stack pointer;
ElementType * top;//the stack pointer.
Int stacksize;//the current stack can accommodate a total how many ElementType element, is the largest capacity
}SqStack;
//according to the above definition, complete the following functions (using sequential storage structure)
The Status InitStack (SqStack & amp; S)//constructs an empty stack, stack space distribution
{
S.b ase=(ElementType *) malloc (STACK_INIT_SIZE * sizeof (ElementType));
if(! S.b ase)
exit(0);
S.t op=S.b ase;
S.s tacksize=STACK_INIT_SIZE; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull