链式堆栈的初始化、出栈、入栈、取栈顶元素、判空
2017-04-03 20:56
246 查看
#include<stdio.h> #include<malloc.h> typedef int DataType; typedef struct snode //定义结构体 { DataType data; struct snode *next; }LSNode; void InitStact(LSNode **head) //初始化 { *head = (LSNode *)malloc(sizeof(LSNode)); (*head)->next = NULL; } int NotEmpty(LSNode *head){ //判断堆栈是否为空 if(head->next == NULL){ return 0; } else{ return 1; } } void Push(LSNode *head,DataType x) //入栈 { LSNode *p; p = (LSNode *)malloc(sizeof(LSNode)); p->data = x; p->next = head ->next; head->next = p; } int Pop(LSNode *head,DataType *x){ //出栈 LSNode *p = head->next; if(p == NULL){ printf("堆栈已空出错!"); return 0; } head->next = p->next; *x = p->data; free(p); return 1; } int Top(LSNode *head, DataType *x){ //取栈顶元素 LSNode *p = head->next; if(p == NULL){ printf("堆栈已空出错!"); return 0; } *x = p->data; return 1; } int main() { LSNode *s; int x; InitStact(&s); // scanf_s("%d",&x); for(x=0;x<10;++x) { Push(s,x+1); } Top(s,&x); printf("%d \n",x); while(NotEmpty(s)) { Pop(s,&x); printf("%d ",x); } return 0; }
相关文章推荐
- C语言实现顺序栈的基本操作(初始化、判断空、入栈、出栈、获取栈顶元素)
- 栈的顺序存储的初始化、判断栈空、入栈、出栈、读取栈顶元素、栈置空、求栈长操作
- 数据结构栈的数组实现初始化,入栈,出栈,获取栈顶元素,栈的长度等操作
- 栈的链式存储的定义、初始化、判断空、入栈、出栈、读取栈顶元素、求栈长度、清空栈操作
- C++栈的初始化,入栈,出栈,获取栈顶元素等操作
- 链栈:初始化、判断栈空、入栈、出栈、获取栈顶元素等
- 顺序栈的基本操作:初始化、进栈、出栈、读栈顶元素
- 顺序栈和链式栈的结构及其基本操作(置空,获取栈顶元素,入栈,出栈)
- 链栈的初始化 入栈 出栈 打印栈中的元素等基础内容
- 顺序栈的基本操作:初始化、进栈、出栈、读栈顶元素
- 数据结构:stack栈的初始化、入栈、出栈及显示栈元素
- c++ 实现顺序栈类(初始化,入栈,退栈。读栈顶元素以及顺序输出栈顶指针与栈中的元素<<计算机软件技术基础 徐士良>>
- 数据结构:stack栈的初始化、入栈、出栈及显示栈元素
- 数据结构之栈的初始化、创建、入栈、出栈、销毁-c++代码实现及运行实例结果
- 【数据结构】用C++编写栈及基本操作(包括入栈,出栈,获得栈顶,摧毁,清空等等)
- n个元素的入栈顺序有多少种出栈顺序?
- 数据结构之栈的初始化、创建、入栈、出栈、销毁-c++代码实现及运行实例结果
- 链栈的定义、初始化、出栈、入栈等操作
- 顺序栈的定义、初始化、出栈、入栈等操作 C++代码实现
- 链栈的置空入栈,出栈,和返回栈顶元素操作