五一放假,也没有什么事情可以做的,本来想出去玩一玩或者打打球什么的,可是天公不作美,这两天天气一直是在下雨,弄的人的心情湿透了。
昨天学习了一天的栈和队列,昨天在下午些的时候完成了栈的编程工作。晚上本应该是用来写完队列的程序的,可是晚上到寝室熄灯之前只是差一点程序就可以全部完工了,可是总是有几个小bug调试不过去。没办法,今天上午又花了一个小时才完工。
艾,不过总算写完了,对于数据结构中的队列有了一个更加清新的认识。
好啦,不废话了,还是和以前一样把,把代码和大家一起分享把!
如果代码中有什么错误,希望您不吝赐教,您可以把您的意见发送到[email protected],我会尽快给您回复的。
C语言 队列的链式结构的实现与表示 数据结构 队列的表示与实现
/****************************************/
/*Description: Link Queue*/
/*Email:[email protected]*/
/*Author:yi_landry Harbin Normal University Computer Science*/
/*Date:2008-5-1*/
/*Copyright:HNU2008.cop*/
/*Environment:turbo c 2.01 English Version*/
/****************************************/
# include
# include
# define OVERFLOW 0
# define OK 1
/****************************************/
/*The node of link queue */
/****************************************/
struct LqNode
{
int item;
struct LqNode * next;
};
/****************************************/
/*The struct of link queue */
/****************************************/
struct LqQueue
{
struct LqNode * front;/*the head pointer of the link queue*/
struct LqNode * rear;/*the tail pointer of the link queue*/
int length;/*the total number of the nodes in the queue*/
}Q;
/****************************************/
/*Initial the queue*/
/****************************************/
InitQueue(struct LqQueue * Q)
{
Q->front=Q->rear=(struct LqNode *)malloc(sizeof(struct LqNode));
if (!Q->front && !Q->rear) exit(OVERFLOW);
Q->length = 0;
printf("Initial the link queue successfully!/n");
}
/****************************************/
/*insert a node into the queue at the tail*/
/****************************************/
QueueEn(struct LqQueue * Q,int elm)
{
struct LqNode * p,* q ;
int count = 0;
p = (struct LqNode *)malloc(sizeof(struct LqNode));
q = Q->front;
if (!p && !q) exit(OVERFLOW);
if (Q->length == 0)
{
p->item = elm;
p->next = NULL;
Q->front=Q->rear=p;
Q->length ++;
printf("The new node %d was inserted into the Queue successfully!/n",elm);
}
else
{
p->item = elm;
p->next = NULL;
while (1)
{
count++;
if (count == Q->length) break;
q = q->next;
}
q->next = Q->rear = p;
Q->length++;
printf("The new node %d was inserted into the Queue successfully!/n",elm);
}
}
/****************************************/
/*Delet the first node in the queue first out*/
/****************************************/
QueueDefirst(struct LqQueue * Q)
{
int DefirstElm=0;
if (Q->front != NULL)
{
DefirstElm = Q->front->item;
}
if (Q->length == 0)
{
printf("The queue now is empty,you can not delete the first node again!/n");
}
else if (Q->length == 1)
{
Q->front = Q->rear = NULL;
Q->length--;
printf("Delete the first node %d successfully!/n",DefirstElm);
}
else
{
Q->front = Q->front->next;
Q->length--;
printf("Delete the first node %d successfully!/n",DefirstElm);
}
}
/****************************************/
/*Judge the queue is empty or not*/
/****************************************/
QueueEmpty(struct LqQueue * Q)
{
if (Q->length==0)
{
printf("The queue now is empty!/n");
}
else
{
printf("The queue now is not empty!/n");
PrintQueue(Q);
}
}
/****************************************/
/*Get the length of the queue*/
/****************************************/
QueueLength(struct LqQueue * Q)
{
printf("The length of current queue is %d/n",Q->length);
}
/****************************************/
/*Destroy the queue and free the memory distributed at first*/
/****************************************/
DestroyQueue(struct LqQueue * Q)
{
free(Q->front);
free(Q->rear);
Q->length = 0;
printf("Destroy the queue successfully!/n");
}
/****************************************/
/*Clear all the nodes in the queue*/
/****************************************/
ClearQueue(struct LqQueue * Q)
{
free(Q->front);
free(Q->rear);
Q->length = 0;
printf("Clear the queue successfully!/n");
}
/****************************************/
/*Get the node at tbe location i */
/****************************************/
GetElement(struct LqQueue * Q,int i)
{
struct LqNode * p;
int count = 0;
p = Q->front;
if (i <= 0|| i>Q->length)
{
printf("The location you input is not valid!/n");
}
while (1)
{
count++;
if (count==i) break;
p=p->next;
}
return p->item;
}
/****************************************/
/*Find a number if or not in the queue*/
/****************************************/
FindElement(struct LqQueue * Q,int elm)
{
struct LqNode *p;
int count=0,totalFind=0;
p =Q->front;
while (1)
{
if (count == Q->length) break;
count++;
if(p->item == elm)
{
totalFind++;
printf("We find %d at the location of %d in the queue!/n",elm,count);
}
p = p->next;
}
if (totalFind == 0)
{
printf("We can not find %d in the queue!/n",elm);
}
else
{
printf("We totally find %d <%d> in the queue/n",totalFind,elm);
}
}
/****************************************/
/*Get prior node of some node in the queue*/
/****************************************/
PriorElement(struct LqQueue * Q,int elm)
{
struct LqNode *p;
int count=0,priorNode=0,totalFind=0;
p =Q->front;
while (1)
{
if (count == Q->length) break;
count++;
if(p->item == elm)
{
if (count == 1)
{
printf("We can not find the prior of %d in the queue!/n",elm);
}
else
{
totalFind++;
priorNode = GetElement(Q,count-1);
printf("We find prior of %d is %d at the location of %d in the queue!/n",elm,priorNode,count-1);
}
}
p=p->next;
}
if (totalFind == 0)
printf("We can not find the prior of %d in the queue!/n",elm);
}
/****************************************/
/*Get next node of some node in the queue*/
/****************************************/
NextElement(struct LqQueue * Q,int elm)
{
struct LqNode *p;
int count=0,nextNode=0,totalFind=0;
p =Q->front;
while (1)
{
if (count == Q->length) break;
count++;
if(p->item == elm)
{
if (count == Q->length)
{
printf("We can not find the next of %d in the queue!/n",elm);
}
else
{
totalFind++;
nextNode = GetElement(Q,count+1);
printf("We find next of %d is %d at the location of %d in the queue!/n",elm,nextNode,count+1);
}
}
p=p->next;
}
if (totalFind == 0)
printf("We can not find the next of %d in the queue!/n",elm);
}
/****************************************/
/*Print the queue */
/****************************************/
PrintQueue(struct LqQueue * Q)
{
int count;
count = Q->length;
if (Q->length==0)
{
printf("The queue now is empty!/n");
}
else if (Q->length == 1)
{
printf("The current queue:/n");
printf(" _______ /n");
printf("rear -->| %3d |/n",Q->front->item);
printf("front-->|_______|/n");
}
else
{
printf("The current queue:/n");
while(1)
{
if(count == 0) break;
if(count ==Q->length)
{
printf(" _______/n");
printf("rear -->| %3d |/n",GetElement(Q,count));
printf(" |_______|/n");
count--;
}
else if (count==1)
{
printf(" | %3d |/n",GetElement(Q,count));
printf("front-->|_______|/n");
count--;
}
else
{
printf(" | %3d |/n",GetElement(Q,count));
printf(" |_______|/n");
count--;
}
}
}
}
/****************************************/
/*Show a example to the user*/
/****************************************/
QueueExample()
{
InitQueue(&Q);
QueueEn(&Q,1);
QueueEn(&Q,2);
QueueEn(&Q,3);
QueueEn(&Q,4);
QueueEn(&Q,5);
QueueEn(&Q,6);
QueueEn(&Q,5);
QueueEn(&Q,6);
QueueEn(&Q,60);
QueueEn(&Q,50);
QueueEn(&Q,7);
QueueEn(&Q,8);
QueueEn(&Q,9);
FindElement(&Q,6);
FindElement(&Q,7);
FindElement(&Q,10);
PriorElement(&Q,6);
PriorElement(&Q,10);
NextElement(&Q,6);
NextElement(&Q,10);
GetElement(&Q,3);
PrintQueue(&Q);
QueueLength(&Q);
DestroyQueue(&Q);
return 0;
}
/****************************************/
/*Show the help information to the user*/
/****************************************/
void help()
{
printf(" /*****************************************************************//n");
printf(" /*****************************************************************//n");
printf(" /* The link queue Operation Version 1.0 *//n");
printf(" /* View the example information 1 *//n");
printf(" /* insert a node into the queue at the tail 2 *//n");
printf(" /* delete the head number from the queue 3 *//n");
printf(" /* Find a number if or not in the queue 4 *//n");
printf(" /* Get the prior of some number from the queue 5 *//n");
printf(" /* Get the next of some number from the queue 6 *//n");
printf(" /* Get the size of queue 7 *//n");
printf(" /* Show the help information 8 *//n");
printf(" /* View current queue information 9 *//n");
printf(" /* Exit link queue Q *//n");
printf(" /*****************************************************************//n");
}
main()
{
char userInput;
int insertItem,findItem,priorItem,nextItem;
help();
InitQueue(&Q);
while(1)
{
printf("Slect the operation you want to do: input the correspond number!you can enter 1 to view the example ,and enter 8 to view the help./n");
scanf("%c",&userInput);
switch(userInput)
{
case '1':QueueExample();break;
case '2':
for(;;)
{
printf("Please input a integer you want to push into the queue:/n");
printf("Forexample 1 . if you do not want to push a number into the queue again,you can input -1/n");
scanf("%d",&insertItem);
if (insertItem == -1)
break;
QueueEn(&Q,insertItem);
PrintQueue(&Q);
}
break;
case '3':
QueueDefirst(&Q);
PrintQueue(&Q);
break;
case '4':
for(;;)
{
printf("Please input a integer you want to find from the queue:/n");
printf("Forexample 1 . if you do not want to find a number from the queue again,you can input -1/n");
scanf("%d",&findItem);
if (findItem == -1)
break;
FindElement(&Q,findItem);
PrintQueue(&Q);
}
break;
case '5':
for(;;)
{
printf("Please input a integer you want to get the prior from the queue:/n");
printf("Forexample 1. if you do not want to get the prior form the queue again,you can input -1/n");
scanf("%d",&priorItem);
if (priorItem == -1) break;
PriorElement(&Q,priorItem);
PrintQueue(&Q);
}
break;
case '6':
for(;;)
{
printf("Please input a integer you want to get the next from the queue:/n");
printf("Forexample 1. if you do not want to get the next form the queue again,you can input -1/n");
scanf("%d",&nextItem);
if (nextItem == -1) break;
NextElement(&Q,nextItem);
PrintQueue(&Q);
}
break;
case '7':QueueLength(&Q);break;
case '8':help();break;
case '9':PrintQueue(&Q);break;
case 'Q':break;
case 'q':break;
}
if (userInput == 'q'|| userInput == 'Q')
break;
}
return 0;
}
如果代码中有什么错误,希望您不吝赐教,您可以把您的意见发送到[email protected],我会尽快给您回复的。