实现链队各种基本运算的算法
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); } }
相关文章推荐
- 实现顺序队列各种基本运算的算法
- 实现顺序串各种基本运算的算法
- !实现链串各种基本运算的算法
- 实现二叉树各种基本运算的算法
- 实现环形队列各种基本运算的算法(用顺序结构)
- 环形队列各种基本运算的算法
- 二叉树各种基本运算的算法
- 实现二叉树各种基本运算的算法
- C语言 循环单链表各种基本运算的算法
- 实现顺序表各种基本运算算法
- 实现顺序表各种基本运算的算法
- 顺序串各种基本运算的算法
- 实现循环单链表各种基本运算的算法
- 实现单链表各种基本运算的算法
- 单链表各种基本运算的算法
- 实现顺序栈的各种基本运算的算法
- 实现双链表的各种基本运算的算法
- 实现二叉树的各种基本运算的算法
- !实现循环双链表各种基本运算的算法
- 实验3_2 实现链栈各种基本运算的算法