#include <stdio.h>
#include <stdlib.h>
enum s
{
QUEUE_EMPTY,
QUEUE_NOEMPTY,
IN_OK,
OUT_OK,
OUT_NO
};
struct node
{
int data;
struct node *next;
};
struct Queue
{
struct node *front;
struct node *rear;
};
void init_queue(struct Queue *s)
{
s->front = NULL;
s->rear = NULL;
}
int empty_queue(struct Queue *s)
{
if(s->rear == NULL)
{
printf("queue is empty!\n");
return QUEUE_EMPTY;
}
return QUEUE_NOEMPTY;
}
int in_queue(struct Queue *s,int num)
{
struct node *p = (struct node*)malloc(sizeof(struct node));
if(p == NULL)
{
printf("malloc fail!\n");
return -1;
}
p->data = num;
p->next = NULL;
if(s->rear == NULL)
{
s->front = p;
s->rear = p;
}
else
{
s->rear->next = p;
s->rear = p;
return IN_OK;
}
}
int out_queue(struct Queue *s,int *num)
{
if(empty_queue(s) == QUEUE_EMPTY)
{
return OUT_NO;
}
struct node *p = s->front;
*num = p->data;
// printf("%d",*num);
s->front = p->next;
if(s->front == NULL)
{
s->rear = NULL;
}
free(p);
p = NULL;
return OUT_OK;
}
int main()
{
struct Queue *s = (struct Queue*)malloc(sizeof(struct Queue));
int i = 0;
int num = 0;
init_queue(s);
for(i = 0;i < 9;i++)
{
in_queue(s,i+1);
printf("第[%d]: %din queue\n",i+1,s->rear->data);
}
for(i = 0;i < 9;i++)
{
if(empty_queue(s) != QUEUE_EMPTY)
{
out_queue(s,&num);
printf("%d\n",num);
}
else
{
printf("out error!\n");
}
}
return 0;
}
队列的链式存储相关操作
最新推荐文章于 2023-08-12 02:05:56 发布