链栈的操作
来源:互联网 发布:爱奇艺网络电影排行榜 编辑:程序博客网 时间:2024/06/11 16:42
//链栈的基本操作#include <iostream>#include <stdio.h>#include <malloc.h>typedef struct node{ int data; struct node *next;}node,*linkstack;//初始化栈void init(linkstack *top){ *top=NULL;}//入栈void push(linkstack *top,int x){ linkstack s = (linkstack)malloc(sizeof(node)); s->data=x; s->next=*top; *top=s; printf("%d 入栈成功!\n",x);}//出栈void pop(linkstack *top){ linkstack s = (linkstack)malloc(sizeof(node)); int e=0; if(NULL==s) printf("分配失败"); else{ s = *top; e=s->data; *top=s->next; printf("出栈的元素是:%d ",e); } s=NULL;}//取栈顶元素int gettop(linkstack top){ if(top!=NULL) return top->data; else printf("栈空!不存在栈顶元素!\n"); return 0;}//打印栈内元素void display(linkstack s){ while(s!=NULL){ printf("%d ",s->data); s=s->next; }}int main(){ linkstack s; init(&s); push(&s,2); push(&s,3); display(s); printf("\n"); pop(&s); printf("\n"); push1(&s,4); printf("\n"); int x = gettop(s); printf("栈顶元素是:%d",x); return 0;
阅读全文
0 0
- 链栈的操作
- 链栈的操作
- 链栈的操作
- 链栈的操作
- 链栈的一些操作
- 链栈的相关操作
- 链栈的一些操作
- 链栈的基本操作
- 链栈的基本操作
- 链栈的基本操作
- 链栈的相关操作
- 链栈的基本操作
- 链栈的基本操作
- 链栈的基本操作
- 链栈的基本操作
- 链栈的基本操作
- 链栈的基本操作
- 链栈的主要操作
- 《计算机程序的构造和解释(第2版)》【PDF】下载
- Python Flask 学习笔记
- C#使用APlayer开发自制媒体播放器
- MVC和三层架构
- MySQL悲观锁
- 链栈的操作
- Vue2.0 使用vue-resource 模拟网络请求加载本地数据
- oracle 获取中文中的数字、字符串中的数字
- Python:linecache库
- 《计算机程序的构造和解释(第2版)》【PDF】下载
- C++构造函数 explicit关键字不起作用的一次小坑记录
- 求三角形面积
- 深入理解Android音视频同步机制(三)NuPlayer的avsync逻辑
- java 类的继承