数据结构---队列的链式实现
2015-04-16 15:08
417 查看
其实理解了一个数据结构后,它的各种实现与操作就能很容易的写出来 话说貌似下周数据结构有期中考试...Orz我翘了两次课 串和数组那地方还不太懂 要跪啊....
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct Node { ElemType Data; struct Node *Next; }Node, *PtrNode; typedef struct Queue { PtrNode Front; PtrNode Rear; }Queue, *LinkQueue; //create an empty linkqueue LinkQueue InitQueue(); //destroy linkqueue void DestroyQueue(LinkQueue Q); //clear a linkqueue void ClearQueue(LinkQueue Q); //if the linkqueue is empty return 1 int IsEmpty(LinkQueue Q); //get the length of linkqueue int QueueLength(LinkQueue Q); //get the head of linkqueue ElemType GetHead(LinkQueue Q); //insert an element to the rear of linkqueue void EnQueue(LinkQueue Q, ElemType E); //get and delete the front of linkqueue ElemType DeQueue(LinkQueue Q); void PrintQueue(LinkQueue Q); int main() { LinkQueue Q = InitQueue(); EnQueue(Q, 1); EnQueue(Q, 2); EnQueue(Q, 3); EnQueue(Q, 4); PrintQueue(Q); printf("%d\n", GetHead(Q)); printf("%d\n", QueueLength(Q)); DeQueue(Q); DeQueue(Q); PrintQueue(Q); DestroyQueue(Q); return 0; } LinkQueue InitQueue() { LinkQueue Q = (LinkQueue)malloc(sizeof(Queue)); Q->Front = Q->Rear = (PtrNode)malloc(sizeof(Node)); Q->Front->Next = NULL; return Q; } void DestroyQueue(LinkQueue Q) { while(Q->Front) { Q->Rear = Q->Front->Next; free(Q->Front); Q->Front = Q->Rear; } } void ClearQueue(LinkQueue Q) { Q->Rear = Q->Front; PtrNode Tmp = Q->Front->Next; while(Tmp) { Q->Front = Tmp->Next; free(Tmp); Tmp = Q->Front; } Q->Front = Q->Rear; } int IsEmpty(LinkQueue Q) { return (Q->Front == Q->Rear) ? 1 : 0; } int QueueLength(LinkQueue Q) { int count = 0; PtrNode Tmp = Q->Front; while(Tmp != Q->Rear) { Tmp = Tmp->Next; count ++; } return count; } ElemType GetHead(LinkQueue Q) { return Q->Front->Next->Data; } void EnQueue(LinkQueue Q, ElemType E) { PtrNode Tmp = (PtrNode)malloc(sizeof(Node)); Tmp->Data = E; Tmp->Next; Q->Rear->Next = Tmp; Q->Rear = Tmp; } ElemType DeQueue(LinkQueue Q) { PtrNode Tmp = Q->Front->Next; ElemType Data = Tmp->Data; Q->Front->Next = Tmp->Next; free(Tmp); return Data; } void PrintQueue(LinkQueue Q) { PtrNode Tmp = Q->Front->Next; while(Tmp) { printf("%d ", Tmp->Data); Tmp = Tmp->Next; } printf("\n"); }
相关文章推荐
- 数据结构实现链式队列(C语言)
- c语言数据结构实现-链式队列
- 【数据结构】用两个栈实现一个队列(链式)
- C语言 队列的链式结构的实现与表示 数据结构 队列的实现与表示
- [数据结构]队列之链式队列的类模板实现
- 【数据结构】实现一个队列(链式)
- 数据结构队列C++实现 链式和循环
- 数据结构之链式队列go言语实现
- 数据结构 - 队列简介 及 1个简单的c语言链式队列代码实现
- 数据结构学习笔记-队列的链式存储(C语言实现)
- [数据结构]链式队列的实现。
- 数据结构-链式队列的实现
- [置顶] 数据结构——队列的链式存储结构以及实现
- 重温数据结构:队列的链式实现、顺序实现及循环队列
- 数据结构(五)——双链表、链式栈、链式队列 及实现
- [数据结构]程杰队列的链式存储结构及实现代码
- 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列
- 数据结构--用C实现链式队列
- 数据结构之队列的链式表示和实现
- 数据结构队列的java实现,包括线性和链式两种方式