#ifndef _2_H
#define _2_H
#ifdef __cplusplus
extern "C" {
#endif
#define TRUE 1
#define FALSE 0
//¥¯ø’Õ∑
typedef struct qnode
{
int score;
struct qnode *next;
}QNODE;
typedef struct queue
{
QNODE *front;
QNODE *rear;
}QUEUE;
QUEUE *initQueue(QUEUE *q);
int EMPTY(QUEUE *q);
QUEUE *ENQUEUE(QUEUE *q,int x);
int DEQUEUE(QUEUE *q);
#ifdef __cplusplus
}
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include "2.h"
QUEUE *initQueue(QUEUE *q)
{
q->front = q->rear = (QNODE *)malloc(sizeof(QNODE));
q->front->next =NULL;
return q;
}
int EMPTY(QUEUE *q)
{
return (q->front == q->rear);
}
QUEUE *ENQUEUE(QUEUE *q,int x)
{
q->rear->next =(QNODE *)malloc(sizeof(QNODE));
if(NULL == q->rear->next)
{
printf("ram error\n");
}
q->rear = q->rear->next;
q->rear->score = x;
q->rear->next =NULL;
return q;
}
int DEQUEUE(QUEUE *q)
{
int x;
QNODE *p;
if(EMPTY(q))
{
exit(1);
}
p = q->front->next;
q->front->next = p->next;
x = p->score;
free(p);
if(NULL == q->front->next) //µÙ¡À’‚æ‰æÕŒfi∑®≈–∂œø’∂”
{
q->front = q->rear;
}
return (x);
}
int main()
{
int a[]={50,80,70,90},b[4]={0};
int i,score,flag;
QUEUE *p =NULL,*tmp;
p = (QUEUE *)malloc(sizeof(QUEUE));
tmp = p;
p = initQueue(p);
flag = EMPTY(p);
printf("-----------------------------\n");
printf("this queue's empty flag is %d\n",flag);
printf("-----------------------------\n");
printf("ENQUEUE:\n");
for(i =0;i < 4;i++)
{
printf("%d\n",a[i]);
p = ENQUEUE(p, a[i]);
}
flag = EMPTY(p);
printf("-----------------------------\n");
printf("this queue's empty flag is %d\n",flag);
printf("-----------------------------\n");
printf("DEQUEUE:\n");
for(i =0;i < 4;i++)
{
b[i] = DEQUEUE(p);
printf("%d\n",b[i]);
}
flag = EMPTY(p);
printf("-----------------------------\n");
printf("this queue's empty flag is %d\n",flag);
printf("-----------------------------\n");
p =NULL;
free(tmp);
tmp =NULL;
return0;
}