实现队列

//实现队列

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

struct Node
{
int data;
Node
* pNext;
};

struct Queue
{
Node
* head;
Node
* rear;
};

Queue
* Insert(Queue* que, int x)
{
Node
* istNode = (Node*)malloc(sizeof(Node));
istNode
->data = x;
istNode
->pNext =NULL;
if(que->head==NULL)
{
que
->head=istNode;
que
->rear = istNode;
printf(
"Insert %d!\n",istNode->data);
}
else
{
que
->rear->pNext = istNode;
que
->rear = istNode;
printf(
"Insert %d!\n",istNode->data);
}
return que;
}

Queue
* Delete(Queue* que)
{
if(que->head ==NULL)
{
printf(
"Error!");
}
Node
* del=NULL;
if(que->head == que->rear)
{
del
=que->head;
que
->head=NULL;
que
->rear =NULL;
printf(
"Delete--%d!\n",del->data);
free(del);
}
else
{
del
=que->head;
que
->head = del->pNext;
printf(
"Delete--%d!\n",del->data);
free(del);
}
return que;
}
void PrintQueue(Queue* que)
{
if(que->head ==NULL)
{
printf(
"NULL!\n");
}
else
{
Node
* t_head=que->head;
Node
* t_rear=que->rear;
while(t_head != t_rear)
{
printf(
"%d ",t_head->data);
t_head
=t_head->pNext;
}
printf(
"%d ",t_head->data);
printf(
"\n");
}
}

int main()
{
Queue
* que=(Queue*)malloc(sizeof(Queue));
que
->head =NULL;
que
->rear =NULL;
que
=Insert(que, 1);
que
=Insert(que, 3);
que
=Insert(que, 4);
que
=Insert(que, 5);
que
=Insert(que, 3);
que
=Insert(que, 1);
que
=Insert(que, 2);
PrintQueue( que);
que
=Delete(que);
que
=Delete(que);
que
=Delete(que);
que
=Delete(que);
que
=Delete(que);
PrintQueue( que);
system(
"pause");
}

posted on 2011-09-20 23:48  Lovell Liu  阅读(154)  评论(0编辑  收藏  举报

导航