数据结构-链栈(C语言)
来源:互联网 发布:淘宝后台操作流程视频 编辑:程序博客网 时间:2024/06/05 20:29
ChainStack.h
typedef struct Node{DATA data;struct Node *next;}ChainStack;//////有头结点的链栈 /////////ChainStack *CreatChainStack()//构建栈的头结点,返回该结点指针 {ChainStack *top;top=malloc(sizeof(struct Node));//头结点 top->next=NULL;//初始化 空栈 return top;}int ChainStackIsEmpty(ChainStack *top)// 判断栈是否为空,空返回1 {return(top->next==NULL);}void ChainStackPush(ChainStack *top,DATA data)//入栈 {ChainStack *node;if(!(node=malloc(sizeof(struct Node)))){printf("分配空间失败!\n");exit(0);}node->data=data;node->next=top->next;top->next=node;}DATA ChainStackPop(ChainStack *top)//出栈 {if(ChainStackIsEmpty(top)){printf("栈为空!\n");//return NULL; error:incompatible types when returning type 'void *' but 'DATA' was expected. //NULL空指针 exit(0);}else{DATA data;struct Node *node1;node1=top->next;//指向栈顶结点 data=node1->data;top->next=node1->next;//头指针指向栈顶下一个结点 free(node1);return data;}}ChainStackTest.c
#include<stdio.h>#include<stdlib.h>typedef struct {char key[15];char name[15];}DATA;#include"ChainStack.h"int main(){ChainStack *s;DATA data;s=CreatChainStack();printf("input key,name:");scanf("%s%s",data.key,data.name);ChainStackPush(s,data);printf("input key,name:");scanf("%s%s",data.key,data.name);ChainStackPush(s,data);printf("任意键出栈!\n");getch();data=ChainStackPop(s);printf("(%s,%s)\n",data.key,data.name);data=ChainStackPop(s);printf("(%s,%s)",data.key,data.name);free(s);getch();return 0;}
0 0
- C语言数据结构-链栈
- 数据结构-链栈(C语言)
- C语言-数据结构-链栈
- 基数排序 C语言数据结构
- 数据结构(C语言实现)
- C语言的数据结构
- C语言数据结构----链表
- C语言的数据结构
- 数据结构c语言学习
- 数据结构(C语言)
- 《数据结构》、《软件工程》、《C语言》
- 《数据结构》、《软件工程》、《C语言
- 数据结构c语言基础
- C语言之数据结构
- 数据结构--单链表(C语言)
- 数据结构--双链表(C语言)
- C语言数据结构-单链表
- C语言数据结构-双链表
- UE4下实现HTC Vive手柄拾取物品蓝图编程
- 微服务架构的学习
- Leetcode 62. Unique Paths
- 软件工程——机房收费各种图
- android之适配器
- 数据结构-链栈(C语言)
- Jenkins控制台输出乱码的问题
- NOIP2011 day2 T3 观光公交
- scala-problem41-50
- rtp打包发送H264文件
- mysql一列值排序后的序列号更新到另一列内
- IceDemos : IceGrid
- JDBC进行批处理的四种方式
- 【Android】自定义ScrollingTabs结合ViewPager实现指引的效果