栈的数据类型定义
来源:互联网 发布:scratch编程实例 编辑:程序博客网 时间:2024/05/21 16:16
LinkStack.h:
#ifndef _LINKSTACK_H_
#define _LINKSTACK_H_
#define SUCCESS 1000001
#define FAILURE 1000002
typedef int ElemType;
struct node
{
ElemType data;
struct node *next;
};
typedef struct node Node;
typedef Node *LinkNode;
struct stack
{
LinkNode top;
int count;
};
typedef struct stack Stack;
int StackInit(Stack **S);
int Push(Stack *S,ElemType e);
int StackLength(Stack *S);
int GetTop(Stack *S,ElemType *e);
int Pop(Stack *S,ElemType *e);
int StackClear(Stack *S);
int StackDestory(Stack **S);
#endif
LinkStack.c
#include <stdio.h>
#include "LinkStack.h"
#include <stdlib.h>
int StackInit(Stack **S)
{
(*S) = (Stack *)malloc(sizeof(Stack));
if(NULL == (*S))
{
return FAILURE;
}
(*S)->top = NULL;
(*S)->count = 0;
return SUCCESS;
}
int Push(Stack *S,ElemType e)
{
Node *p = (Node *)malloc(sizeof(Node));
if(NULL == p)
{
return FAILURE;
}
p->data = e;
p->next = S->top;
S->top = p;
S->count++;
return SUCCESS;
}
int StackLength(Stack *S)
{
return (S->count);
}
int GetTop(Stack *S,ElemType *e)
{
if(NULL == S->top)
{
return FAILURE;
}
*e = S->top->data;
return SUCCESS;
}
int Pop(Stack *S,ElemType *e)
{
if(NULL == S->top)
{
return FAILURE;
}
LinkNode p = S->top;
*e = p->data;
S->top = p->next;
free(p);
S->count--;
return SUCCESS;
}
int StackClear(Stack *S)
{
LinkNode p;
while(S->top)
{
p = S->top;
S->top = p->next;
free(p);
S->count--;
}
return SUCCESS;
}
int StackDestroy(Stack **S)
{
if(NULL == (*S))
{
return FAILURE;
}
free(*S);
(*S) = NULL;
return SUCCESS;
}
TestLinkStack.c
#include <stdio.h>
#include "LinkStack.h"
int main()
{
int ret,i;
Stack *s;
ElemType e;
ret = StackInit(&s);
if(FAILURE == ret)
{
printf("Init Failure!!\n");
}
else
{
printf("Init Success!!\n");
}
for(i = 0;i < 5; i++)
{
e = i + 1;
ret = Push(s,e);
if(FAILURE == ret)
{
printf("Push Failure!!\n");
}
else
{
printf("Push %d Success!!\n",e);
}
}
printf("Length is %d !\n",StackLength(s));
ret = GetTop(s,&e);
if(FAILURE == ret)
{
printf("GetTop Failure!!\n");
}
else
{
printf("Top is %d!\n",e);
}
for(i = 0;i < 2; i++)
{
ret = Pop(s,&e);
if(FAILURE == ret)
{
printf("Pop Faliure!!\n");
}
else
{
printf("Pop is %d Success!\n",e);
}
}
ret = GetTop(s,&e);
if(FAILURE == ret)
{
printf("GetTop Failure!!\n");
}
else
{
printf("Top is %d!\n",e);
}
ret = StackClear(s);
if(FAILURE == ret)
{
printf("CleanStack Failure!!\n");
}
else
{
printf("CleanStack Success!!\n");
}
ret = GetTop(s,&e);
if(FAILURE == ret)
{
printf("GetTop Failure!!\n");
}
else
{
printf("Top is %d!\n",e);
}
ret = StackDestroy(&s);
if(FAILURE == ret)
{
printf("Destory Failure!!\n");
}
else
{
printf("Destory Success!!\n");
}
printf("%p\n",s);
return 0;
}
- 栈的数据类型定义
- 不熟悉数据类型的定义
- TI 数据类型的定义
- 数据类型的定义
- 数据类型定义的重要性
- 抽象数据类型的定义
- 数据类型的定义
- 修改用户定义的数据类型
- 用户定义的数据类型转换
- 修改用户定义的数据类型
- VC扩充定义的数据类型
- 定义跨平台的数据类型
- 创建用户定义的数据类型
- Windows自己定义的数据类型
- 字典数据类型-字典的定义
- C++11定义的数据类型
- 关于基本数据类型的定义
- ORACLE定义长度的数据类型
- java.lang.NoClassDefFoundError: org/springframework/core/MethodClassKey
- 关于处理样本不平衡问题的Trick整理
- C# windows程序应用与JavaScript 程序交互实现例子
- 搞定这8个实战项目,秒杀80%人工智能工程师面试者
- 双系统下,解决ubuntu无法访问windows建立的硬盘
- 栈的数据类型定义
- 物料管理自动记账详解
- 【Scikit-Learn 中文文档】模型持久化
- 设置Android通知栏Notification的字体/图标颜色随背景色变化而变化
- js 中//<![CDATA[ 意义
- 存储过程第一讲:存储过程的创建,删除,修改,调用,查看
- 练习小算法
- 在ArcGIS里使用SciPy ,安装Anaconda集成环境
- eclipse找不到环境变量