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);
}

最新文章

  1. pyqt 发射接收信号
  2. Lattice FPGA 板子 调试笔记
  3. mysql 根据查询结果集更新
  4. RDS记录
  5. Debugging Process Startup
  6. Semi-prime H-numbers(筛法)
  7. xml格式化成json
  8. (十二)学习CSS之display属性
  9. C++ sizeof的使用总结
  10. Oracle 游标使用全解(转)
  11. codevs 4768 跳石头
  12. 【Asp.Net】后台生成控件并绑定事件
  13. 学习了php之后再来看php怎样学java
  14. FastDFS教程IV-文件服务器集群搭建
  15. python unicode 字节串转成中文问题
  16. pygme 安装
  17. CNN卷积层基础:特征提取+卷积核+反向传播
  18. Mybatis:Eclipse引入dtd约束文件使得xml文件有提示
  19. 清除的通用样式 css
  20. 突破MSDE 的2GB数据限制

热门文章

  1. python 传入任意多个参数(方法调用可传参或不传参)
  2. [LeetCode] 568. Maximum Vacation Days 最大化休假日
  3. python:使用多线程同时执行多个函数
  4. java的特性与优势
  5. 在ensp上STP配置和选路规则
  6. js继承的几种方法理解和代码演示
  7. isset和empty,isset和unset,__isset和__unset
  8. 少儿编程|Scratch编程教程系列合集,总有一款适合你
  9. java接口幂等性校验
  10. docker查看容器日志