队列

作者: qianranow | 来源:发表于2018-09-11 17:41 被阅读33次

0. 顺序存储结构


#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 20

typedef struct Node {
  
  int Data[MAXSIZE];
  
  int front;
  
  int rear;
  
}QNode, *Queue;

Queue CreateQueue(int MaxSize) {
  
  Queue Q;
  
  Q = (Queue)malloc(sizeof(QNode));
  
  Q -> front = 0;
  
  Q -> rear = 0;
  
  return Q;
  
}

void AddQ(Queue PtrQ, int item) {
  
  if ((PtrQ -> rear + 1) % MAXSIZE == PtrQ -> front) {
    
    printf("队列满");
    
    return;
    
  }
  
  PtrQ -> rear = (PtrQ -> rear + 1) % MAXSIZE;
  
  PtrQ -> Data[PtrQ -> rear] = item;
  
}

int DeleteQ(Queue PtrQ) {
  
  if (PtrQ -> front == PtrQ -> rear) {
    
    printf("队列空");
    
    return -2;
    
  } else {
    
    PtrQ -> front = (PtrQ -> front + 1) % MAXSIZE;
    
    return PtrQ -> Data[PtrQ -> front];
    
  }
  
}

int Length(Queue Q) {
  
  return (Q -> rear - Q -> front + MAXSIZE) % MAXSIZE;
  
}

1. 链式存储结构


#include <stdio.h>
#include <stdlib.h>

struct Node {
  
  int Data;
  
  struct Node *Next;
  
};

struct QNode {
  
  struct Node *front;
  
  struct Node *rear;
  
};

typedef struct QNode * Queue;

// 创建空队列
Queue CreateQueue() {
  
  Queue Q;
  
  Q = (Queue)malloc(sizeof(struct QNode));
  
  Q -> front = NULL;
  
  Q -> rear = NULL;
  
  return Q;
  
}

void AddQ(Queue PtrQ, int item) {
  
  struct Node *TmpCell = (struct Node *)malloc(sizeof(struct Node));
  
  TmpCell -> Data = item;
  
  TmpCell -> Next = NULL;
  
  if (PtrQ -> front == NULL) { // 队列为空,进第一个元素
    
    PtrQ -> rear = PtrQ -> front = TmpCell;
    
  } else {
    
    PtrQ -> rear -> Next = TmpCell;
    
    PtrQ -> rear = TmpCell;
    
  }

}

int DeleteQ(Queue PtrQ) {
  
  struct Node *FrontCell;
  
  int FrontItem;
  
  if (PtrQ -> front == NULL) {
    
    printf("队列空");
    
    return -2;
    
  }
  
  FrontCell = PtrQ -> front;
  
  if (PtrQ -> front == PtrQ ->rear) { // 队列只有一个元素
    
    PtrQ -> front = PtrQ -> rear = NULL;
    
  } else {
    
    PtrQ -> front = FrontCell -> Next;
    
  }
  
  FrontItem = FrontCell -> Data;
  
  free(FrontCell);
  
  return FrontItem;
  
}

相关文章

  • 队列

    队列特性 对比队列和栈 基于数组的队列 对比队列学习循环队列 循环队列难点 阻塞队列 并发队列 应用:线程池中拒绝...

  • 队列

    文章结构 什么是队列 实现队列顺序队列链式队列循环队列 Java中的队列 1. 什么是队列 队列也是一种操作受限的...

  • iOS底层-- GCD源码分析(1)-- dispatch_qu

    手动目录认识队列队列的结构队列的产生主队列全局队列创建的队列管理队列 代码版本dispatch version :...

  • 队列,异步,同步,线程通俗理解

    一、队列 串行队列 并行队列 主队列(只在主线程执行的串行队列) 全局队列(系统的并行队列) 二、 任务(是否具有...

  • GCD基础总结一

    上代码~ 同步串行队列 同步并行队列 异步串行队列 异步并行队列 主队列同步 会卡住 主队列异步

  • OC多线程

    队列创建 线程与队列 队列线程间通信 队列组

  • GCD

    获得主队列 获得全局队列 串行队列 异步队列 同步队列 阻隔队列 (像栅栏一样 ) 例如 A -->栅栏 --...

  • 数据结构第三篇 队列

    队列的特性 前进先出。 我们来大致描述下进出队列的情况。 进队列 1 进队列现在队列是 12 进队列现在队列是 1...

  • 利用链表实现队列

    队列成员变量: 队列长度 队列头节点 队列尾节点队列方法: 队列包含元素个数 队列是否为空 进队操作 出队操作 d...

  • Git 常用操作命令(持续更新)

    当前更新到stash队列 查看stash队列 清空队列 删除某个队列

网友评论

      本文标题:队列

      本文链接:https://www.haomeiwen.com/subject/ufougftx.html