C实现栈与队列

做了个栈和队列的基础demo,写得比较快,就没有什么注释,其实看各个函数的名字就可以知道函数的作用了。

栈的实现

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct stack{
  4. int *nums;
  5. int top;
  6. int size;
  7. }*stack;
  8. void changeSize(stack s,int size){
  9. int *p = (int *)realloc(s->nums,size*sizeof(int));
  10. s->nums = p;
  11. s->size = size;
  12. printf("Size has changed\n");
  13. }
  14. void push(stack s, int x){
  15. if(s->top < s->size - 1){
  16. s->top ++;
  17. }else{
  18. printf("The stack is full\n");
  19. changeSize(s,2*s->size);
  20. }
  21. s->nums[s->top] = x;
  22. }
  23. void pop(stack s){
  24. if(s->top < 0){
  25. printf("No enough data\n");
  26. exit(0);
  27. }
  28. s->top--;
  29. }
  30. int peek(stack s){
  31. if(s->top < 0){
  32. printf("The stack is empty");
  33. return 0;
  34. }
  35. return s->nums[s->top];
  36. }
  37. _Bool isempty(stack s){
  38. if(s->top == -1){
  39. return 0;
  40. }else{
  41. return 1;
  42. }
  43. }
  44. void clearstack(stack s){
  45. free(s->nums);
  46. s->nums = NULL;
  47. s->top = -1;
  48. s->size = 0;
  49. }
  50. int main(){
  51. stack s = (stack)malloc(sizeof(struct stack));
  52. int size = 10;
  53. if(size < 1){
  54. printf("Error in size\n");
  55. exit(0);
  56. }
  57. s->nums = (int *)malloc(sizeof(int)*size);
  58. s->top = -1;
  59. s->size = size;
  60. int a[12] = {3,2,1,4,6,5,8,7,0,9,6,4};
  61. for(int i = 0; i < 12; i++){
  62. push(s,a[i]);
  63. printf("The num on the top is %d\n",peek(s));
  64. }
  65. pop(s);
  66. printf("The num on the top after pop is %d\n",peek(s));
  67. printf("The stack is %s\n",isempty==0?"empty":"not empty");
  68. clearstack(s);
  69. }

队列

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct queue{
  4. int *nums;
  5. int front,rear;
  6. int size;
  7. }*queue;
  8. void addSize(queue s,int size){
  9. int *p = (int *)realloc(s->nums,size*sizeof(int));
  10. if(s->rear > s->front){
  11. for(int i=0; i < s->front; i++){
  12. if(i+s->size < size){
  13. p[i+s->size] = s->nums[i];
  14. }else{
  15. p[(i+s->size)%size] = s->nums[i];
  16. }
  17. }
  18. }
  19. s->nums = p;
  20. s->size = size;
  21. printf("Size has changed\n");
  22. }
  23. void push(queue s, int x){
  24. if((s->front+1)%(s->size) == s->rear){
  25. printf("The queue is full!\n");
  26. addSize(s,2*(s->size));
  27. }else{
  28. s->nums[(s->front)%(s->size)] = x;
  29. }
  30. s->front ++;
  31. }
  32. void pop(queue s){
  33. if(s->rear == s->front){
  34. printf("The queue is empty\n");
  35. exit(0);
  36. }
  37. s->rear ++;
  38. if(s->rear >= s->size){
  39. s->rear = (s->rear)%(s->size);
  40. }
  41. }
  42. int peekfront(queue s){
  43. if(s->front == s->rear){
  44. printf("The queue is empty\n");
  45. return 0;
  46. }
  47. return s->nums[s->front-1];
  48. }
  49. int peekrear(queue s){
  50. if(s->front == s->rear){
  51. printf("The queue is empty\n");
  52. return 0;
  53. }
  54. return s->nums[s->rear];
  55. }
  56. _Bool isempty(queue s){
  57. if(s->front == s->rear){
  58. return 0;
  59. }else{
  60. return 1;
  61. }
  62. }
  63. void clearqueue(queue s){
  64. free(s->nums);
  65. s->nums = NULL;
  66. s->front = 0;
  67. s->rear = 0;
  68. s->size = 0;
  69. }
  70. int main(){
  71. queue s = (queue)malloc(sizeof(struct queue));
  72. int size = 10;
  73. if(size < 1){
  74. printf("Error in size\n");
  75. exit(0);
  76. }
  77. s->nums = (int *)malloc(sizeof(int)*size);
  78. s->front = 0;
  79. s->rear = 0;
  80. s->size = size;
  81. int a[12] = {3,2,1,4,5,7,6,9,8,0,8,9};
  82. for(int i = 0; i < 12; i++){
  83. push(s,a[i]);
  84. printf("The num on the front is %d\n",peekfront(s));
  85. }
  86. pop(s);
  87. printf("The num on the top after push is %d\n",peekfront(s));
  88. printf("The num on the rear after pop is %d\n",peekrear(s));
  89. pop(s);
  90. printf("The num on the rear after pop is %d\n",peekrear(s));
  91. printf("The queue is %s\n",isempty==0?"empty":"not empty");
  92. clearqueue(s);
  93. }

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] 149. Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  2. [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  3. Kubernetes 控制器之 Service 讲解(七)

    一.背景介绍 我们这里准备三台机器,一台master,两台node,采用kubeadm的方式进行安装的,安装过程大家可以参照我之前的博文. IP 角色 版本 192.168.1.200 master ...

  4. Jacob操作ppt

    前几天使用Apache 的POI操作ppt,后来发现转成的图片出现乱码,而且处理了之后,还会有遗留 因此决定换一种处理方式 Jacob 是 JAVA-COM Bridge的缩写,是一个中间件,能够提供 ...

  5. 【神经网络与深度学习】【计算机视觉】SSD

    SSD 转自:https://zhuanlan.zhihu.com/p/24954433?refer=xiaoleimlnote 参考: SSD: Single Shot MultiBox Detec ...

  6. Oracle Spatial图层元数据坐标范围影响R-TREE索引的ROOT MBR吗?

    Oracle Spatial的空间索引R-TREE,其实现原理为一级级的MBR(最小定界矩形).我突然想到一个问题,它的ROOT MBR是怎么确定的?是根据元数据表user_sdo_geom_meta ...

  7. linux awk的用法

    linux awk的用法 <pre>[root@iZ23uewresmZ ~]# cat /home/ceshis.txtb 12 42 30 b 03 43 25 a 08 10 16 ...

  8. linux中安装python3.7

    linux中安装python3.7 1. 安装依赖包 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite- ...

  9. [CMD] Jenkins上执行robot命令如果出现fail不往下走其他的CMD命令了

    需要在后面加上||exit 0 robot -o %disSection%.xml --include %disSection% -v ENV:%envBmk% .||exit 0

  10. MySQL练手小试题

    创建表和数据 创建class表 create table class ( cid int(11) primary key auto_increment, caption varchar(32) not ...