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)当表中没有元素时称为 ...
随机推荐
- roject ..\appcompat_v7 is missing. Needed by eclipse 转AS项目时遇到的问题
参考的 http://www.cnblogs.com/vanezkw/p/4182917.html 去转换项目, 在第一步的时候就遇到问题 ,提示 missing 而那个又是兼容包 解决方法:项目右键 ...
- hihocoder #1388 : Periodic Signal fft
题目链接: https://hihocoder.com/problemset/problem/1388 Periodic Signal 时间限制:5000ms内存限制:256MB 问题描述 Profe ...
- python learning OOP2.py
class Student(object): pass s = Student() s.name = 'Chang' # 给一个实例动态绑定一个属性 print(s.name) def set_age ...
- 2016011998+sw
Coding.net原码仓库地址:https://git.coding.net/laolaf/sw.git 目录 1 需求分析 2 功能设计 3 设计实现 4 算法详解 5 测试运行 6 满意代码 1 ...
- week3c:个人博客作业
程序测试: 一个基本的测试. 在Visual Studio 2013 中使用C++单元测试 操作如下: 这是我学到的过程. 有复杂程序的测试.以后有时间再弄.
- jquery easyui datagrid getSelected getChecked获取选择行数据(勾选)数据
要想获得上图所选取的元素只能用getChecked getSelected不能进行多个选择,只能单选 /* getSelected取得选中的数据,否则为null */ var user=$(" ...
- Spring 中使用Properties文件
Spring提供了加载Properties文件的工具类:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. ...
- Android:java.lang.OutOfMemoryError:GC overhead limit exceeded
Android编译:java.lang.OutOfMemoryError:GC overhead limit exceeded 百度好多什么JVM啊之类的东西,新手简单粗暴的办法: 1.在的Model ...
- 自动创建web.xml
摘自:http://blog.csdn.net/weiral/article/details/51366485 今天在学习JSP时先创建了一个web项目,后来在用到web.xml文件时,才发现项目创建 ...
- lr几个常用的函数
将字符串保存为参数 lr_save_string("string you want to save", "arg_name"); 将int型数字保存为参数 lr ...