赞
踩
- typedef int QDataType;
- #define MAXSIZE 50 //定义元素的最大个数
- /*循环队列的顺序存储结构*/
- typedef struct {
- QDataType *data;
- int front; //头指针
- int rear; //尾指针
- }Queue;
- void QueueInit(Queue* q)
- {
- q->data = (QDataType*)malloc(sizeof(QDataType )* MAXSIZE);
- if (q->data == NULL)
- {
- perror("malloc fail:");
- return;
- }
- q->front = 0;
- q->rear = 0;
- }
- bool QueueEmpty(Queue*q)
- {
- assert(q);
- return q->front == q->rear;
- }
- bool QueueFull(Queue*q)
- {
- assert(q);
- return q->front == (q->rear+1)%MAXSIZE;
- }
- void QueuePush(Queue* q, QDataType x)
- {
- assert(q);
- if(QueueFull)
- {
- printf("队列已满\n");
- return ;
- }
- q->data[q->rear] = x;
- q->rear = (q->rear + 1) % MAXSIZE; //rear指针向后移一位置,若到最后则转到数组头部
- }
- void QueuePop(Queue* q)
- {
- assert(q);
- assert(!QueueEmpty(q));
- q->front = (q->front + 1) % MAXSIZE; //front指针向后移一位置,若到最后则转到数组头部
- }
- void QueuePrint(Queue* q)
- {
- assert(q);
- int cur = q->front;
- printf("队头->");
- while (cur != q->rear)
- {
- printf("%d->", q->data[cur]);
- cur = (cur + 1) % MAXSIZE;
- }
- printf("队尾\n");
- }
- void QueueDestroy(Deque* q)//销毁队列
- {
- assert(q);
- free(q->data);
- q->data = NULL;
- q->front = q->rear = 0;
- }
- typedef int QDataType;
- typedef struct QueueNode
- {
- QDataType data;
- struct QueueNode* next;
- }QNode;
- typedef struct Queue
- {
- QNode* front;
- QNode* rear;
- size_t size;
- }Queue;
- void QueueInit(Queue* q)
- {
- q->front = NULL;
- q->rear = NULL;
- q->size = 0;
- }
- bool QueueEmpty(Queue* q)
- {
- assert(q);
- return (q->front == NULL) && (q->rear == NULL);
- }
- void QueuePush(Queue* q, QDataType x)
- {
- assert(q);
- QNode* newnode = (QNode*)malloc(sizeof(QNode));
- newnode->data = x;
- newnode->next = NULL;
- if (newnode == NULL)
- {
- perror("malloc fail");
- return;
- }
- if (q->front == NULL)
- {
- q->front = q->rear = newnode;
- }
- else
- {
- q->rear->next = newnode;
- q->rear = newnode;
- }
- q->size++;
- }
- void QueuePop(Queue* q)
- {
- assert(q);
- assert(!QueueEmpty(q));
- //1.只有一个结点
- if (q->front == q->rear)
- {
- free(q->front);
- q->front = q->rear = NULL;
- }
- //2.有多个结点
- else
- {
- QNode* del = q->front;
- q->front = q->front->next;
- free(del);
- del = NULL;
- }
- q->size--;
- }
- void QueuePrint(Queue* q)
- {
- assert(q);
- QNode* cur = q->front;
- QNode* tail = q->rear;
- printf("队头->");
- while (cur != tail->next)
- {
- printf("%d->",cur->data);
- cur = cur->next;
- }
- printf("队尾\n");
- }
- void QueueDestroy(Queue* q)
- {
- assert(q);
- QNode* cur = q->front;
- while (cur)
- {
- QNode* del = cur;
- cur = cur->next;
- free(del);
- del = NULL;
- }
- q->front = q->rear = NULL;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。