实现简单的队列操作函数

来源:互联网 发布:捷克语翻译软件 编辑:程序博客网 时间:2024/06/05 20:09
#include <stdio.h>
#include <malloc.h>

struct node
{
char element;
struct node *front;
struct node *rear;
struct node *next;
};
typedef struct node *pnode;
typedef pnode QUEUE;

void makeNULL(QUEUE Q)//初始化
{
Q->front=(QUEUE)malloc(sizeof( struct node));
Q->rear=(QUEUE)malloc(sizeof( struct node));
Q->rear=Q->front;
Q->front->next=NULL;
}

int isEmpty(QUEUE Q)//判断是否为空
{
if(Q->front==Q->rear)
return 1;
else
return 0;
}

char deleteQUEUE(QUEUE Q)//删除队列元素并返回
{
QUEUE tmp;

if(isEmpty(Q)==1)
{
return 0;
}
else 
{
tmp=Q->front->next;
Q->front->next=tmp->next;

if(Q->front->next==NULL)
{
Q->rear=Q->front;
   }
return tmp->element;

free(tmp);
}
}

void EnQUEUE(QUEUE Q,char x)//将元素插入队列
{
QUEUE q1;
q1=(QUEUE)malloc(sizeof(struct node));

q1->element=x;
q1->next=NULL;
if(Q->front->next==NULL)
{
Q->front->next=q1;
}
Q->rear->next=q1;
Q->rear=q1;
}

char FRONT(QUEUE Q)//返回队列的头元素
{
QUEUE q1;


q1=Q->front->next;
return q1->element;
}

void OutputQUEUE(QUEUE Q)//输出队列

QUEUE q1;
 
q1=Q->front->next;
 
while(q1!=NULL)

printf("%c ",q1->element);
q1=q1->next;
}
}