队列

创建: 2018/04/10

完成: 2018/04/13 (不带练习)

队列
 定义  数据结构, FIFO(先放先拿)
 特有名词

 enqueue [计] 入队,排队;

 dequeue v. 出列;

   
   
队列的用法
   重视放入的顺序
   
   
   
队列的ADT
为简单, 数据为int
 主要运算

 

 EnQueue(int data)  插入末尾
 init DeQueue()  获取并从队列删除首个元素

 

 辅助运算 

 

 int Front()  查看首元素(不删除)
 int QueueSize()  返回元素个数
 int IsEmptyQueue()  检查是否有要素

 

   
   
队列的异常
 删除空队列元素  Empty Queue Exception 
 插入满容队列  Full Queue Exception 
   
   
队列的应用
 直接应用

 ● 系统指示的分配(同优先度), 如印刷机

 ● 排队

 ● 多线程编程

 ● 异步数据传输

 ● 呼叫中心的等待

 ● 收银台

 间接应用

 ● 作为其他算法的辅助数据结构

 ● 其他数据结构的元素

   
   
队列的实现
 静态循环数组法

 

//------------------------------------------------
//                  序列的型声明
//------------------------------------------------
struct ArrayQueue {
    int front, rear;
    int capacity;
    int *array;
};

//------------------------------------------------
//                   函数声明
//------------------------------------------------
struct ArrayQueue *Queue(int size); // 初始化
int isEmptyQueue(struct ArrayQueue *Q); // 是否为空
int isFullQueue(struct ArrayQueue *Q); // 是否已满
int QueueSize(struct ArrayQueue *Q); // 获取大小
void enQueue(struct ArrayQueue *Q, int data); // push元素
int deQueue(struct ArrayQueue *Q); // unshift元素
void deleteQueue(struct ArrayQueue *Q); // 删除整个队列

//------------------------------------------------
//                   函数实现
//------------------------------------------------
struct ArrayQueue *Queue(int size) { // 初始化
    struct ArrayQueue *Q = (struct ArrayQueue *)malloc(sizeof(struct ArrayQueue));
    if (!Q) {
        return NULL;
    }
    Q->capacity = size;
    Q->front = Q->rear = -1;
    Q->array = (int *)malloc(Q->capacity*size);
    if (!Q->array) {
        return NULL;
    }
    return Q;
}

int isEmptyQueue(struct ArrayQueue *Q) { // 是否为空
    return Q->front == -1;
}

int isFullQueue(struct ArrayQueue *Q) { // 是否已满
    return (Q->rear+1) % Q->capacity == Q->front;
}

int QueueSize(struct ArrayQueue *Q) { // 获取大小
    return (Q->rear - Q->front + 1) % Q->capacity;
}

void enQueue(struct ArrayQueue *Q, int data) { // push元素
    if (isFullQueue(Q)) {
        printf("Queue Overflow");
    } else {
        Q->rear = (Q->rear + 1) % Q->capacity; // 0 ..< Q->capacity 之间
        Q->array[Q->rear] = data;
        if (Q->front == -1) {
            Q->front = Q->rear;
        }
    }
}

int deQueue(struct ArrayQueue *Q) { // unshift元素
    int data;
    if (isEmptyQueue(Q)) {
        printf("Queue is Empty\n");
        return 0;
    } else {
        data = Q->array[Q->front];
        if (Q->front == Q->rear) {
            Q->front = Q->rear = -1;
        } else {
            Q->front = (Q->front + 1) % Q->capacity;
        }
        return 0;
    }
}

void deleteQueue(struct ArrayQueue *Q) { // 删除整个队列
    if (Q) {
        if (Q->array) {
            free(Q->array);
        }
        free(Q);
    }
}

 

 动态循环数组法  

 在静态基础上加上满了倍增容量的函数

// 就只需要在 静态循环数组实现 基础上增加 容量满了的时候扩容的方法
//------------------------------------------------
//                   函数声明
//------------------------------------------------
void resizeQueue(struct ArrayQueue *Q);
//------------------------------------------------
//                   函数实现
//------------------------------------------------
void resizeQueue(struct ArrayQueue *Q) { // 满容量时候容量扩为两倍
    int size = Q->capacity;
    Q->capacity *= 2;
    Q->array = (int *)realloc(Q->array, Q->capacity);
    if (!Q->array) {
        printf("Memory Error");
        return;
    }
    if (Q->front > Q->rear) { // 处理原数据后边因为超过容量移到前面的情况
        for (int i = 0; i < Q->front; i++) {
            Q->array[i+size] = Q->array[i];
        }
        Q->rear += size;
    }
}

 

 

 链表法 

 

//------------------------------------------------
//                  序列的型声明
//------------------------------------------------
struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListQueue {
    struct ListNode *front;
    struct ListNode *rear;
};

//------------------------------------------------
//                   函数声明
//------------------------------------------------
struct ListQueue *Queue_list(void); // 创建链表队列
int isEmptyQueue_list(struct ListQueue * Q); // 是否为空
void enQueue_List(struct ListQueue *Q, int data); //插入 push
int deQueue_list(struct ListQueue *Q); // 取出第一个 unshift
void deleteQueue_list(struct ListQueue * Q); // 删除链表队列

//------------------------------------------------
//                   函数实现
//------------------------------------------------
struct ListQueue *Queue_list(void) { // 创建链表队列
    struct ListQueue *Q;
    Q = (struct ListQueue *)malloc(sizeof(struct ListQueue));
    if (!Q) {
        printf("Memory Error\n");
        return NULL;
    }
    Q->front = Q->rear = NULL;
    return Q;
}

int isEmptyQueue_list(struct ListQueue * Q) { // 是否为空
    return Q->front == NULL;
}

void enQueue_List(struct ListQueue *Q, int data) { //插入 push
    struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
    if (!newNode) {
        printf("Memory Error\n");
        return;
    }
    newNode->data = data;
    newNode->next = NULL;
    if (!Q->front || !Q->rear) {
        Q->front = Q->rear = newNode;
    } else {
        Q->rear->next = newNode;
        Q->rear = newNode;
        
    }
}

int deQueue_list(struct ListQueue *Q) { // 取出第一个 unshift
    int data;
    struct ListNode *temp;
    if (isEmptyQueue_list(Q)) {
        printf("ListQueue is Empty\n");
        return 0;
    } else {
        temp = Q->front;
        data = temp->data;
        Q->front = Q->front->next;
        free(temp);
    }
    return data;
}

void deleteQueue_list(struct ListQueue * Q) { // 删除链表队列
    struct ListNode *currentNode = Q->front, *nextNode;
    if (Q) {
        while (currentNode) {
            nextNode = currentNode->next;
            free(currentNode);
            currentNode = nextNode;
        }
        free(Q);
    }
}

 

 

   
队列的问题
   
   
   
   
posted @ 2018-04-10 00:05  懒虫哥哥  阅读(78)  评论(0编辑  收藏  举报