Traditional Culture Encyclopedia - Photography major - Data structure hutong problem please master help C++ version.

Data structure hutong problem please master help C++ version.

Stacks can be used

# include & ltiostream.h & gt

# include & ltstdlib.h & gt

# include & ltstdio.h & gt

# Define Overflow -2

# Define ok 1

# definestack _ init _ size100//Initial allocation of storage space

# Define the stack increment 10 // Add components

Typedef structure {

char * base

char * top

Int stacksize// currently allocated space

Int length;

} SqStack//Sqlist

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int init stack(SQL stack & amp; s)

{

s . base =(char *)malloc(STACK _ INIT _ SIZE * sizeof(char)); //Allocate storage space

If (! American base) exports (0);

s . top = s . base;

S.stacksize = STACK _ INIT _ SIZE

s . length = 0;

Returns1;

}

/******************************************************************************/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Boolean stack (sqstack & amps)

If(S.top==S.base) returns1;

other

Returns 0;

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int free stack(SqStack & amp; s)

{

Free (s.base);

s . top = s . base;

Return ok;

}

/******************************************************************/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int stack lenth(SqStack & amp; S){

s . length = s . top-s . base;

Return length;

}

/******************************************************************/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int Push(sq stack & amp; s,char e){

if(s . length & gt; =S.stacksize){

S.base=(char*)realloc(S.base,(s . stack size+stack crement)* sizeof(char)); //Increase the allocated storage

If (! S.base) exit (overflow);

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

s . stack size+= stack crement;

}

* s . top++ = e;

s . length++;

Return ok;

}

/**************************************************************/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Character pop-up (SqStack & amps, char & ampe){ (

if(S.base==S.top)

Returns 0; //When the stack is empty, an error is returned.

other

e = *-s . top;

Length-;

Return e;

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void disp stack(SQL stack S)

{

int I;

for(I = s . length; I>0; I-)

{

printf("%c ",*(-s . top));

}

printf(" \ n ");

}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

int main(){

int I;

SqStack Lst

char a,b,c,d,e,f;

char g;

Printf ("Initialize stack: \ n");

init stack(Lst);

Printf ("Stack elements A, B, C, D, E, f \ n "); in turn);

CIN & gt; & gta & gt& gtb & gt& gtc & gt& gtd & gt& gte & gt& gtf;

Push(Lst,a);

Push(Lst,b);

Push(Lst,c);

Push(Lst,d);

Push(Lst,e);

Push(Lst,f);

printf(" print \ n ");

disp stack(Lst);

int l = stack lenth(Lst);

Cout & lt& lt "The length of the stack is"

Printf ("stack sequence:");

for(I = 1; I < = 6; i++)

{

Pop(Lst,g);

printf("%c ",g);

}

printf(" \ n ");

Printf ("stack is: %s\n ",(StackEmpty(Lst)? "empty": "not empty");

Printf ("Free stack \ n");

free stack(Lst);

Returns 0;

}

Please correct what you wrote before.