栈的实现及基本操作
来源:互联网 发布:gradle java home 编辑:程序博客网 时间:2024/05/23 18:53
转载修改:
VC6.0 测试通过。
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <string.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;
};
struct STACK
...{
STU *base;
STU *top;
int stacksize;
};
int InitStack(STACK **S);
int DestroyStack(STACK *S);
int ClearStack(STACK *S);
int StackEmpty(STACK S);
int StackLength(STACK S);
int GetTop(STACK S,STU *e);
int Push(STACK *S,STU e);
int Pop(STACK *S,STU *e);
int StackTraverse(STACK S,Status (*visit)());
/**//*
* 初始化栈
*/
int InitStack(STACK **S)
...{
(*S)=(STACK *) malloc(sizeof(STACK));
(*S)->base=(STU *)malloc(STACK_INIT_SIZE *sizeof(STU));
if(!(*S)->base)
exit(-1);
(*S)->top=(*S)->base;
(*S)->stacksize=STACK_INIT_SIZE;
return OK;
}
int DestroyStack(STACK *S)
...{
free(S->base);
free(S);
return 1;
}
int ClearStack(STACK *S)
...{
S->top=S->base;
return 1;
}
int StackEmpty(STACK S)
...{
if(S.top==S.base) return TRUE;
else
return FALSE;
}
int StackLength(STACK S)
...{
int i;
STU *p;
i=0;
p=S.top;
while(p!=S.base)
...{
p--;
i++;
}
return i;
}
int GetTop(STACK S,STU *e)
...{
if(S.top==S.base) return ERROR;
*e=*(S.top-1);
return OK;
}
int Push(STACK *S,STU e)
...{
// 栈空间的增加
if(S->top - S->base>=S->stacksize)
...{
S->base=(STU *) realloc(S->base,
(S->stacksize + STACKINCREMENT) * sizeof(STU));
if(!S->base)exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize += STACKINCREMENT;
}
*(S->top++)=e;
return OK;
}
int Pop(STACK *S)
...{
if(S->top==S->base) return ERROR;
*--S->top;
printf("%s %s %d %d ",(*S).top->name, (*S).top->stuno,(*S).top->age,(*S).top->score);
// 未释放空间
return OK;
}
int StackPrintElem(STU * e)
...{
printf("%s %s %d %d ",e->name,e->stuno,e->age,e->score);
return 1;
}
int StackTraverse(STACK S)
...{
while(S.top!=S.base)
...{
--S.top;
printf("%s %s %d %d ",S.top->name, S.top->stuno,S.top->age,S.top->score);
}
return 1;
}
main()
...{
STU e;
STACK *Sa;
printf(" -------------------STACK Demo is running...---------------- ");
printf("First is Push function. ");
InitStack(&Sa);
printf("Now Stack is Empty. ");
strcpy(e.name,"stu1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
Push(Sa,e);
printf("Now Stack has %d element. ",StackLength(*Sa));
strcpy(e.name,"stu2");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
Push(Sa,e);
printf("Now Stack has another element. ");
printf("Now Stack has %d element. ",StackLength(*Sa));
printf("Now Pop Stack,the top elem put into variable e. ");
Pop(Sa);
printf("Now Stack has %d element. ",StackLength(*Sa));
strcpy(e.name,"stu3");
strcpy(e.stuno,"100003");
e.age=30;
e.score=3000;
Push(Sa,e);
printf("Let's see the left of Stack's elem: ");
StackTraverse(*Sa);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <string.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;
};
struct STACK
...{
STU *base;
STU *top;
int stacksize;
};
int InitStack(STACK **S);
int DestroyStack(STACK *S);
int ClearStack(STACK *S);
int StackEmpty(STACK S);
int StackLength(STACK S);
int GetTop(STACK S,STU *e);
int Push(STACK *S,STU e);
int Pop(STACK *S,STU *e);
int StackTraverse(STACK S,Status (*visit)());
/**//*
* 初始化栈
*/
int InitStack(STACK **S)
...{
(*S)=(STACK *) malloc(sizeof(STACK));
(*S)->base=(STU *)malloc(STACK_INIT_SIZE *sizeof(STU));
if(!(*S)->base)
exit(-1);
(*S)->top=(*S)->base;
(*S)->stacksize=STACK_INIT_SIZE;
return OK;
}
int DestroyStack(STACK *S)
...{
free(S->base);
free(S);
return 1;
}
int ClearStack(STACK *S)
...{
S->top=S->base;
return 1;
}
int StackEmpty(STACK S)
...{
if(S.top==S.base) return TRUE;
else
return FALSE;
}
int StackLength(STACK S)
...{
int i;
STU *p;
i=0;
p=S.top;
while(p!=S.base)
...{
p--;
i++;
}
return i;
}
int GetTop(STACK S,STU *e)
...{
if(S.top==S.base) return ERROR;
*e=*(S.top-1);
return OK;
}
int Push(STACK *S,STU e)
...{
// 栈空间的增加
if(S->top - S->base>=S->stacksize)
...{
S->base=(STU *) realloc(S->base,
(S->stacksize + STACKINCREMENT) * sizeof(STU));
if(!S->base)exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize += STACKINCREMENT;
}
*(S->top++)=e;
return OK;
}
int Pop(STACK *S)
...{
if(S->top==S->base) return ERROR;
*--S->top;
printf("%s %s %d %d ",(*S).top->name, (*S).top->stuno,(*S).top->age,(*S).top->score);
// 未释放空间
return OK;
}
int StackPrintElem(STU * e)
...{
printf("%s %s %d %d ",e->name,e->stuno,e->age,e->score);
return 1;
}
int StackTraverse(STACK S)
...{
while(S.top!=S.base)
...{
--S.top;
printf("%s %s %d %d ",S.top->name, S.top->stuno,S.top->age,S.top->score);
}
return 1;
}
main()
...{
STU e;
STACK *Sa;
printf(" -------------------STACK Demo is running...---------------- ");
printf("First is Push function. ");
InitStack(&Sa);
printf("Now Stack is Empty. ");
strcpy(e.name,"stu1");
strcpy(e.stuno,"100001");
e.age=80;
e.score=1000;
Push(Sa,e);
printf("Now Stack has %d element. ",StackLength(*Sa));
strcpy(e.name,"stu2");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
Push(Sa,e);
printf("Now Stack has another element. ");
printf("Now Stack has %d element. ",StackLength(*Sa));
printf("Now Pop Stack,the top elem put into variable e. ");
Pop(Sa);
printf("Now Stack has %d element. ",StackLength(*Sa));
strcpy(e.name,"stu3");
strcpy(e.stuno,"100003");
e.age=30;
e.score=3000;
Push(Sa,e);
printf("Let's see the left of Stack's elem: ");
StackTraverse(*Sa);
return 0;
}
- 栈的实现及基本操作
- 栈的实现及基本操作
- 栈的基本操作及实现
- 栈-链栈的基本操作及实现
- 堆的概念及基本操作实现
- 队列的基本操作及实现
- 重学数据结构004——栈的基本操作及实现(数组实现)
- Lua的栈及基本栈操作
- 链栈的基本操作及回文
- 栈的基本操作及应用
- 算法与数据结构--图的实现、基本操作及应用
- qt 图片显示及基本操作的实现
- 队列基本操作及银行简易排号的实现
- 单向链表的基本操作及逆序实现
- 有序线性表的基本操作及实现
- 实验一、线性表的基本操作及实现
- 顺序队列及链队列的基本操作实现
- 重学数据结构003——栈的基本操作及实现(链式存储)
- GreaseMokey 里面搞笑的JavaScript代码
- 2006年4月度国际标准化组织C++会议纪要[原文发表时间:2006年5月3日]
- 用GDB调试程序(二)
- 学好VC++的十大良好习惯
- 用GDB调试程序(一)
- 栈的实现及基本操作
- 使用PHP生成1000个随机注册码
- 关于CSocket类的Receive超时的问题
- Inside i++
- 哈希表心得
- VC之美化界面篇
- 系统架构师是怎样炼成的?(转)
- 用C写有面向对象特点的程序
- OO的bug,C++的bug,还是编译器的bug?