数据结构之顺序队列和链式队列常用的一些操作
来源:互联网 发布:去马赛克的软件 编辑:程序博客网 时间:2024/06/16 10:46
顺序队列是队列的顺序存储结构,顺序队列实际上是运算受限的顺序表。和顺序表一样,顺序队列用一个向量空间来存放当前队列中的元素。由于队列的队头和队尾的位置是变化的,设置两个指针front和rear分别指示队头元素和队尾元素在向量空间中的位置,它们的初值在队列初始化时均应设置为0。
头文件 SqQueue.h
#ifndef _SQUEUE_H__#define _SQUEUE_H__#include "error.h"#define TRUE 1#define FALSE 0#define SIZE 10typedef int QueueData;typedef struct _queue{ QueueData data[SIZE]; int front; // 指向队头的下标 int rear; // 指向队尾的下标}Queue;// 置空队int InitQueue (Queue* q);// 判队空否int QueueEmpty (Queue* q);// 判队满否int QueueFull (Queue* Q);// 进队int EnQueue (Queue* q, QueueData x);// 出队int DeQueue (Queue* s, QueueData *x);// 取队头int GetFront (Queue* s, QueueData *x);#endif // _SQUEUE_H__
源文件 :SqQueue.c
#include "SqQueue.h"// 置空队int InitQueue (Queue* q){ if (NULL == q) { errno = ERROR; return FALSE; } // 置空队 q->front = 0; q->rear = 0; return TRUE;}// 判队空否int QueueEmpty (Queue* q){ if (NULL == q) { errno = ERROR; return FALSE; } return (q->front == q->rear);}// 判队满否int QueueFull (Queue* q){ if (NULL == q) { errno = ERROR; return FALSE; } return (q->front == (q->rear+1) % SIZE);}// 进队int EnQueue (Queue* q, QueueData x){ if (NULL == q) { errno = ERROR; return FALSE; } if (QueueFull(q)) { errno = FULL_QUEUE; return FALSE; } q->data[(++q->rear) % SIZE] = x; return TRUE;}// 出队int DeQueue (Queue* q, QueueData *x){ if (NULL == q) { errno = ERROR; return FALSE; } if (QueueEmpty(q)) { errno = EMPTY_QUEUE; return FALSE; } *x = q->data[(++q->front) % SIZE]; return TRUE;}// 取队头int GetFrontf (Queue* q, QueueData *x){ if (NULL == q) { errno = ERROR; return FALSE; } if (QueueEmpty(q)) { errno = EMPTY_QUEUE; return FALSE; } *x = q->data[(q->front + 1) % SIZE]; return TRUE;}
链式队列 链式队列没有空间溢出的问题
头文件 LinkQueue.h
#ifndef __LINKQUEUE_H__#define __LINKQUEUE_H__#include "error.h"#define TRUE 1#define FALSE 0typedef int QueueData;typedef struct _node{ QueueData data; struct _node* next;}Node;typedef struct _queue{ Node* front; Node* rear;}Queue;// 创建队列Queue* Create_Queue ();// 置空队列int QueueEmpty (Queue* q);// 进队int EnQueue (Queue* q, QueueData x);// 出队int DeQueue (Queue* q, QueueData *x);// 取队头int GetFront (Queue* q, QueueData *x);// 销毁队列int Destroy_Queue (Queue *q);#endif
源文件 LinkQueue.c
#include "LinkQueue.h"#include <stdlib.h>// 创建队列Queue* Create_Queue (){ Queue* q = (Queue*) malloc(sizeof(Queue)/sizeof(char)); if (NULL == q) { errno = MALLOC_ERROR; return NULL; } // 置空队 q->front = NULL; q->rear = NULL; return q;}// 置空队int QueueEmpty (Queue* q){ if (NULL == q) { errno = ERROR; return FALSE; } return q->front == NULL;}// 进队int EnQueue (Queue* q, QueueData x){ if (NULL == q) { errno = ERROR; return FALSE; } Node* node = (Node*) malloc(sizeof(Node)/sizeof(char)); if (NULL == node) { errno = MALLOC_ERROR; return FALSE; } node->data = x; node->next = NULL; if (NULL == q->front) { q->front = node; q->rear = node; } else { q->rear->next = node; q->rear = node; } return TRUE;}// 出队int DeQueue (Queue* q, QueueData *x){ if (NULL == q) { errno = ERROR; return FALSE; } if (QueueEmpty(q)) { errno = EMPTY_QUEUE; return FALSE; } Node* p = q->front; *x = p->data; q->front = p->next; free(p); if (NULL == q->front) { q->rear = NULL; } return TRUE;}// 取队头int GetFront (Queue* q, QueueData *x){ if (NULL == q) { errno = ERROR; return FALSE; } if (QueueEmpty(q)) { errno = EMPTY_QUEUE; return FALSE; } *x = q->front->data; return TRUE;}// 销毁队列int Destroy_Queu (Queue* q){ if (NULL == q) { errno = ERROR; return FALSE; } int x; while (TRUE != QueueEmpty(q)) { DeQueue (q, &x); } free(q); return TRUE;}
阅读全文
1 0
- 数据结构之顺序队列和链式队列常用的一些操作
- 数据结构之队列(顺序队列和链式队列)
- 【数据结构】 链式队列的一些操作
- 数据结构之链式队列的所有操作
- PHP数据结构之七_队列的链式存储和队列的基本操作
- Python 数据结构 之 队列 的顺序、链式存储结构
- 数据结构—队列的顺序和链式存储
- 顺序队列和链式队列
- C++——数据结构之 顺序队列 链式队列?
- 数据结构之队列(链式队列)
- 链式队列一些操作
- 常用数据结构之链式存储队列
- 数据结构之链式队列
- 数据结构之链式队列
- 数据结构之链式队列
- 数据结构之链式队列
- 队列 -- 顺序队列和链式队列类的实现
- 【数据结构】链式队列的基本操作
- reset.css
- 278. First Bad Version
- leetcode#98. Validate Binary Search Tree
- Java集合HashMap的源码解刨
- 设计模式之策略模式(STRATEGY)
- 数据结构之顺序队列和链式队列常用的一些操作
- 腾讯js面试题2
- 结构化数据上的机器学习大杀器XGBoost
- 百练 8462:大盗阿福
- c++容易混淆的概念
- 英语发音困难点记录
- nginx安装lua和echo模块
- 微信小程序之自定义toast实例 —— 微信小程序实战系列(6)
- java作业调度框架Quartz