数据结构之队列的链式表示及其实现

记录一下自己学习过程中写的代码。以下是我看严蔚敏老师的数据结构教材后,结合教材所讲用C语言实现了关于队列的链式表示及其实现的基本操作,供以后复习所用。本程序建立的队列是一个链式队列,在队列建立之初,队头指针和队尾指针都指向新开辟的头结点,且头结点在整个过程中只存放指针,不存放任何数据。如此,便于程序运行过程中对队列的各种操作。 

编译软件:VC++6.0 

测试用例结果截图如下:



源代码如下:

/********************************** 
队列的链式表示和实现(完整代码,C实现)
Author:大地在我腳下 
Date:2016-7-30
Email:jsrcdjcyy@163.com 
**********************************/  
#include<stdio.h>
#include<stdlib.h>

typedef struct QNode  
{  
 int data;  
 struct QNode *next;  
}QNode, *QueueNode;  

typedef struct LinkQueue
{  
 QueueNode Front;  
 QueueNode Rear;
 int Queuesize;
}LinkQueue,*Queue;  

Queue CreateQueue();//建立带头结点的队列
void DeleteQueue(Queue,int*);//在队头删除元素
bool QueueEmpty(Queue); //队列是否为空
bool EnterQueue(Queue,int);//在队尾插入元素
void TraverseQueue(Queue);//遍历队列并输出元素
void DestroyQueue(Queue);//销毁已存在的队列
void ClearQueue(Queue);//清空已存在的队列
int Queuelength(Queue);//计算队列长度,即队列元素个数
int GetHead(Queue);//返回队列队头元素


void main()
{int d;
//创建一个空的队列,pQueue指针指向该队列
Queue pQueue= CreateQueue();  

//进行入队(插入)操作,之后遍历输出队列内数据
EnterQueue(pQueue,6);
EnterQueue(pQueue,25);
EnterQueue(pQueue,89);
EnterQueue(pQueue,127);
EnterQueue(pQueue,888);
TraverseQueue(pQueue);

//当前队列的长度
printf("\nNow the length of queue is:%d\n",Queuelength(pQueue));

//取出队头元素
if(QueueEmpty(pQueue)) 
    printf("\nNow the queue is empty!");
else printf("Now the head of queue is:%d\n",GetHead(pQueue));

//进行出队(删除)操作,之后遍历输出
if(QueueEmpty(pQueue)) 
   printf("\nNow the queue is empty!");
else
{printf("\nDeleting succeed!\n");
 DeleteQueue(pQueue,&d);
 printf("The deleted data is:%d\n",d);
 TraverseQueue(pQueue);
}

//重新输出队列的长度
printf("\nNow the length of queue is:%d\n",Queuelength(pQueue));

//重新取出队头元素
if(QueueEmpty(pQueue)) 
    printf("\nNow the queue is empty!");
else printf("Now the head of queue is:%d\n",GetHead(pQueue));

//清空队列,并输出清空后队列中的数据  
ClearQueue(pQueue);  
printf("\ndata cleared!\n");  
TraverseQueue(pQueue);
}

//建立带头结点的队列
Queue CreateQueue()
{Queue q=(Queue)malloc(sizeof(LinkQueue));
 QueueNode p=(QueueNode)malloc(sizeof(QNode));
 if(!p||!q)
 {printf("Malloc failed!");
  exit(-1);
 }
 p->data=0;
 q->Front=q->Rear=p;
q->Front->next=NULL;
q->Queuesize=0;
return q;
}

bool EnterQueue(Queue Q,int e)
{QueueNode p=(QueueNode)malloc(sizeof(QNode));
if(!p)
 {printf("Malloc failed!");
  exit(-1);
 }
 p->data=e;
 p->next=NULL;
 Q->Rear->next=p;
 Q->Rear=p;
 Q->Queuesize++;
 return true;
}

bool QueueEmpty(Queue Q)
{if(Q->Rear==Q->Front) return true;
else return false;
}

void DeleteQueue(Queue Q,int *d)
{QueueNode p=Q->Front->next;
 if(QueueEmpty(Q)) printf("Now the queue is empty!");
else 
{*d=p->data;
 Q->Front->next=p->next;
 if(Q->Rear==p) Q->Rear=Q->Front;
 free(p);
 Q->Queuesize--;
}
}

void TraverseQueue(Queue Q)
{QueueNode p=Q->Front->next;
if(QueueEmpty(Q))  printf("Now the queue is empty!\n");
else 
{printf("Now datas in the queue are:\n");
while(p)
{printf("%d",p->data);
 p=p->next;
 putchar(32);
}
printf("\n");
}
}

void DestroyQueue(Queue Q)
{QueueNode q,p=Q->Front->next;  
 while(p)
{q=p->next;
 free(p);
 p=q;
}
Q->Rear=Q->Front;
}

void ClearQueue(Queue Q)
{if(QueueEmpty(Q))  printf("Now the queue is empty!");
else DestroyQueue(Q);
}

int Queuelength(Queue Q)
{return Q->Queuesize;
}

int GetHead(Queue Q)
{return Q->Front->next->data;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值