# include <iostream>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <malloc.h>
# define STACK_INIT_SIZE 5
# define ok 1
# define error 0
using namespace std;
typedef int elemtype;
typedef int status;
typedef struct
{
elemtype * base;
elemtype * top;
int stacksize;
} sqstack;
status initstack ( sqstack & s)
{
s. base= ( elemtype* ) malloc ( STACK_INIT_SIZE* sizeof ( elemtype) ) ;
if ( ! s. base)
exit ( 0 ) ;
s. top= s. base;
s. stacksize= STACK_INIT_SIZE;
return ok;
}
status destorystack ( sqstack & s)
{
s. top= NULL ;
s. stacksize= 0 ;
free ( s. base) ;
return ok;
}
status clearstack ( sqstack & s)
{
s. top= s. base;
return ok;
}
status push ( sqstack & s, elemtype e)
{
if ( s. top- s. base== 5 )
cout<< "No" ;
* s. top= e;
s. top++ ;
return ok;
}
status pop ( sqstack & s, elemtype e)
{
if ( s. top= s. base)
return error;
else
{
s. top-- ;
e= * s. top;
return e;
}
}
int main ( )
{
sqstack s;
printf ( "构造一个空栈……\n" ) ;
initstack ( s) ;
int i, n ;
printf ( "输入栈的长度:\n" ) ;
scanf ( "%d" , & n) ;
for ( i = 1 ; i <= n; i++ )
{
printf ( "输入栈的第%d个元素\n" , i) ;
++ s. top;
scanf ( "%d" , s. top- 1 ) ;
}
push ( s, 3 ) ;
}
# include <iostream>
# include <stdio.h>
# include <malloc.h>
typedef int elementtype;
using namespace std;
typedef struct snode * stack;
struct snode
{
elementtype data;
stack next;
} ;
stack createstack ( )
{
stack s;
s= ( stack) malloc ( sizeof ( stack) ) ;
s-> next= NULL ;
return s;
}
int isempty ( stack s)
{
return ( s-> next == NULL ) ;
}
void push ( stack s, elementtype e)
{
stack tmp;
tmp= ( stack) malloc ( sizeof ( stack) ) ;
tmp-> data= e;
tmp-> next= s-> next;
s-> next= tmp;
}
elementtype pop ( stack s)
{
stack first;
elementtype number;
if ( isempty ( s) )
return 0 ;
else
{
first= s-> next;
number= first-> data;
s-> next= first-> next;
free ( first) ;
return number;
}
}
int main ( ) {
stack s;
s= createstack ( ) ;
push ( s, 2 ) ;
push ( s, 4 ) ;
push ( s, 9 ) ;
push ( s, 666 ) ;
cout<< pop ( s) ;
return 0 ;
}