#include<stdio.h>
#include<stdlib.h>
#define ERROR -1
#define OK 1
typedef char SElemType;
typedef int Status;
typedef struct LNode
{ SElemType data;
struct LNode *next;
} *SLink;
typedef struct
{ SLink top;
int length;
}LStack;
LNode *p;
LNode *q;
LStack S;
void InitStack(LStack &S){
S.top=NULL;
S.length = 0;
}
int gettop(LStack S,char &e){
if(S.length==0) return ERROR;
e=(S.top->next->data);
return OK;
}
void push(LStack &S,char e){
p=(LNode*)malloc(sizeof(LNode));
if(!p) exit(1);
p->data=e;
p->next=S.top;
S.top=p;
++S.length;
}
bool Pop(LStack &S,char &e){
if(!S.top) return false;
else{
e=S.top->data;
S.top=S.top->next;
--S.length;
return true;
}
}
int main(){
InitStack(S);
char e='a';
for(int i=0;i<5;i++){
push(S,e);
e++;
}
if(gettop(S,e)==1) printf("%c ",e);
puts("");
while(Pop(S,e)){
printf("%c ",e);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef char SElemType;
typedef int Status;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
int OK=1,OVERFLOW=-1,ERROR=-1;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack_Sq(SqStack &S) {
S.base=(SElemType * )malloc((STACK_INIT_SIZE)*sizeof(SElemType));
if (! S. base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack S, SElemType &e) {
if (S.top==S.base) return ERROR;
e = * (S.top-1);
return OK;
}
Status Push(SqStack &S, SElemType e) {
if (S.top-S.base>=S.stacksize) {
S.base= (SElemType*)realloc(S.base,(S.stacksize +STACKINCREMENT)*sizeof(SElemType));
if (! S. base) exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S, SElemType &e) {
if (S.top== S.base) return ERROR;
e = * --S.top;
return OK;
}
int main(){
SqStack S;
char c;
printf("%d ",InitStack_Sq(S));
puts("");
char e='a';
for(int i=0;i<5;i++){
Push(S,e);
e++;
}
if(GetTop(S,e)==1) printf("%c ",e);
puts("");
while(Pop(S,e)==1){
printf("%c ",e);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define ERROR -1
#define OK 1
typedef char SElemType;
typedef int Status;
typedef struct LNode
{ SElemType data;
struct LNode *next;
} *SLink;
typedef struct
{ SLink top;
int length;
}LStack;
LNode *p;
LNode *q;
LStack S,R;
char e;
void InitStack(LStack &S){
S.top=NULL;
S.length = 0;
}
int gettop(LStack S,char &e){
if(S.length==0) return ERROR;
e=(S.top->next->data);
return OK;
}
void push(LStack &S,char e){
p=(LNode*)malloc(sizeof(LNode));
if(!p) exit(1);
p->data=e;
p->next=S.top;
S.top=p;
++S.length;
}
bool Pop(LStack &S,char &e){
if(!S.top) return false;
else{
e=S.top->data;
S.top=S.top->next;
--S.length;
return true;
}
}
void Enqueue(LStack &S){
e='a';
for(int i=0;i<5;i++){
push(S,e);
printf("%c ",e);
e++;
}
}
void dequeue(LStack &S,LStack &R){
if(R.top) {
Pop(R,e);
}
else{
while(S.length!=0){
Pop(S,e);
push(R,e);
}
Pop(R,e);
}
printf("%c ",e);
}
int main(){
InitStack(S);
InitStack(R);
puts("依次进队5个:");
Enqueue(S);
puts("");
puts("出队一个:") ;
dequeue(S,R);
puts("") ;
puts("再出队一个哈哈:") ;
dequeue(S,R);
return 0;
}