| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 435 人关注过本帖
标题:有人回吗
只看楼主 加入收藏
新人新手
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-3-19
收藏
 问题点数:0 回复次数:3 
有人回吗
我的信箱是flyonsundy@126.COM
2007-03-19 14:20
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 
我已经给你发了一个去了。看看呀。。。

~~我的明天我知道~~
2007-03-19 15:15
e4lich
Rank: 2
等 级:新手上路
威 望:4
帖 子:182
专家分:0
注 册:2006-10-26
收藏
得分:0 

#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();
}


我只想变强!     
2007-03-19 15:51
e4lich
Rank: 2
等 级:新手上路
威 望:4
帖 子:182
专家分:0
注 册:2006-10-26
收藏
得分:0 

要这样输入:
-------------------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


我只想变强!     
2007-03-19 15:52
快速回复:有人回吗
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011499 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved