﻿ 链式队列的实现 - 鸿网互联

# 链式队列的实现

``````#define _CRT_SECURE_NO_WARNINGS 1

#ifndef __QUEUE_H__
#define __QUEUE_H__

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int datatype;

typedef struct queue
{
datatype data;
struct queue *next;
}Quenode,*PtrQueue;

typedef struct queue_list
{
PtrQueue Tail;

enum
{
EXIT,
INIT,
DESTORY,
CLEAR,
LENGTH,
ENQUEUE,
DEQUEUE,
PRINTQUEUE
};

#endif //！__QUEUE_H__

``````

``````#define _CRT_SECURE_NO_WARNINGS 1

#include"queue.h"

{
printf("\$\$\$\$\$\$\$\$\$\$\$\$\$\$    QUEUE    \$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\n");
printf("\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$￥\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\n");
printf("\$\$\$  1.init            2.destory        \$\$\$\n");
printf("\$\$\$  3.clear           4.length         \$\$\$\n");
printf("\$\$\$  5.enqueue         6.dequeue        \$\$\$\n");
printf("\$\$\$  7.printqueue      0.EXIT           \$\$\$\n");
printf("\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\n");

}
{
assert(q);
{
printf("out of memory");
exit(EXIT_FAILURE);
}

}

{
assert(q);
{
}
}

{
assert(q);
while (p != NULL)
{
PtrQueue tmp = p;
p = p->next;
free(tmp);
tmp = NULL;
}
free(p);
}

{
assert(q);

return 1;
else
return 0;

}

{
assert(q);

int count = 0;
while (cur != NULL)
{
count++;
cur = cur->next;
}
return count;
}

{
assert(q);

if (QueueIsEmpty(q))
{
printf("队列为空队列！\n");
}

}

void EnQueue(pLinkQueue q , datatype x)
{
assert(q);
PtrQueue newnode = (PtrQueue)malloc(sizeof(Quenode));
if (newnode == NULL)
{
printf("out of memory");
exit(EXIT_FAILURE);
}
newnode->data = x;
newnode->next = NULL;
while (cur->next != NULL)
{
cur = cur->next;
}
cur ->next= newnode;
q->Tail = newnode;
}

{
assert(q);
PtrQueue del = NULL,cur=NULL;
{
printf("队列为空\n");
return;
}
del = cur;
free(del);
del = NULL;

}

{
assert(q);
return;

while (cur != NULL)
{
printf("%3d", cur->data);
cur = cur->next;
}
printf("\n");
}``````

## test.c

``````#define _CRT_SECURE_NO_WARNINGS 1

#include"queue.h"

void Test()
{
int input =1 ;
int length = 0;
datatype x = 0;
InitQueue(&que);
while (input)
{
printf("请选择：");
scanf("%d", &input);
switch (input)
{
case EXIT:
DestoryQueue(&que);
break;
case INIT:
InitQueue(&que);
break;
case DESTORY:
DestoryQueue(&que);
break;
case CLEAR:
ClearQueue(&que);
break;
case LENGTH:
length=QueueLength(&que);
printf("%d\n", length);
break;
case ENQUEUE:
printf("请输入你所要入队元素的值：");
scanf("%d", &x);
fflush(stdin);
EnQueue(&que,x);
break;
case DEQUEUE:
Dequeue(&que);
break;
case PRINTQUEUE:
PrintQueue(&que);
break;
default:
input = 1 ;
break;
}

}

}

int main()
{
Test();
system("pause");
return 0;
}``````

<