队列是一种特殊的表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的表。进行插入操作的端称为队尾,进行删除操作的端称为队头
队列的实现同栈一样,都可以通过链表和线性表实现。下面是链表实现的队列
头文件queue.h
struct Node;
typedef int Element;
typedef struct Node *ptrToNode;
struct Queue;
typedef struct Queue *ptrToQueue;
//创建队列
ptrToQueue createQueue();
//入队
void enQueue(ptrToQueue Q,Element e);
//出队
Element deQueue(ptrToQueue Q);
//对是否为空
int isEmpty(ptrToQueue Q);
//销毁队列
void destoryQueue(ptrToQueue &Q);
//获得队列大小
int getSize(ptrToQueue Q);
实现
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
struct Node{
Element datd;
ptrToNode next;
};
struct Queue{
ptrToNode front,rear;
int size; //队列大小
};
//创建队列
ptrToQueue createQueue(){
ptrToQueue ptr = (ptrToQueue)malloc(sizeof(struct Queue));
if(ptr==NULL){