#include<stdio.h>
typedef struct dataNode
{
int data;
struct dataNode *next;
}qNode;
typedef struct linkQueue
{
qNode *head;
qNode *tail;
}linkQueue;
//创建一个新队列
linkQueue* createLinkQueue()
{
linkQueue *q = (linkQueue*)malloc(sizeof(linkQueue));
q->head = NULL;
q->tail = NULL;
return q;
}
//向队列插入一个结点
linkQueue* insertNode(linkQueue *q,int i)
{
qNode *node;
node = (qNode*)malloc(sizeof(qNode));
node ->data = i;
node ->next = NULL;
if(q->tail == NULL)
{
q->head = node;
q->tail = node;
}
else
{
q->tail->next = node;
q->tail = node;
}
return q;
}
//从队列删除一个结点
linkQueue* deleteNode(linkQueue *q)
{
qNode *node;
if(q->head == NULL)
{
printf("the queue is empty \n");
return q;
}
node = q->head;
if(q->head == q->tail)
{
q->head = NULL;
q->tail = NULL;
free(node);
}else
{
q->head = q->head->next;
free(node);
}
return q;
}
//顺序打印队列
void printLinkQueue(linkQueue *q)
{
qNode *node;
node = q->head;
while(node)
{
printf("%d ",node->data);
node=node->next;
}
printf("\n");
}
void main()
{
int i ;
linkQueue *q;
q = createLinkQueue();
for(i=0;i<=10;i++)
q = insertNode(q,i);
for(i=0;i<=4;i++)
q = deleteNode(q);
printLinkQueue(q);
}
[C语言]队列基本操作
最新推荐文章于 2023-04-22 21:56:45 发布