C实现栈与队列
C实现栈与队列
做了个栈和队列的基础demo,写得比较快,就没有什么注释,其实看各个函数的名字就可以知道函数的作用了。
栈的实现
#include <stdio.h>
#include <stdlib.h>
typedef struct stack{
int *nums;
int top;
int size;
}*stack;
void changeSize(stack s,int size){
int *p = (int *)realloc(s->nums,size*sizeof(int));
s->nums = p;
s->size = size;
printf("Size has changed\n");
}
void push(stack s, int x){
if(s->top < s->size - 1){
s->top ++;
}else{
printf("The stack is full\n");
changeSize(s,2*s->size);
}
s->nums[s->top] = x;
}
void pop(stack s){
if(s->top < 0){
printf("No enough data\n");
exit(0);
}
s->top--;
}
int peek(stack s){
if(s->top < 0){
printf("The stack is empty");
return 0;
}
return s->nums[s->top];
}
_Bool isempty(stack s){
if(s->top == -1){
return 0;
}else{
return 1;
}
}
void clearstack(stack s){
free(s->nums);
s->nums = NULL;
s->top = -1;
s->size = 0;
}
int main(){
stack s = (stack)malloc(sizeof(struct stack));
int size = 10;
if(size < 1){
printf("Error in size\n");
exit(0);
}
s->nums = (int *)malloc(sizeof(int)*size);
s->top = -1;
s->size = size;
int a[12] = {3,2,1,4,6,5,8,7,0,9,6,4};
for(int i = 0; i < 12; i++){
push(s,a[i]);
printf("The num on the top is %d\n",peek(s));
}
pop(s);
printf("The num on the top after pop is %d\n",peek(s));
printf("The stack is %s\n",isempty==0?"empty":"not empty");
clearstack(s);
}
队列
#include <stdio.h>
#include <stdlib.h>
typedef struct queue{
int *nums;
int front,rear;
int size;
}*queue;
void addSize(queue s,int size){
int *p = (int *)realloc(s->nums,size*sizeof(int));
if(s->rear > s->front){
for(int i=0; i < s->front; i++){
if(i+s->size < size){
p[i+s->size] = s->nums[i];
}else{
p[(i+s->size)%size] = s->nums[i];
}
}
}
s->nums = p;
s->size = size;
printf("Size has changed\n");
}
void push(queue s, int x){
if((s->front+1)%(s->size) == s->rear){
printf("The queue is full!\n");
addSize(s,2*(s->size));
}else{
s->nums[(s->front)%(s->size)] = x;
}
s->front ++;
}
void pop(queue s){
if(s->rear == s->front){
printf("The queue is empty\n");
exit(0);
}
s->rear ++;
if(s->rear >= s->size){
s->rear = (s->rear)%(s->size);
}
}
int peekfront(queue s){
if(s->front == s->rear){
printf("The queue is empty\n");
return 0;
}
return s->nums[s->front-1];
}
int peekrear(queue s){
if(s->front == s->rear){
printf("The queue is empty\n");
return 0;
}
return s->nums[s->rear];
}
_Bool isempty(queue s){
if(s->front == s->rear){
return 0;
}else{
return 1;
}
}
void clearqueue(queue s){
free(s->nums);
s->nums = NULL;
s->front = 0;
s->rear = 0;
s->size = 0;
}
int main(){
queue s = (queue)malloc(sizeof(struct queue));
int size = 10;
if(size < 1){
printf("Error in size\n");
exit(0);
}
s->nums = (int *)malloc(sizeof(int)*size);
s->front = 0;
s->rear = 0;
s->size = size;
int a[12] = {3,2,1,4,5,7,6,9,8,0,8,9};
for(int i = 0; i < 12; i++){
push(s,a[i]);
printf("The num on the front is %d\n",peekfront(s));
}
pop(s);
printf("The num on the top after push is %d\n",peekfront(s));
printf("The num on the rear after pop is %d\n",peekrear(s));
pop(s);
printf("The num on the rear after pop is %d\n",peekrear(s));
printf("The queue is %s\n",isempty==0?"empty":"not empty");
clearqueue(s);
}
C实现栈与队列的更多相关文章
- 学习javascript数据结构(一)——栈和队列
前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...
- [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)
再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...
- 剑指Offer面试题:6.用两个栈实现队列
一.题目:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...
- C实现栈和队列
这两天再学习了数据结构的栈和队列,思想很简单,可能是学习PHP那会没有直接使用栈和队列,写的太少,所以用具体代码实现的时候出现了各种错误,感觉还是C语言功底不行.栈和队列不论在面试中还是笔试中都很重要 ...
- JavaScript数组模拟栈和队列
*栈和队列:js中没有真正的栈和队列的类型 一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭 FILO 何时使用:今后只要仅希望数组只能从一端进 ...
- 用JS描述的数据结构及算法表示——栈和队列(基础版)
前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...
- JavaScript中的算法之美——栈、队列、表
序 最近花了比较多的时间来学习前端的知识,在这个期间也看到了很多的优秀的文章,其中Aaron可能在这个算法方面算是我的启蒙,在此衷心感谢Aaron的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...
- Java数据结构和算法之栈与队列
二.栈与队列 1.栈的定义 栈(Stack)是限制仅在表的一端进行插入和删除运算的线性表. (1)通常称插入.删除的这一端为栈顶(Top),另一端称为栈底(Bottom). (2)当表中没有元素时称为 ...
- python数据结构之栈、队列的实现
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
- 栈和队列的面试题Java实现【重要】
栈和队列: 面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min ...
随机推荐
- (转载) AutoML 与轻量模型大列表
作者:guan-yuan 项目地址:awesome-AutoML-and-Lightweight-Models 博客地址:http://www.lib4dev.in/info/guan-yuan/aw ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [LeetCode] 801. Minimum Swaps To Make Sequences Increasing 最少交换使得序列递增
We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...
- Graphviz(02) 汉字subgraph不显示汉字的处理
1.按照官方教程设置 2.如果还不显示,在不显示的汉字前面加上一个“空格”,汉字就会正常显示. digraph idp_modules{ rankdir = TB; fontname = " ...
- 【视频开发】【CUDA开发】ffmpeg Nvidia硬件加速总结
原文链接:https://developer.nvidia.com/ffmpeg GPU-accelerated video processing integrated into the most p ...
- Block chain 1_The Long Road to Bitcoin
The path to Bitcoin is littered with the corpses of failed attempts. I've compiled a list of about a ...
- Fastjson爆出重大漏洞,攻击者可使整个业务瘫痪
360网络安全响应中心 https://cert.360.cn/warning/detail?id=82a509e4543433625d6fe4361b5802c9 报告编号:B6-2019-0905 ...
- macbook下使用pycharm2019版本配置远程连接服务器
pycharm提供了很方便的与服务器同步代码,并执行的插件.我在配置windows版的pycharm时配置成功,在挪用到mac上则遇到了些许问题,终于是解决了,在此记录配置的过程 目的:pycharm ...
- gitlab 安装、备份与还原及常见设置
gitlab 安装.备份与还原及常见设置 安装 安装过程比较简单,跑在 docker 上,执行命令即可 -v参数后面的值为卷的名称,自动创建数据卷(如果数据卷不存在) https://docs.git ...
- Javascript实现的智能消防栓状态监测画面
系统需要添加智能消防栓模块.集成了一家采用NbIOT通讯的智能消防栓产品.由第厂家平台对接NbIot特联网平台,我们平台提供一个api从第三方平台接收消防栓状态,用SignlaR把状态推送到前端.需要 ...