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实现栈与队列的更多相关文章

  1. 学习javascript数据结构(一)——栈和队列

    前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...

  2. [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)

    再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...

  3. 剑指Offer面试题:6.用两个栈实现队列

    一.题目:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...

  4. C实现栈和队列

    这两天再学习了数据结构的栈和队列,思想很简单,可能是学习PHP那会没有直接使用栈和队列,写的太少,所以用具体代码实现的时候出现了各种错误,感觉还是C语言功底不行.栈和队列不论在面试中还是笔试中都很重要 ...

  5. JavaScript数组模拟栈和队列

    *栈和队列:js中没有真正的栈和队列的类型              一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭       FILO   何时使用:今后只要仅希望数组只能从一端进 ...

  6. 用JS描述的数据结构及算法表示——栈和队列(基础版)

    前言:找了上课时数据结构的教程来看,但是用的语言是c++,所以具体实现在网上搜大神的博客来看,我看到的大神们的博客都写得特别好,不止讲了最基本的思想和算法实现,更多的是侧重于实例运用,一边看一边在心里 ...

  7. JavaScript中的算法之美——栈、队列、表

    序 最近花了比较多的时间来学习前端的知识,在这个期间也看到了很多的优秀的文章,其中Aaron可能在这个算法方面算是我的启蒙,在此衷心感谢Aaron的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...

  8. Java数据结构和算法之栈与队列

    二.栈与队列 1.栈的定义 栈(Stack)是限制仅在表的一端进行插入和删除运算的线性表. (1)通常称插入.删除的这一端为栈顶(Top),另一端称为栈底(Bottom). (2)当表中没有元素时称为 ...

  9. python数据结构之栈、队列的实现

    这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...

  10. 栈和队列的面试题Java实现【重要】

    栈和队列: 面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min ...

随机推荐

  1. [LeetCode] 1. Two Sum 两数和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. opengl读取灰度图生成三维地形并添加光照

    转自:https://www.cnblogs.com/gucheng/p/10152889.html 准备第三方库 glew.freeglut.glm.opencv 准备一张灰度图 最终效果 代码如下 ...

  3. Influx Sql系列教程九:query数据查询基本篇二

    前面一篇介绍了influxdb中基本的查询操作,在结尾处提到了如果我们希望对查询的结果进行分组,排序,分页时,应该怎么操作,接下来我们看一下上面几个场景的支持 在开始本文之前,建议先阅读上篇博文: 1 ...

  4. 查看端口被哪个程序占用的DOS命令

    netstat -aon | findstr 80Proto Local Address Foreign Address State PID==== ============ ============ ...

  5. 查看LINUX进程内存占用情况及启动时间

    可以直接使用top命令后,查看%MEM的内容.可以选择按进程查看或者按用户查看,如想查看oracle用户的进程内存使用情况的话可以使用如下的命令: (1) top top命令是Linux下常用的性能分 ...

  6. [转帖]PC虚拟化主流:KVM、XEN、OpenVZ详解

    PC虚拟化主流:KVM.XEN.OpenVZ详解 https://zhuanlan.zhihu.com/p/90920566 1.pc虚拟化——KVM KVM是完整的硬件虚拟化,可以在Windows ...

  7. windows server系统打印服务配置

    系统环境:windows server 2008 R2 Enterprise Service Pack 1 安装内存:8G 系统类型:64位操作系统 目标:在此系统上开启打印服务,可以添加网络打印机 ...

  8. Find the median(线段树+离散化)(2019牛客暑期多校训练营(第七场))

    题目出处:Find the median 示例: 输入: 53 1 4 1 5 92 7 1 8 2 9 输出:3 4 5 4 5 说明:L = [3, 2 ,4, 1, 7],R = [4, 8, ...

  9. 我的 VSCode 配置

    VSCode 配置 先安装 Settings Sync 插件,然后点击"download from github"之类的一个链接,弹出一个输入框,输入 a5922d436b82dd ...

  10. Java多线程系列——锁的那些事

    引入 Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高的效率. 下面先带大家来总体预览一下锁的分类图 java锁的具体实现类 1.乐观锁 VS 悲观锁 乐观锁与悲观锁是 ...