£¨1£©³õʼ»¯¶ÓÁУºInit_Queue(q) £¬³õʼÌõ¼þ£º¶Óq ²»´æÔÚ¡£²Ù×÷½á¹û£º¹¹ÔìÁËÒ»¸ö¿Õ¶Ó£»
£¨2£©Èë¶Ó²Ù×÷£º In_Queue(q,x),³õʼÌõ¼þ£º ¶Óq ´æÔÚ¡£²Ù×÷½á¹û£º ¶ÔÒÑ´æÔڵĶÓÁÐq£¬²åÈëÒ»¸öÔªËØx µ½¶Ó⣬¶Ó·¢Éú±ä»¯£»
£¨3£©³ö¶Ó²Ù×÷£º Out_Queue(q,x)£¬³õʼÌõ¼þ: ¶Óq ´æÔÚÇÒ·Ç¿Õ£¬²Ù×÷½á¹û£º ɾ³ý¶ÓÊ×ÔªËØ£¬²¢·µ»ØÆäÖµ£¬¶Ó·¢Éú±ä»¯£»
£¨4£©¶Á¶ÓÍ·ÔªËØ£ºFront_Queue(q,x)£¬³õʼÌõ¼þ: ¶Óq ´æÔÚÇÒ·Ç¿Õ£¬²Ù×÷½á¹û£º ¶Á¶ÓÍ·ÔªËØ£¬²¢·µ»ØÆäÖµ£¬¶Ó²»±ä£»
£¨5£©ÅжӿղÙ×÷£ºEmpty_Queue(q)£¬³õʼÌõ¼þ£º ¶Óq ´æÔÚ£¬²Ù×÷½á¹û£º Èôq Ϊ¿Õ¶ÓÔò·µ»ØΪ1£¬·ñÔò·µ»ØΪ0
³ÌÐòʾÀý´úÂ룺
#include<stdio.h>
#include<stdlib.h>
#define true 1
#define false 0
struct STU
{
int data;
struct STU *next;
};
struct Queue
{
struct STU *front;/*¶ÓÊ×Ö¸Õë*/
struct STU *rear;/*¶ÓβָÕë*/
};
void init_queue(struct Queue *hq);//³õʼ»¯Ò»¸ö¶ÓÁÐ
void Insert_queue(struct Queue *hq,int val);//Èë¶Ó£¬²åÈëÒ»¸öÔªËص½¶ÓÁÐÖÐ
int empty_queue(struct Queue *hq);//Åж϶ÓÁÐÊÇ·ñΪ¿Õ
void Pri_queue(struct Queue *hq);//±éÀú¶ÓÁÐÔªËØ
int Delete_queue(struct Queue *hq);//ɾ³ý¶ÓÁÐÔªËØ
int read_queue_first(struct Queue *hq);//¶Á¶ÓÊ×ÔªËØ
void clear_queue(struct Queue *hq);//Çå¿Õ¶ÓÁÐ
int main()
{
int a[6]={10,20,30,40,6,78};
int i;
struct Queue hg;
init_queue(&hg);
for(i=0;i<6;i++)
Insert_queue(&hg,a[i]);
Pri_queue(&hg);
printf("Ê׶ÓÔªËØ %d ±»É¾³ý!\n",Delete_queue(&hg));
printf("Ê׶ÓÔªËØ %d ±»É¾³ý!\n",Delete_queue(&hg));
Pri_queue(&hg);
printf("¶ÓÊ×ÔªËØΪ %d\n",read_queue_first(&hg));
clear_queue(&hg);
Pri_queue(&hg);
return 0;
}
/*³õʼ»¯Ò»¸ö¶ÓÁÐ*/
void init_queue(struct Queue *hq)
{
/*½«¶ÓÊ×Ö¸ÕëºÍ¶ÓβָÕëÖÿÕ*/
hq->front=hq->rear=NULL;
}
/*Ïò¶ÓÄÚ²åÈëÒ»¸öÔªËØ*/
void Insert_queue(struct Queue *hq,int val)
{
/*µÃµ½Ò»¸öÓÉpÖ¸ÕëÖ¸ÏòµÄнڵã*/
struct STU *p=(struct STU *)malloc(sizeof(struct STU));
if(p==NULL)
{
printf("ÄÚ´æ·ÖÅäʧ°Ü\n");
return ;
}
p->data=val; /*½«Êý¾Ý¸³¸øнڵãµÄÊý¾ÝÓò*/
p->next=NULL; /*½«Ð½ڵãµÄÖ¸ÕëÓòÖÿÕ*/
/*Èç¹û¶ÓÁÐΪ¿Õ£¬Ð½ڵã¼ÈÊǶÓÊ×½ÚµãÓÖÊǶÓβ½Úµã*/
if(hq->rear==NULL)
{
hq->front=p;
hq->rear=p;
}
else
{
/*Èç¹û¶ÓÁзǿգ¬ÒÀ´ÎÐ޸ĶÓβָÕëÓòºÍ¶ÓβָÕ룬ʹ¶ÓβָÕëÖ¸ÏòеĶÓβ½Úµã*/
hq->rear->next=p;
hq->rear=p;
}
}
/*Åж϶ÔÁÐÊÇ·ñΪ¿Õ£¬ÈôÊÇÔò·µ»ØÕæ*/
int empty_queue(struct Queue *hq)
{
/*Åж϶ÓÊ×»ò¶ÓβָÕëÊÇ·ñΪ·Ç¿Õ¼´¿É*/
if(hq->front==NULL)
return true;
else
return false;
}
/*±éÀú¶ÓÄÚµÄÔªËØ*/
void Pri_queue(struct Queue *hq)
{
/*¶¨ÒåÒ»¸öеÄÖ¸ÕëʹÆäÖ¸Ïò¶ÓÊ×*/
struct STU *p=hq->front;
if(empty_queue(hq))
{
printf("¸Ã¶ÓÁÐÊÇ¿Õ¶ÓÁÐ\n");
return;
}
else
{
printf("¶ÓÄÚÔªËØΪ£º ");
/*½øÐÐÖ¸ÕëÆ«ÒÆ£¬µ±Ã»ÓÐÆ«ÒƵ½¶Óβʱ£¬´òÓ¡Êý¾Ý*/
while(p->next!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("%d ",p->data);
printf("\n");
}
}
/*ɾ³ý¶ÓÄÚµÄÊ×ÔªËØ£¬²¢ÇÒ·µ»Øɾ³ýµÄÔªËØ*/
int Delete_queue(struct Queue *hq)
{
/*¶¨ÒåÒ»¸öÖ¸Õ룬ʹÆäÖ¸Ïò¶ÓÊ×*/
struct STU *p=hq->front;
int val;
if(empty_queue(hq))
{
printf("¸Ã¶ÓÁÐÊÇ¿Õ¶ÓÁÐ\n");
return -1;
}
/*½«¶ÓÊ×ÔªËر£´æÏÂÀ´£¬ÓÃÓÚ·µ»Ø*/
val=p->data;
/*ʹÆä¶ÓÊ×Ö¸ÕëÖ¸ÏòÏÂÒ»¸ö½Úµã*/
hq->front=p->next;
/*Èôɾ³ýºó£¬¶ÓÊ×Ö¸ÕëΪ¿Õ£¬ÔòÐèʹ¶ÓβָÕëҲΪ¿Õ*/
if(hq->front==NULL)
{
hq->rear=NULL;
}
/*»ØÊÕÔ¶ÓÊ×½Úµã*/
free(p);
return val; /*·µ»Ø±»É¾³ýµÄ¶ÓÊ×½ÚµãµÄÔªËØÖµ*/
}
/*¶ÁÈ¡¶ÓÊ×ÔªËØ*/
int read_queue_first(struct Queue *hq)
{
if(empty_queue(hq))
{
printf("¸Ã¶ÓÁÐÊÇ¿Õ¶ÓÁÐ\n");
return -1;
}
return hq->front->data;
}
/*Çå¿Õ¶ÓÁÐÖеÄÔªËØ*/
void clear_queue(struct Queue *hq)
{
struct STU *p=hq->front;
if(empty_queue(hq))
{
printf("¸Ã¶ÓÁÐÊÇ¿Õ¶ÓÁÐ\n");
return ;
}
else
{
/*ÒÀ´Îɾ³ý¶ÓÊ×ÖеÄÿһ¸ö½Úµã£¬×îºó£¬Ê¹Æä¶ÓÊ×Ö¸ÕëΪ¿Õ*/
while(p!=NULL)
{
hq->front=p->next;
free(p);
p=hq->front;
}
/*Ñ»·½áÊøÖ®ºó£¬¶ÓÊ×Ö¸ÕëÒѾΪ¿Õ*/
hq->rear=NULL; /*½«¶ÓβָÕëÖÿÕ*/
}
}