链式队列的入队出队操作

#include
#include
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;


typedef struct QueueNode
{
ElemType data;
struct QueueNode *next;


}Node;   //定义结点


typedef struct LinkQueue
{
Node *front;
Node *rear;
int length;
}LinkQueue;   //定义链队列


Status IintQueue(LinkQueue *q)
{
Node *p=(Node*)malloc(sizeof(Node));
if (p == NULL)
{
printf("申请空间失败!\n");
return ERROR;
}
q->front = p;
q->rear = p;
p->next = NULL;
q->length = 0;
return OK;
}


Status EnQueue(LinkQueue *q,ElemType e)
{
Node *p = (Node*)malloc(sizeof(Node));
if (p == NULL)
{
printf("申请空间失败!\n");
return ERROR;
}
p->data = e;
q->rear->next = p;
q->rear = p;
p->next = NULL;
q->length++;
return OK;
}


Status DeQueue(LinkQueue *q,ElemType *e)
{
if (q->front == q->rear)
{
printf("队列为空!\n");
return ERROR;
}
Node *L=q->front->next;
*e = L->data;
q->front->next = L->next;
free(L);
q->length--;
if (q->front==q->rear)
{
q->rear=q->front;
}
return OK;


}


void print(const LinkQueue *q)
{
if (q->front == q->rear)
{
printf("队列为空!\n");
}
int i;
Node *k=q->front->next;
for ( i = 1; i <= q->length ; ++i)
{


printf("%d ", k->data);
k=k->next;
}
printf("\n");
}


int main()
{
LinkQueue q;
ElemType e;
int i;
IintQueue(&q);
for ( i = 1; i <= 9; ++i)
{
EnQueue(&q,i);
}
print(&q);
print(&q);
DeQueue(&q,&e);
print(&q);
return OK;
}

你可能感兴趣的:(c语言)