AfxStd.h
#pragma once #ifndef AfxStd_H #define AfxStd_H #include<stdio.h> #include<stdlib.h> #include<string.h> #endif // !AfxStd
LinkQueue.h
#pragma once #include"AfxStd.h" #ifndef LINKQUEUE_H #define LINKQUEUE_H typedef int QElemType; typedef struct QNode { QElemType data; QNode *next; }QNode, *PQNode; typedef struct { PQNode front; PQNode rear; int cursize; }ListQueue; QNode * Buynode(); void Freenode(QNode *p); void InitListQueue(ListQueue &qu); void DestroyListQueue(ListQueue &qu); int GetSize(ListQueue &qu); bool Empty(ListQueue &qu); QElemType GetFront(ListQueue &qu); QElemType GetBack(ListQueue &qu); bool Push(ListQueue &qu, QElemType x); void Pop(ListQueue &qu); void Clear(ListQueue &qu); void Print(ListQueue &qu); #endif //LINKQUEUE_H
LinkQueue.cpp
#include"LinkQueue.h" QNode * Buynode() { QNode *s = (QNode*)malloc(sizeof(QNode)); if (NULL == s) exit(1); memset(s, 0, sizeof(QNode)); return s; } void Freenode(QNode *p) { free(p); } void InitListQueue(ListQueue &qu) { qu.cursize = 0; qu.front = qu.rear = Buynode(); } void DestroyListQueue(ListQueue &qu) { Clear(qu); Freenode(qu.front); qu.front = qu.rear = NULL; } int GetSize(ListQueue &qu) { return qu.cursize; } bool Empty(ListQueue &qu) { return GetSize(qu) == 0; } QElemType GetFront(ListQueue &qu) { return qu.front->next->data; } QElemType GetBack(ListQueue &qu) { return qu.rear->data; } bool Push(ListQueue &qu, QElemType x) { PQNode s = Buynode(); s->data = x; s->next = NULL; qu.rear->next = s; qu.rear = s; qu.cursize += 1; return true; } void Pop(ListQueue &qu) { if (Empty(qu)) { return; } QNode *q = qu.front->next; qu.front->next = q->next; Freenode(q); qu.cursize -= 1; if (qu.cursize == 0) { qu.rear = qu.front;// head; } } void Clear(ListQueue &qu) { QNode *q = NULL; while (qu.front->next != NULL) { q = qu.front->next; qu.front->next = q->next; Freenode(q); } qu.rear = qu.front; qu.cursize = 0; } void Print(ListQueue &qu) { QNode *s = qu.front; while (s!=qu.rear) { printf("%d",s->data); s = s->next; } }