Chapter 4(栈与队列)

//*********************************stack_array.h************************************
#ifndef STACK_ARRAY_H
#define STACK_ARRAY_H
#define MAXSIZE 1000
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int datatype;
typedef struct stack_array
{
datatype data[MAXSIZE];
size_t top;
}SqStack;
//栈初始化
bool InitStack(SqStack *p);
//入栈
bool push(SqStack *stack,datatype e);
//出栈
bool pop(SqStack *stack,datatype *e);
#endif //STACK_ARRAY_H
//*********************************stack_array.c************************************
#include "stack_array.h"
//栈初始化
bool InitStack(SqStack *p)
{
if(NULL == p)return false;
p->top = -1;
return true;
}
//入栈
bool push(SqStack *stack,datatype e)
{
if(MAXSIZE-1 == stack->top)return false;//栈满
++stack->top;//入栈
stack->data[stack->top] = e;
return true;
}
//出栈
bool pop(SqStack *stack,datatype *e)
{
if(-1 == stack->top)return false;
*e = stack->data[stack->top];
--stack->top;
return true;
}
//*********************************stack_arrayTest.c************************************
#include "stack_array.h"
int main()
{
SqStack stack;
InitStack(&stack);
for(int i = 1;i <= 10;i++)
{
push(&stack,i);
}
datatype tmp;
for(int i =1;i <= 10;i++)
{
pop(&stack,&tmp);
printf("%d \n",tmp);
}
}
//*********************************stack_array.h************************************
#ifndef STACK_ARRAY_H
#define STACK_ARRAY_H
#define MAXSIZE 1000
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int datatype;
typedef struct stack_array
{
datatype data[MAXSIZE];
size_t top;
}SqStack;
//栈初始化
bool InitStack(SqStack *p);
//入栈
bool push(SqStack *stack,datatype e);
//出栈
bool pop(SqStack *stack,datatype *e);
#endif //STACK_ARRAY_H
//*********************************stack_array.c************************************
#include "stack_array.h"
//栈初始化
bool InitStack(SqStack *p)
{
if(NULL == p)return false;
p->top = -1;
return true;
}
//入栈
bool push(SqStack *stack,datatype e)
{
if(MAXSIZE-1 == stack->top)return false;//栈满
++stack->top;//入栈
stack->data[stack->top] = e;
return true;
}
//出栈
bool pop(SqStack *stack,datatype *e)
{
if(-1 == stack->top)return false;
*e = stack->data[stack->top];
--stack->top;
return true;
}
//*********************************stack_arrayTest.c************************************
#include "stack_array.h"
int main()
{
SqStack stack;
InitStack(&stack);
for(int i = 1;i <= 10;i++)
{
push(&stack,i);
}
datatype tmp;
for(int i =1;i <= 10;i++)
{
pop(&stack,&tmp);
printf("%d \n",tmp);
}
}
//*******************************stack_share.h***********************************
#ifndef STACK_SHARE_H
#define STACK_SHARE_H
#define MAXSIZE 1000
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <memory.h>
typedef int datatype;
typedef struct stack_share
{
datatype data[MAXSIZE];
size_t top1;
size_t top2;
}SqDoubleStack;
//初始化栈
void InitStack(SqDoubleStack *stack);
//压栈
bool push(SqDoubleStack *stack,datatype e,int num);
//弹栈
bool pop(SqDoubleStack *stack,datatype *e,int num);
#endif //STACK_SHARE_H
//*******************************stack_share.c***********************************
#include "stack_share.h"
//初始化栈
void InitStack(SqDoubleStack *stack)
{
stack->top1 = -1;
stack->top2 = MAXSIZE;
}
//压栈
bool push(SqDoubleStack *stack,datatype e,int num)
{
if(stack->top1+1 == stack->top2)return false;
if(1 == num)
{
stack->data[++stack->top1] = e;
}
else if(2 == num)
{
stack->data[--stack->top2] = e;
}
return true;
}
//弹栈
bool pop(SqDoubleStack *stack,datatype *e,int num)
{
if(1 == num)
{
if(-1 != stack->top1)return false;
*e = stack->data[stack->top1--];
}
else if(2 == num)
{
if(MAXSIZE != stack->top2)return false;
*e = stack->data[stack->top2++];
}
return true;
}
//*******************************stack_shareTest.c***********************************
#include "stack_share.h"
int main()
{
SqDoubleStack stack;
InitStack(&stack);
memset(stack.data,0,sizeof(int)*1000);
for(int i =1;i <= 10;i++)
{
push(&stack,i,1);
}
for(int i =1;i <= 10;i++)
{
push(&stack,i,2);
}
for(int i = 0;i < 1000;i++)
{
printf("%d ",stack.data[i]);
}
return 0;
}
//*******************************stack_share.h***********************************
#ifndef STACK_SHARE_H
#define STACK_SHARE_H
#define MAXSIZE 1000
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <memory.h>
typedef int datatype;
typedef struct stack_share
{
datatype data[MAXSIZE];
size_t top1;
size_t top2;
}SqDoubleStack;
//初始化栈
void InitStack(SqDoubleStack *stack);
//压栈
bool push(SqDoubleStack *stack,datatype e,int num);
//弹栈
bool pop(SqDoubleStack *stack,datatype *e,int num);
#endif //STACK_SHARE_H
//*******************************stack_share.c***********************************
#include "stack_share.h"
//初始化栈
void InitStack(SqDoubleStack *stack)
{
stack->top1 = -1;
stack->top2 = MAXSIZE;
}
//压栈
bool push(SqDoubleStack *stack,datatype e,int num)
{
if(stack->top1+1 == stack->top2)return false;
if(1 == num)
{
stack->data[++stack->top1] = e;
}
else if(2 == num)
{
stack->data[--stack->top2] = e;
}
return true;
}
//弹栈
bool pop(SqDoubleStack *stack,datatype *e,int num)
{
if(1 == num)
{
if(-1 != stack->top1)return false;
*e = stack->data[stack->top1--];
}
else if(2 == num)
{
if(MAXSIZE != stack->top2)return false;
*e = stack->data[stack->top2++];
}
return true;
}
//*******************************stack_shareTest.c***********************************
#include "stack_share.h"
int main()
{
SqDoubleStack stack;
InitStack(&stack);
memset(stack.data,0,sizeof(int)*1000);
for(int i =1;i <= 10;i++)
{
push(&stack,i,1);
}
for(int i =1;i <= 10;i++)
{
push(&stack,i,2);
}
for(int i = 0;i < 1000;i++)
{
printf("%d ",stack.data[i]);
}
return 0;
}
//****************************stack_link.h************************************
#ifndef STACK_LINK_H
#define STACK_LINK_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int datatype;
typedef struct StackNode
{
datatype data;
struct StackNode *next;
}StackNode;
typedef struct StackLink
{
StackNode *top;
size_t count;
}Stack;
//创建链栈
Stack *create();
//压栈
bool push(Stack *stack,datatype e);
//弹栈
bool pop(Stack *stack,datatype *e);
#endif //STACK_LINK_H
//****************************stack_link.c************************************
#include "stack_link.h"
//创建链栈
Stack *create()
{
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->top = NULL;
stack->count = 0;
return stack;
}
//压栈
bool push(Stack *stack,datatype e)
{
if(NULL == stack)return false;
StackNode *node = (StackNode *)malloc(sizeof(StackNode)); //申请新节点
node->data = e;//为新节点赋值
node->next = stack->top;
stack->top = node;//使top指向新节点
++stack->count;//count++
return true;
}
//弹栈
bool pop(Stack *stack,datatype *e)
{
if(NULL == stack->top)return false;//如果栈空,弹出失败
StackNode *curr = stack->top;//保存栈顶节点地址
StackNode *next = curr->next;//保存栈顶下一个节点地址
*e = curr->data;//保存弹出值
stack->top = next;//使top指向原栈顶下一个节点
--stack->count;
free(curr);
return true;
}
//****************************stack_linkTest.c************************************
#include "stack_link.h"
int main()
{
Stack *stack = create();
for(int i = 1;i <= 10;i++)
{
push(stack,i);
}
datatype tmp;
for(int i = 1;i <= 10;i++)
{
pop(stack,&tmp);
printf("%d ",tmp);
}
printf("\n");
return 0;
}
//****************************stack_link.h************************************
#ifndef STACK_LINK_H
#define STACK_LINK_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int datatype;
typedef struct StackNode
{
datatype data;
struct StackNode *next;
}StackNode;
typedef struct StackLink
{
StackNode *top;
size_t count;
}Stack;
//创建链栈
Stack *create();
//压栈
bool push(Stack *stack,datatype e);
//弹栈
bool pop(Stack *stack,datatype *e);
#endif //STACK_LINK_H
//****************************stack_link.c************************************
#include "stack_link.h"
//创建链栈
Stack *create()
{
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->top = NULL;
stack->count = 0;
return stack;
}
//压栈
bool push(Stack *stack,datatype e)
{
if(NULL == stack)return false;
StackNode *node = (StackNode *)malloc(sizeof(StackNode)); //申请新节点
node->data = e;//为新节点赋值
node->next = stack->top;
stack->top = node;//使top指向新节点
++stack->count;//count++
return true;
}
//弹栈
bool pop(Stack *stack,datatype *e)
{
if(NULL == stack->top)return false;//如果栈空,弹出失败
StackNode *curr = stack->top;//保存栈顶节点地址
StackNode *next = curr->next;//保存栈顶下一个节点地址
*e = curr->data;//保存弹出值
stack->top = next;//使top指向原栈顶下一个节点
--stack->count;
free(curr);
return true;
}
//****************************stack_linkTest.c************************************
#include "stack_link.h"
int main()
{
Stack *stack = create();
for(int i = 1;i <= 10;i++)
{
push(stack,i);
}
datatype tmp;
for(int i = 1;i <= 10;i++)
{
pop(stack,&tmp);
printf("%d ",tmp);
}
printf("\n");
return 0;
}
//************************************circular_queue.h****************************************
#ifndef CIRCULAR_LINK_H
#define CIRCULAR_LINK_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAXSIZE 1000
typedef int datatype;
typedef struct
{
datatype data[MAXSIZE];
size_t front;
size_t rear;
}SqQueue;
//初始化队列
void InitQueue(SqQueue *Q);
//求队列长度
size_t length(SqQueue Q);
//入队
bool EnQueue(SqQueue *Q,datatype e);
//出队
bool DeQueue(SqQueue *Q,datatype *e);
#endif //CIRCULAR_LINK_H
//************************************circular_queue.c****************************************
#include "circular_queue.h"
//初始化队列
void InitQueue(SqQueue *Q)
{
Q->front = 0;
Q->rear = 0;
}
//求队列长度
size_t length(SqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
//入队
bool EnQueue(SqQueue *Q,datatype e)
{
if((Q->rear+1)%MAXSIZE == Q->front)return false;
Q->data[Q->rear] = e;
Q->rear = (Q->rear+1)%MAXSIZE;
return false;
}
//出队
bool DeQueue(SqQueue *Q,datatype *e)
{
if(Q->front == Q->rear)return false;
*e = Q->data[Q->front];
Q->front = (Q->front+1)%MAXSIZE;
return true;
}
//************************************circular_queueTest.c****************************************
#include "circular_queue.h"
int main()
{
SqQueue Q;
InitQueue(&Q);
printf("length:%d \n",length(Q));
for(size_t i = 1;i <= 1000;i++)
{
EnQueue(&Q,i);
}
printf("length:%d \n",length(Q));
size_t temp;
for(size_t i = 1;i <= 10;i++)
{
DeQueue(&Q,&temp);
printf("%d ",temp);
}
printf("\n");
printf("length:%d \n",length(Q));
for(size_t i = 1;i <= 10;i++)
{
EnQueue(&Q,i);
}
printf("length:%d \n",length(Q));
for(size_t i = 1;i <= 1000;i++)
{
DeQueue(&Q,&temp);
printf("%d ",temp);
}
printf("length:%d \n",length(Q));
return 0;
}
x
//************************************circular_queue.h****************************************
#ifndef CIRCULAR_LINK_H
#define CIRCULAR_LINK_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAXSIZE 1000
typedef int datatype;
typedef struct
{
datatype data[MAXSIZE];
size_t front;
size_t rear;
}SqQueue;
//初始化队列
void InitQueue(SqQueue *Q);
//求队列长度
size_t length(SqQueue Q);
//入队
bool EnQueue(SqQueue *Q,datatype e);
//出队
bool DeQueue(SqQueue *Q,datatype *e);
#endif //CIRCULAR_LINK_H
//************************************circular_queue.c****************************************
#include "circular_queue.h"
//初始化队列
void InitQueue(SqQueue *Q)
{
Q->front = 0;
Q->rear = 0;
}
//求队列长度
size_t length(SqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
//入队
bool EnQueue(SqQueue *Q,datatype e)
{
if((Q->rear+1)%MAXSIZE == Q->front)return false;
Q->data[Q->rear] = e;
Q->rear = (Q->rear+1)%MAXSIZE;
return false;
}
//出队
bool DeQueue(SqQueue *Q,datatype *e)
{
if(Q->front == Q->rear)return false;
*e = Q->data[Q->front];
Q->front = (Q->front+1)%MAXSIZE;
return true;
}
//************************************circular_queueTest.c****************************************
#include "circular_queue.h"
int main()
{
SqQueue Q;
InitQueue(&Q);
printf("length:%d \n",length(Q));
for(size_t i = 1;i <= 1000;i++)
{
EnQueue(&Q,i);
}
printf("length:%d \n",length(Q));
size_t temp;
for(size_t i = 1;i <= 10;i++)
{
DeQueue(&Q,&temp);
printf("%d ",temp);
}
printf("\n");
printf("length:%d \n",length(Q));
for(size_t i = 1;i <= 10;i++)
{
EnQueue(&Q,i);
}
printf("length:%d \n",length(Q));
for(size_t i = 1;i <= 1000;i++)
{
DeQueue(&Q,&temp);
printf("%d ",temp);
}
printf("length:%d \n",length(Q));
return 0;
}
//*******************************queue_link.h****************************************
#ifndef QUEUE_LINK_H
#define QUEUE_LINK_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int datatype;
typedef struct QNode
{
datatype data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
//创建队列
LinkQueue create();
//入队
bool EnQueue(LinkQueue *Q,datatype e);
//出队
bool DeQueue(LinkQueue *Q,datatype *e);
#endif //QUEUE_LINK_H
//*******************************queue_link.c****************************************
#include "queue_link.h"
//创建队列
LinkQueue create()
{
QueuePtr head = (QueuePtr)malloc(sizeof(QNode));
head->next = NULL;
LinkQueue Q;
Q.front = head;
Q.rear = head;
return Q;
}
//入队
bool EnQueue(LinkQueue *Q,datatype e)
{
QueuePtr s = (QueuePtr)malloc(sizeof(QNode));//创建新节点
if(!s)return false;
s->data = e; //设置新节点数据
s->next = NULL;
Q->rear->next = s; //将原来的队尾指向新节点
Q->rear = s; //队尾指针指向s
return true;
}
//出队
bool DeQueue(LinkQueue *Q,datatype *e)
{
if(Q->front == Q->rear)return false;
QueuePtr node = Q->front->next;//保存要删除节点的地址
QueuePtr behind = node->next;//保存要删除节点下一个节点的地址
*e = node->data;//保存数据
Q->front->next = behind;//使front指向下一个节点
if(Q->rear == node)Q->rear = Q->front;//假如出队前队列只有一个元素,则出队后队列为空,队尾应该指向头节点。
free(node);
return true;
}
//*******************************queue_linkTest.c****************************************
#include "queue_link.h"
int main()
{
LinkQueue Q = create();
for(size_t i = 1;i <= 10;i++)
{
EnQueue(&Q,i);
}
size_t tmp;
for(size_t i = 1;i <= 10;i++)
{
DeQueue(&Q,&tmp);
printf("%d ",tmp);
}
printf("\n");
return 0;
}
x
//*******************************queue_link.h****************************************
#ifndef QUEUE_LINK_H
#define QUEUE_LINK_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int datatype;
typedef struct QNode
{
datatype data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front,rear;
}LinkQueue;
//创建队列
LinkQueue create();
//入队
bool EnQueue(LinkQueue *Q,datatype e);
//出队
bool DeQueue(LinkQueue *Q,datatype *e);
#endif //QUEUE_LINK_H
//*******************************queue_link.c****************************************
#include "queue_link.h"
//创建队列
LinkQueue create()
{
QueuePtr head = (QueuePtr)malloc(sizeof(QNode));
head->next = NULL;
LinkQueue Q;
Q.front = head;
Q.rear = head;
return Q;
}
//入队
bool EnQueue(LinkQueue *Q,datatype e)
{
QueuePtr s = (QueuePtr)malloc(sizeof(QNode));//创建新节点
if(!s)return false;
s->data = e; //设置新节点数据
s->next = NULL;
Q->rear->next = s; //将原来的队尾指向新节点
Q->rear = s; //队尾指针指向s
return true;
}
//出队
bool DeQueue(LinkQueue *Q,datatype *e)
{
if(Q->front == Q->rear)return false;
QueuePtr node = Q->front->next;//保存要删除节点的地址
QueuePtr behind = node->next;//保存要删除节点下一个节点的地址
*e = node->data;//保存数据
Q->front->next = behind;//使front指向下一个节点
if(Q->rear == node)Q->rear = Q->front;//假如出队前队列只有一个元素,则出队后队列为空,队尾应该指向头节点。
free(node);
return true;
}
//*******************************queue_linkTest.c****************************************
#include "queue_link.h"
int main()
{
LinkQueue Q = create();
for(size_t i = 1;i <= 10;i++)
{
EnQueue(&Q,i);
}
size_t tmp;
for(size_t i = 1;i <= 10;i++)
{
DeQueue(&Q,&tmp);
printf("%d ",tmp);
}
printf("\n");
return 0;
}
附件列表
Chapter 4(栈与队列)的更多相关文章
- Chapter 2 栈和队列
Chapter 2 栈和队列 1- 栈 当n个元素以某顺序进栈,可在任意时刻出栈,元素排列的顺序N满足Catalan()规则: 常用操作: 1 栈的初始化和定义: 2 元素x进栈: 3 ...
- 学习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)当表中没有元素时称为 ...
随机推荐
- webpack入门指南-step02
webpack 安装 1)安装前的准备:webpack是基于node环境的项目,所以使用前必须先安装node和npm. 在安装 Webpack 前,你本地环境需要支持 node.js.如果电脑没有装过 ...
- 个人作业2——APP案例分析
产品:网易LOFTER(乐乎) 网易LOFTER是网易旗下图片社交APP,产品覆盖web及移动各端. 网易LOFTER社区内汇聚了多领域的品质生活家与生活达人,包含女神.明星.穿搭.文具.旅行.美 ...
- vue-cli 安装时 npm 报错 errno -4048
如何解决vue-cli 安装时 npm 报错 errno -4048 第一种解决方法:以管理身份运行cmd.exe 第二种解决办法:在dos窗口输入命令 npm cache clean --fo ...
- 第二次结对作业-WordCount进阶需求
原博客 队友博客 github项目地址 目录 具体分工 需求分析 PSP表格 解题思路描述与设计实现说明 爬虫使用 代码组织与内部实现设计(类图) 算法的关键与关键实现部分流程图 附加题设计与展示 设 ...
- win7系统安装SQLServer2000的详细步骤(图文)
首先,如果以前安装的话,要删除干净.我也找了半天的网络资料.1.把原来SQLServer的安装目录 C:\Program Files\Microsoft SQL Server 删除2.所有SQLSe ...
- APP端测试与web端测试的区别
想要知道APP端测试与web端测试的区别 ,那么我们就要先来了解,web和app的区别. web项目,一般都是b/s架构,基于浏览器的,而app则是c/s的,必须要有客户端.那么在系统测试测试的时候就 ...
- 转载LoadRunner的常用Java API
Java API是访问Vuser函数的基础,通过LoadRunner的Java API可以在脚本中很容易地创建事务与并发点.获取用户信息等功能. 1. 事务函数(Transaction Functio ...
- HTTP压力测试工具wrk的安装及测试
本次在VMware虚拟机的CentOS6.3系统中进行安装wrk压测工具,具体如下: 一.预先安装需求项 为了安装顺利,不受权限的限制,首先可以把用户切换为root用户# su + 输入root用户对 ...
- Spring4+Spring MVC+MyBatis整合思路
1.Spring框架的搭建 这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件 ...
- poj3320 Jessica's Reading Problem
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...