#include<stdio.h>
#include<conio.h> /*头文件*/
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10 /*宏定义*/
typedef int Status ; /*类型名定义用status代替int*/
struct STU{ /*定义学生信息结构体*/
char name[20];
char stuno[10];
int age;
int score;
};
typedef struct STU SElemType; /*类型名定义用selemtype代替结构体STU*/
struct STACK /*定义结构体指针*/
{
SElemType *base;
SElemType *top;
int stacksize;
};
typedef struct STACK SqStack; /*类型名定义用sqstack代替结构体stack*/
typedef struct STACK *pSqstack; /*类型名定义用*psqstack代替结构体stack*/
Status InitStack(SqStack **S);
Status DestroyStack(SqStack *S);
Status ClearStack(SqStack *S);
Status StackEmpty(SqStack S);
int StackLength(SqStack S);
Status GetTop(SqStack *Sa,SElemType *e);
Status Push(SqStack *S,SElemType e);
Status Pop(SqStack *Sa,SElemType *e);
Status StackPrintElem(SElemType e);
Status StackTraverse(SqStack S,SElemType *e);
Status input(SElemType e,SqStack *Sa); /*函数申明*/
Status InitStack(SqStack **S)
{ /*初始化栈*/
(*S)=(SqStack *) malloc(sizeof(SqStack)); /*为栈申请空间*/
(*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 DestroyStack(SqStack *Sa)
{ /*销毁栈*/
free(Sa->base);
free(Sa); /*释放空间*/
}
Status ClearStack(SqStack *Sa)
{ /*清空栈*/
Sa->top=Sa->base;
}
Status StackEmpty(SqStack S)
{ /*测试栈是否为空*/
if(S.top==S.base)
printf("the stack is empty!\n");
else
printf("the stack is not empty!\n");
}
StackLength(SqStack S)
{ /*求栈长度*/
int i;
SElemType *p;
i=0;
p=S.top;
while(p!=S.base) /*依次改变top指针并用i记录*/
{p--;
i++;
}
printf("\ni=%d\n",i);
}
Status GetTop(SqStack *Sa,SElemType *e)
{ /*读栈顶元素*/
if(Sa->top==Sa->base)
return ERROR;
else{ *e=*(Sa->top-1);
StackPrintElem(*e); /*调用打印函数*/
}
}
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 *Sa,SElemType *e)
{ /*出栈*/
if(Sa->top==Sa->base)
return ERROR;
else{
*e=*Sa->top;
StackPrintElem(*e);
}
}
Status StackPrintElem(SElemType e)
{ /*打印*e元素的信息*/
printf("%s %s %d %d\n",e.name,e.stuno,e.age,e.score);
}
Status StackTraverse(SqStack S,SElemType *e)
{ /*遍历并打印*/
while(S.top!=S.base)
{
S.top--;
e=S.top;
StackPrintElem(*e);
}
}
Status input(SElemType e,SqStack *Sa) /*输入信息*/
{ int i,n;
printf("Input the n:"); /*输入要插入的元素的个数*/
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("input:");
scanf("%s %s %d %d",&e.name,&e.stuno,&e.age,&e.score);
Push(Sa,e);
}
}
main()
{ int j,i,n;
SElemType e;
SqStack *Sa;
clrscr();
printf("\n\n-------------------SqStack Demo is running...----------------\n\n");
printf("First is Push function.\n");
InitStack(&Sa); /*初始化栈*/
printf(" Now Stack is Empty.\n");
input(e,Sa); /*输入*/
printf("StackTraverse and printing:\n");
StackTraverse(*Sa,&e); /*遍历并打印*/
printf(" StackLength is i:");
StackLength(*Sa); /*求栈长度*/
printf("\nthe number you want output:"); /*要出桟元素的个数*/
scanf("%d",&j);
Sa->top=Sa->top-1;
for(i=0;i<j;i++)
{ Pop(Sa,&e);
Sa->top=Sa->top-1;
} /*出栈个数及打印*/
Sa->top=Sa->top+1;
StackEmpty(*Sa); /*测试栈是否为空*/
printf("StackTraverse and printing:\n");
StackTraverse(*Sa,&e);
input(e,Sa); /*输入*/
printf(" StackLength is i:");
StackLength(*Sa); /*求栈长度*/
printf("StackTraverse and printing:\n");
StackTraverse(*Sa,&e); /*遍历并打印*/
printf("\nGetTop is:");
GetTop(Sa,&e); /*读栈顶元素*/
ClearStack(Sa); /*清空栈*/
printf(" StackLength is i:");
StackLength(*Sa); /*求栈长度*/
DestroyStack(&Sa); /*销毁栈*/
getch();
}
要这样输入:
-------------------SqStack Demo is running...----------------
First is Push function.
Now Stack is Empty.
Input the n:3
input:stu5 2004005 19 65
input:stu3 2004003 19 82
input:stu2 2004002 20 79
StackTraverse and printing:
stu2 2004002 20 79
stu3 2004003 19 82
stu5 2004005 19 65
StackLength is i:
i=3
the number you want output:2
stu2 2004002 20 79
stu3 2004003 19 82
the stack is not empty!
StackTraverse and printing:
stu5 2004005 19 65
Input the n:2
input:stu1 2004001 18 95
input:stu6 2004006 19 35
StackLength is i:
i=3
stu6 2004006 19 35
stu1 2004001 18 95
stu5 2004005 19 65
GetTop is:stu6 2004006 19 35
StackLength is i:
i=0