C语言实现链式队列
2013-10-16 21:12
411 查看
C语言实现,只是简单实现了初始化、入队、出队和打印四个功能比较简单。
#include<stdio.h> #include<stdlib.h> typedef int elemType; typedef struct qNode{ elemType info; struct qNode *next; }qNode; typedef struct{ qNode *front; qNode *rear; }LinkQueue; void initQueue(LinkQueue *q){ // q->front = q->rear = (qNode *)malloc(sizeof(qNode)); // q->front->next = NULL; q->front = q->rear = NULL; } void enQueue(LinkQueue *q, elemType value){ qNode *ptr; ptr = (qNode *)malloc(sizeof(qNode)); ptr->info = value; ptr->next = NULL; if(q->front == NULL ){ q->front = ptr; q->rear = ptr; } else{ q->rear->next = ptr; q->rear = ptr; } } int deQueue(LinkQueue *q){ qNode *ptr = q->front; if(q->front != NULL){ if(ptr != q->rear) q->front = ptr->next; else q->front = q->rear = NULL; free(ptr); return 0; } printf("empty queue !\n"); return -1; } void printQueue(LinkQueue *q){ qNode *ptr = q->front; if(ptr == q->rear && ptr == NULL) return ; if(ptr != NULL && q->front == q->rear){ printf("%d\n",ptr->info); return ; } while(ptr != q->rear){ printf("%d -> ",ptr->info); ptr = ptr->next; } if(q->front != NULL) printf("%d\n",ptr->info); return ; } int main(){ LinkQueue queue; int i; initQueue(&queue); deQueue(&queue); deQueue(&queue); for(i=0;i<10;i++){ enQueue(&queue,i); printQueue(&queue); } for(i=0;i<12;i++){ if(0 == deQueue(&queue)); printQueue(&queue); } return 0; }
相关文章推荐
- 队列(C语言实现,基于链式结构)
- 数据结构学习笔记-队列的链式存储(C语言实现)
- 数据结构---C语言之队列的链式表示和实现(链队列)
- 对链式队列的实现(C语言)
- 数据结构C语言实现之链式队列的6种算法代码
- 数据结构 - 队列简介 及 1个简单的c语言链式队列代码实现
- C语言一个队列的实现(链式)
- c语言实现链式队列
- 队列的链式结构C语言实现
- 队列的链式存储实现c语言
- c语言实现循环队列和链式队列
- 数据结构(严蔚敏)之六——链式队列c语言实现
- (C语言)队列的链式实现(数据结构十一)
- C语言 队列 链式结构 实现
- (C语言)队列的链式实现(数据结构十一)
- 链式队列ADT实现(C语言)2018.3.14
- 链式队列的基础操作实现(C语言)
- 数据结构之---C语言实现链式队列
- 队列的链式实现(C语言)
- C语言实现一个链式队列