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. matlab学习笔记10_6 字符串与数值间的转换以及进制之间的转换

    一起来学matlab-matlab学习笔记10 10_6 字符串与数值间的转换以及进制之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合 ...

  2. stylelint那些事儿

    一.参考文档 -   http://stylelint.cn/ -   https://stylelint.io/ -   https://stylelint.io/user-guide/exampl ...

  3. Jq如何获取并操作iframe中的元素?

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  4. saltstack配置文件详解

    软件依赖 Python版本大于2.6或版本小于3.0: 对Python版本要求 msgpack-python: SalStack消息交换库 YAML: SaltStack配置解析定义语法 Jinja2 ...

  5. 第3/7Beta冲刺

    1.团队成员 成员姓名 成员学号 秦裕航 201731062432(组长) 刘东 201731062227 张旭 201731062129 王伟 201731062214 2.SCRU部分 2.1各成 ...

  6. HTTP协议:从原理到流程|乐字节

    这次给大家带来的是HTTP协议:从原理到流程的详解 一.HTTP 协议 HTTP 协议(Hypertext Transfer Protocol, 超文本传输协议),是一个客户端请求和回应的 标准协议, ...

  7. TCP/IP学习笔记5--网络的构成要素

    人的灵魂来自一个完美的家园,那里没有任何污秽和丑陋,只有纯净和美丽.----大鱼海棠 1.通信媒介与数据链路 计算机之间通过各种电缆相互连接. 2.网卡 任何一台计算机接入网络都需要网卡,又称网络适配 ...

  8. PHP设计模式 - 门面模式

    门面模式 (Facade)又称外观模式,用于为子系统中的一组接口提供一个一致的界面.门面模式定义了一个高层接口,这个接口使得子系统更加容易使用:引入门面角色之后,用户只需要直接与门面角色交互,用户与子 ...

  9. Java开发笔记(一百四十)JavaFX的选择框

    与Swing一样,JavaFX依然提供了三种选择框,它们是复选框CheckBox.单选按钮RadioButton.下拉框ComboBox,分别说明如下: 一.复选框CheckBox复选框允许同时勾选多 ...

  10. free(分层图最短路)(2019牛客暑期多校训练营(第四场))

    示例: 输入: 3 2 1 3 11 2 12 3 2 输出:1 题意:求s,t最短路,可将k条边权值置零. 题解:分层图最短路原题 #include<bits/stdc++.h> usi ...