2 回答
TA贡献1942条经验 获得超3个赞
表示对数据结构中结点的访问,所以是传相应的参数进去,然后输出来
用途: 对所有的输出统一格式,想修改printf里面的内容,只需要修改visit函数块里面的一处 #include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.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 ;
struct STU{
char name[20];
char stuno[10];
int age;
int score;
};
typedef struct STU SElemType;
struct STACK
{
SElemType *base;
SElemType *top;
int stacksize;
};
typedef struct STACK SqStack;
typedef struct STACK *pSqstack;
Status InitStack(SqStack **S);
Status DestroyStack(SqStack *S);
Status ClearStack(SqStack *S);
Status StackEmpty(SqStack S);
Status StackLength(SqStack S);
Status GetTop(SqStack S,SElemType *e);
Status Push(SqStack *S,SElemType e);
Status Pop(SqStack *S,SElemType *e);
Status StackTraverse(SqStack S,Status (*visit)());
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 *S)
{
free(S->base);
free(S);
}
Status ClearStack(SqStack *S)
{
S->top=S->base;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base) return TRUE;
else
return FALSE;
}
int StackLength(SqStack S)
{
int i;
SElemType *p;
i=0;
p=S.top;
while(p!=S.base)
{p++;
i++;
}
}
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;
}
Status StackPrintElem(SElemType * e)
{
printf("%s %s %d %d\n",e->name,e->stuno,e->age,e->score);
}
Status StackTraverse(SqStack S,Status (*visit)())
{
while(S.top!=S.base)
visit(--S.top);
}
void main()
{
SElemType e;
SqStack *Sa;
printf("\n\n-------------------SqStack Demo is running...----------------\n\n");
printf("First is Push function.\n");
InitStack(&Sa);
strcpy(e.name,"stu1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
printf(" Now Stack is Empty.\n");
StackTraverse(*Sa,StackPrintElem);
Push(Sa,e);
printf(" Now Stack has one element.\n");
StackTraverse(*Sa,StackPrintElem);
strcpy(e.name,"stu3");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
Push(Sa,e);
printf(" Now Stack has another element.\n");
StackTraverse(*Sa,StackPrintElem);
printf(" Now Pop Stack,the top elem put into variable e.\n");
Pop(Sa,&e);
printf("%s\n%s\n%d\n%d\n",e.name,e.stuno,e.age,e.score);
printf(" Let's see the left of Stack's elem:\n");
StackTraverse(*Sa,StackPrintElem);
getch();
printf("\n\n\nWelcom to visit http://zmofun.topcool.net\n\n");
}
TA贡献2012条经验 获得超12个赞
void Visit(int e){
cout<<e<<" ";
}
void StackTra(SqStack s,void(*Visit)(int)) //这个参数是函数指针,就是把一个函数当做参数传入另一个函数,没有什么别的意思 这里的Visit不是指上边的那个函数,而是指一个函数指针变量,你在调用这个函数的时候需要这样写 StackTra(s,Visit)----这里的Visit才指的是你前边定义的那个函数
{ //从栈底开始访问栈元素
while(s.top>s.base)
{
Visit(*s.base);
s.base++;
}
cout<<endl;
}
这里你的栈必须是连续的,而且base指向的是低地址,top指向的是高地址,如果是一个类的话,那你必须重新构造迭代器,为你的base重载一个 ++ 的方法,因为栈的定义是从栈顶开始访问的,但是从栈底访问可是可以做的,只是这样颠覆了栈的概念,一般人不这么干——就程序本身而言是有一定的bug的,大端模式跟小端模式下可能就是两种情况了
- 2 回答
- 0 关注
- 810 浏览
添加回答
举报