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 ...
随机推荐
- [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. ...
- [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 ...
- Kubernetes 控制器之 Service 讲解(七)
一.背景介绍 我们这里准备三台机器,一台master,两台node,采用kubeadm的方式进行安装的,安装过程大家可以参照我之前的博文. IP 角色 版本 192.168.1.200 master ...
- Jacob操作ppt
前几天使用Apache 的POI操作ppt,后来发现转成的图片出现乱码,而且处理了之后,还会有遗留 因此决定换一种处理方式 Jacob 是 JAVA-COM Bridge的缩写,是一个中间件,能够提供 ...
- 【神经网络与深度学习】【计算机视觉】SSD
SSD 转自:https://zhuanlan.zhihu.com/p/24954433?refer=xiaoleimlnote 参考: SSD: Single Shot MultiBox Detec ...
- Oracle Spatial图层元数据坐标范围影响R-TREE索引的ROOT MBR吗?
Oracle Spatial的空间索引R-TREE,其实现原理为一级级的MBR(最小定界矩形).我突然想到一个问题,它的ROOT MBR是怎么确定的?是根据元数据表user_sdo_geom_meta ...
- linux awk的用法
linux awk的用法 <pre>[root@iZ23uewresmZ ~]# cat /home/ceshis.txtb 12 42 30 b 03 43 25 a 08 10 16 ...
- linux中安装python3.7
linux中安装python3.7 1. 安装依赖包 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite- ...
- [CMD] Jenkins上执行robot命令如果出现fail不往下走其他的CMD命令了
需要在后面加上||exit 0 robot -o %disSection%.xml --include %disSection% -v ENV:%envBmk% .||exit 0
- MySQL练手小试题
创建表和数据 创建class表 create table class ( cid int(11) primary key auto_increment, caption varchar(32) not ...