#include
#include
typedef int elemType;
struct sNode{
elemType data;
struct sNode *next;
};
struct queueLK{
struct sNode *front;
struct sNode *rear;
};
void initQueue(struct queueLK *hq)
{
hq->front = hq->rear = NULL;
return;
}
void enQueue(struct queueLK *hq, elemType x)
{
struct sNode *newP;
newP = malloc(sizeof(struct sNode));
if(newP == NULL){
printf("内存空间分配失败! ");
exit(1);
}
newP->data = x;
newP->next = NULL;
if(hq->rear == NULL){
hq->front = hq->rear = newP;
}else{
hq->rear =
hq->rear->next = newP;
}
return;
}
elemType outQueue(struct queueLK *hq)
{
struct sNode *p;
elemType temp;
if(hq->front == NULL){
printf("队列为空,无法删除! ");
exit(1);
}
temp = hq->front->data;
p = hq->front;
hq->front = p->next;
if(hq->front == NULL){
hq->rear = NULL;
}
free(p);
return temp;
}
elemType peekQueue(struct queueLK *hq)
{
if(hq->front == NULL){
printf("队列为空,无法删除! ");
exit(1);
}
return hq->front->data;
}
int emptyQueue(struct queueLK *hq)
{
if(hq->front == NULL){
return 1;
}else{
return 0;
}
}
void clearQueue(struct queueLK *hq)
{
struct sNode *p = hq->front;
while(p != NULL){
hq->front =
hq->front->next;
free(p);
p = hq->front;
}
hq->rear = NULL;
return;
}
int main(int argc, char* argv[])
{
struct queueLK q;
int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
int i;
initQueue(&q);
for(i = 0; i < 8; i++){
enQueue(&q, a[i]);
}
printf("%d ", outQueue(&q)); printf("%d ",
outQueue(&q));
enQueue(&q, 68);
printf("%d ", peekQueue(&q)); printf("%d ",
outQueue(&q));
while(!emptyQueue(&q)){
printf("%d ", outQueue(&q));
}
printf(" ");
clearQueue(&q);
system("pause");
}
typedef int elemType;
struct queue{
elemType *queue;
int front, rear, len;
int maxSize;
};
void againMalloc(struct queue *q)
{
elemType *p;
p = realloc(q->queue, 2 *
q->maxSize * sizeof(elemType));
if(!p){
printf("空间分配失败! ");
exit(1);
}
q->queue = p;
if(q->rear != q->maxSize -1){
int i;
for(i = 0; i <= q->rear;
i++){
q->queue[i+q->maxSize] =
q->queue[i];
}
q->rear += q->maxSize;
}
q->maxSize = 2 * q->maxSize;
return;
}
void initQueue(struct queue *q, int ms)
{
if(ms <= 0){
printf("ms值非法! ");
exit(1);
}
q->maxSize = ms;
q->queue = malloc(ms * sizeof(elemType));
if(!q->queue){
printf("内存空间分配失败! ");
exit(1);
}
q->front = q->rear = 0;
return;
}
void enQueue(struct queue *q, elemType x)
{
if((q->rear + 1) % q->maxSize ==
q->front){
againMalloc(q);
}
q->rear = (q->rear + 1) %
q->maxSize;
q->queue[q->rear] = x;
return;
}
elemType outQueue(struct queue *q)
{
if(q->front == q->rear){
printf("队列为空,无法删除! ");
exit(1);
}
q->front = (q->front +1) %
q->maxSize;
return q->queue[q->front];
}
elemType peekQueue(struct queue *q)
{
if(q->front == q->rear){
printf("队列为空,无法删除! ");
exit(1);
}
return q->queue[(q->front +1) %
q->maxSize];
}
int emptyQueue(struct queue *q)
{
if(q->front == q->rear){
return 1;
}else{
return 0;
}
}
void clearQueue(struct queue *q)
{
if(q->queue != NULL){
free(q->queue);
q->queue = NULL;
q->front = q->rear = 0;
q->maxSize = 0;
}
return;
}
int main(int argc, char* argv[])
{
struct queue q;
int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
int i;
initQueue(&q, 5);
for(i = 0; i < 8; i++){
enQueue(&q, a[i]);
}
printf("%d ", outQueue(&q)); printf("%d ",
outQueue(&q));
enQueue(&q, 68);
printf("%d ", peekQueue(&q)); printf("%d ",
outQueue(&q));
while(!emptyQueue(&q)){
printf("%d ", outQueue(&q));
}
printf(" ");
clearQueue(&q);
system("pause");
return 0;
}