您的位置:首页 > 其它

实现链队各种基本运算的算法

2018-02-02 10:45 295 查看
/*Title:实现链队各种基本运算的算法
Time:2018-2-2
Author:邹吉祥
School:imnu
*/
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct linknode
{
ElemType data;
struct linknode* next;
}QNode;

typedef struct
{
QNode* Front;  //队头指针;
QNode* Rear;    //队尾指针;
}LiQueue;

void InitQueue(LiQueue *& q)
{
q=(LiQueue*)malloc(sizeof(LiQueue));
q->Front=q->Rear=NULL;
}

void DestroyQueue(LiQueue *& q)
{
QNode *p=q->Front,*r;
if(p!=NULL){
r=p->next;
while(r!=NULL)
{
free(p);
p=r;
r=p->next;
}
}
free(p);
free(q);  //
}

bool QueueEmpty(LiQueue *q){

return (q->Rear==NULL);
}

void enQueue(LiQueue *& q,ElemType  e) //入队;
{
QNode *p=(QNode*)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
if(q->Rear==NULL){ //若链队为空时;
q->Front=q->Rear=p;
}
else
{
q->Rear->next=p;
q->Rear=p;
}
}

bool deQueue(LiQueue *& q,ElemType &e)
{
QNode *t;
if(q->Rear==NULL) //链队为空;
{
return false;
}
t=q->Front; //我感觉此处的赋值是结点之间的赋值;
if(q->Rear==q->Front)  //链队中只有一个结点的时候;
{
q->Rear=q->Front=NULL;
}
else
{
q->Front=q->Front->next;
}
e=t->data;
free(t);
return true;
}

int main()
{
LiQueue* q;
ElemType e;
InitQueue(q);  //初始化;
enQueue(q,'a');
enQueue(q,'b');
enQueue(q,'c');
enQueue(q,'d');
while(!QueueEmpty(q))
{
deQueue(q,e);
printf("%c\n",e);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: