栈和队列
  ·栈和队列是两种常用的、重要的数据结构
  ·栈和队列是限定插入和删除只能在表的“端点”进行的线性表

  栈
    只能在队尾插入,只能在队尾删除 -- 后进后出
    表尾称为栈顶;表头称为栈底
    插入元素到栈顶(即表尾)的操作,称为入栈
    从栈顶删除最后一个元素的操作,称为出栈

  注意:函数调用的流程就是入栈和出栈的实现,遵循后调用的先返回
  队列
    只能在队尾插入,只能在对队头删除 -- 先进先出

  顺序栈的实现:

  

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef struct Stack{ // 顺序栈,存储类型为int
int *base; // 指向底部
int *top; // 指向顶部
int length;
}Stack;
typedef Stack *SqStack;
int initStack(SqStack *s);
int isEmpty(SqStack s);
int showStack(SqStack s);
int push(SqStack *s,int num);
int pop(SqStack *s);
// 顺序栈的算法
int main(void){
SqStack s = (SqStack)malloc(sizeof(Stack));
initStack(&s);
printf("顺序栈为空:%d \n",isEmpty(s));
printf("顺序栈长度:%d \n",getLength(s));
push(&s,);
push(&s,);
push(&s,);
pop(&s);
showStack(s);
}
// 遍历栈
int showStack(SqStack s){
while(s->top > s->base){
printf("值:%d \n", *(s->top-));
s->top--;
}
return OK;
}
// 入栈
int push (SqStack *s,int num){
// 判断是否上溢
if( getLength(*s) >= (*s)->length ) {
printf("上溢 \n");
return ERROR;
}
*(*s)->top++ = num; return OK;
}
// 出栈
int pop(SqStack *s){
// 判断是否下溢
if(getLength(*s) <= ){
printf("下溢 \n");
return ERROR;
}
(*s)->top--;
//free(temp);
return OK;
}
// 求长度
int getLength(SqStack s) {
int length = s->top - s->base; // 指针相减,结果为值的个数
return length;
}
// 判断是否为空
int isEmpty(SqStack s){
if(s->top == s->base){
return OK;
}else{
return ERROR;
}
}
// 构造一个空栈
int initStack(SqStack *s){
(*s)->base = (int*)malloc(sizeof(int));
if(!(*s)->base){
return ERROR; // 内存分配失败
}
(*s)->top = (*s)->base; // 栈顶指针等于栈底指针
(*s)->length = MAXSIZE;
return OK;
}

链栈的实现:

   

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef struct Stack{ // 链栈,存储类型为int
int x;
struct Stack *next; // 指向的是最后一个元素,入栈出栈都是它 if != NULL
}Stack;
typedef Stack *SqStack;
int initStack(SqStack *s);
int isEmpty(SqStack s);
int showStack(SqStack s);
int push(SqStack *s,int num);
int pop(SqStack *s);
// 链栈的算法
int main(void){
SqStack s = (SqStack)malloc(sizeof(Stack));
initStack(&s);
printf("链栈为空:%d \n",isEmpty(s));
printf("链栈长度:%d \n",getLength(s));
push(&s,);
push(&s,);
push(&s,);
pop(&s);
showStack(s);
}
// 遍历栈
int showStack(SqStack s){
while(s) {
printf("值:%d \n", s->x);
s = s->next;
}
return OK;
}
// 入栈
int push (SqStack *s,int num){
SqStack temp = (SqStack)malloc(sizeof(Stack));
temp->x = num;
temp->next = (*s); // 精髓之处
(*s) = temp; // !!! 指针的运用
return OK;
}
// 出栈
int pop(SqStack *s){
// 判断是否下溢
if(!(*s)){
printf("下溢 \n");
return ERROR;
}
SqStack temp = *s;
(*s) = (*s)->next;
free(temp); // 释放内存
return OK;
}
// 求长度
int getLength(SqStack s) {
int length = ;
while(s){
length++;
s = s->next;
}
return length;
}
// 判断是否为空
int isEmpty(SqStack s){
if(!s){
return OK;
}else{
return ERROR;
}
}
// 构造一个空栈
int initStack(SqStack *s){
// 构建一个空栈,栈顶指针置为空
*s = NULL;
return OK;
}

 循环顺序队列的实现:

  

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 100 // 最大队列长度
// 建立顺序队列类型
typedef struct{
int a[MAXSIZE];
int front; // 队头
int rear; // 队尾
}SqQueue; // 展示
void display(SqQueue sq)
{
int i=sq.front;
if(sq.front==sq.rear)
{
printf("none\n");
return ;
}
while(i!=sq.rear)
{
printf("%5d",sq.a[i]);
i++;
i=i%MAXSIZE;
}
printf("\n");
} // 队列的初始化
int init(SqQueue *q){
q->front = ;
q->rear = ;
} // 入队
void insert(SqQueue *sq,int x)
{
if((sq->rear+)%MAXSIZE==sq->front)
{
printf("full\n");
return ;
}
sq->a[sq->rear]=x;
sq->rear=(sq->rear+)%MAXSIZE;
printf("front = %d\n",sq->front);
printf("rear = %d\n",sq->rear);
} // 出队
void del(SqQueue *sq)
{
if(sq->front==sq->rear)
{
printf("none\n");
return ;
}
sq->front=(sq->front+)%MAXSIZE;
printf("front = %d\n",sq->front);
printf("rear = %d\n",sq->rear);
} int main(void){
SqQueue q;
init(&q);
insert(&q,);
insert(&q,);
del(&q);
insert(&q,);
insert(&q,);
display(q);
}

c数据结构 -- 栈与队列的更多相关文章

  1. 数据结构和算法(Golang实现)(14)常见数据结构-栈和队列

    栈和队列 一.栈 Stack 和队列 Queue 我们日常生活中,都需要将物品排列,或者安排事情的先后顺序.更通俗地讲,我们买东西时,人太多的情况下,我们要排队,排队也有先后顺序,有些人早了点来,排完 ...

  2. C数据结构-栈和队列,括号匹配举例---ShinePans

    1.栈和队列是两种特殊的线性表             运算操作被限定仅仅能在表的一端或两端插入,删除元素,故也称它们为限定的线性表结构 2.栈的基本运算 1).Stackinit(&s) 构 ...

  3. JavaScript数据结构——栈和队列

    栈:后进先出(LIFO)的有序集合 队列:先进先出(FIFO)的有序集合 --------------------------------------------------------------- ...

  4. &10 基本数据结构——栈,队列和链表

    #1,栈(stack) 定义[来自百度]:栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素 ...

  5. Python数据结构——栈、队列的实现(二)

    1. 一个列表实现两个栈 class Twostacks(object): def __init__(self): self.stack=[] self.a_size=0 self.b_size=0 ...

  6. Python数据结构——栈、队列的实现(一)

    1. 栈 栈(Stack)是限制插入和删除操作只能在一个位置进行的表,该位置是表的末端,称为栈的顶(top).栈的基本操作有PUSH(入栈)和POP(出栈).栈又被称为LIFO(后入先出)表. 1.1 ...

  7. js 数据结构-栈与队列

    /*[客栈的盘子/月井里的货物,后进先出]栈顶:最先入口/出口的位置栈底:最慢最晚出栈的位置*/ function Stack() { var item = []; //推(将货物推入月井) this ...

  8. 【栈和队列】5、队列概述与数组队列的基本实现 - Java

    3-5 数组队列 简单记录 - bobo老师的玩转算法系列–玩转数据结构 - 栈和队列 队列Queue 队列也是一种线性结构 相比数组,队列对应的操作是数组的子集 只能从一端(队尾)添加元素,只能从另 ...

  9. 【栈和队列】2、栈的基本实现 - Java

    简单记录 - bobo老师的玩转算法系列–玩转数据结构 - 栈和队列 栈的实现 Stack<E> void push(E) E pop() E peek() int getSize() b ...

随机推荐

  1. 【新人赛】阿里云恶意程序检测 -- 实践记录11.3 - n-gram模型调参

    主要工作 本周主要是跑了下n-gram模型,并调了下参数.大概看了几篇论文,有几个处理方法不错,准备下周代码实现一下. xgboost参数设置为: param = {'max_depth': 6, ' ...

  2. Linux内核镜像文件格式与生成过程(转)

    <Linux内核镜像格式>   Linux内核有多种格式的镜像,包括vmlinux.Image.zImage.bzImage.uImage.xipImage.bootpImage等. ➤k ...

  3. css中content-box和border-box当宽度为百分比时的位置区别,vw和%区别

    盒模型 参考代码 // CSS 部分 <style> .box1,.box2{ width: 200px; height: 200px; padding: 20px; margin: 20 ...

  4. 积分题1之来自G.Han的一道积分题

    今天,收到G.Han的提问,第一个是计算积分 \[\int_0^{\infty}{\frac{\ln x}{(x^2+1)^n}dx}\]顿时不明觉厉,然后在宝典<Table of Integr ...

  5. [AH2017/HNOI2017] 影魔 - 线段树

    #include<bits/stdc++.h> #define maxn 200010 using namespace std; int a[maxn],st[maxn][2],top,L ...

  6. 深入浅出Mybatis系列四-配置详解之typeAliases别名(mybatis源码篇)

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(三)---配置详解之properties ...

  7. Doing Homework HDU - 1074 状态压缩

    #include<iostream> #include<cstring> #include<cstdio> #include<string> #incl ...

  8. java字符串操作扩充:灵活截取字符串

    java字符串操作扩充:灵活截取字符串 public class StringUtil { static int varlen1; static int varlen2; static String ...

  9. 《深入理解java虚拟机》读书笔记三——第四章

    第四章 虚拟机性能监控与故障处理工具 1.JDK命令行工具 jps命令: 作用:列出正在运行的虚拟机进程. 格式:jps [option] [hostid] 选项:-q 只输出LVMID(Local ...

  10. Spring-Boot-2.0.0-M1版本将默认的数据库连接池从tomcat jdbc pool改为了hikari

    spring-configuration-metadata.json spring-boot-autoconfigure-2.0.0.M7.jar!/META-INF/spring-configura ...