栈的基本操作及实现
来源:互联网 发布:电气设计软件有哪些 编辑:程序博客网 时间:2024/06/10 16:04
#include <stdio.h>#include <stdbool.h>#include <stdlib.h>#include <iostream>typedef int ElementType;//定义结点typedef struct LinkNode{ ElementType data; struct LinkNode* next;} *StackNode, *QueueNode;//定义栈typedef struct { LinkNode *top;} LinkStack;//初始化栈LinkStack* InitStack(LinkStack *L){ printf("初始化栈\n"); L = (LinkStack*)malloc(sizeof(LinkStack)); L->top = (LinkNode*)malloc(sizeof(LinkNode)); L->top->next = NULL; return L;}//判断栈是否为空bool isStackEmpty(LinkStack* L){ printf("判断栈是否为空\n"); if (L->top->next == NULL) return true; else return false;}//入栈StackNode Push(LinkNode *head, ElementType data){ //首先判断栈是否已满,但这里是链栈,则不做是否已满的判断 //我们在这个子方法里面新建一个节点,判断这个节点是否能被分配空间 StackNode S = (StackNode)malloc(sizeof(LinkNode)); if (S == NULL){ printf("没有多余的空间可以分配\n"); exit(0); } S->data = data; printf("S->data=%d, data=%d\n", S->data, data); S->next = head->next; head->next = S; printf("head->next->data=%d\n", head->next->data); return head;}//出栈,返回要出栈的元素ElementType Pop(LinkNode* head){ printf("进行一次pop,"); ElementType data; //首先判断栈是否为空 if (head->next == NULL){ printf("栈为空,无法出栈\n");getchar();getchar();exit(0); } data = head->next->data; LinkNode* node = head->next; head->next = node->next; free(node); //printf("栈顶元素现在是:%d\n", head->next->data); return data;}//返回栈顶元素ElementType GetTop(LinkNode* head){ if (head->next == NULL) printf("栈为空,无法输出元素\n"); ElementType data = head->next->data; printf("栈顶元素为:%d\n", data);}// int main(void)// {// LinkStack* Stack;// Stack = InitStack(Stack);// if (isStackEmpty(Stack) == true){// printf("栈为空,继续进行程序\n");// }// Stack->top = Push(Stack->top, 5);// printf("Stack->top->data=%d\n", Stack->top->data);// GetTop(Stack->top);// //printf("%d ", Pop(Stack->top));// Stack->top = Push(Stack->top, 3);// GetTop(Stack->top);// Stack->top = Push(Stack->top, 1);// GetTop(Stack->top);// Stack->top = Push(Stack->top, 7);// GetTop(Stack->top);// printf("出栈的元素是:%d\n", Pop(Stack->top));// printf("出栈的元素是:%d\n", Pop(Stack->top));// printf("出栈的元素是:%d\n", Pop(Stack->top));// Stack->top = Push(Stack->top, 2);// GetTop(Stack->top);// getchar();// getchar();// return 0;// }
阅读全文
0 0
- 栈的实现及基本操作
- 栈的实现及基本操作
- 栈的基本操作及实现
- 栈-链栈的基本操作及实现
- 堆的概念及基本操作实现
- 队列的基本操作及实现
- 重学数据结构004——栈的基本操作及实现(数组实现)
- Lua的栈及基本栈操作
- 链栈的基本操作及回文
- 栈的基本操作及应用
- 算法与数据结构--图的实现、基本操作及应用
- qt 图片显示及基本操作的实现
- 队列基本操作及银行简易排号的实现
- 单向链表的基本操作及逆序实现
- 有序线性表的基本操作及实现
- 实验一、线性表的基本操作及实现
- 顺序队列及链队列的基本操作实现
- 重学数据结构003——栈的基本操作及实现(链式存储)
- Inno Setup入门(十一)——完成安装后执行某些程序
- 数据自动映射时模型类非关联性字段映射方法
- 二级联动
- 每日练习2017-10-31
- gitbash下命令行操作技巧
- 栈的基本操作及实现
- 我是一个线程
- vue项目中axios跨域的问题
- 每日练习2017-11-01
- 直插电解电容-耐压值-容值-物理尺寸对应关系
- 小李的数据库之旅(上)
- redis整合spring(redisTemplate工具类)
- 利用vue-cli创建新项目和引入Bootstrap的方法
- 交换机从体系结构分类