//实现队列

#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");
}

相关文章:

  • 2021-12-19
  • 2022-01-08
  • 2021-07-02
  • 2021-08-15
  • 2021-12-30
  • 2021-11-18
  • 2022-02-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2021-10-11
  • 2021-08-04
  • 2021-05-07
  • 2021-08-06
相关资源
相似解决方案