队列的链表实现
2015-09-23 11:20
323 查看
queue.h:
实现文件
implementation.c:
测试文件:
main.c:
#ifndef QUEUE_H_INCLUDED #define QUEUE_H_INCLUDED struct Node; typedef struct Node QNode; struct Queue; typedef struct Queue *PtrQ; PtrQ CreateQueue(); void MakeEmpty(PtrQ Q); int DeQueue(PtrQ Q); void EnQueue(int X, PtrQ Q); #endif // QUEUE_H_INCLUDED
实现文件
implementation.c:
#include<stdio.h> #include "queue.h" typedef struct Node{ int Data; struct Node *Next; }QNode; typedef struct Queue{ QNode *Rear; QNode *Front; }*PtrQ; PtrQ CreateQueue(){ PtrQ Q; Q = malloc(sizeof(struct Queue)); MakeEmpty(Q); return Q; } void MakeEmpty(PtrQ Q) { Q->Rear = Q->Front = NULL; } int DeQueue(PtrQ Q) { QNode *FirstCell; int FirstElem; if(Q->Front == NULL){ printf("Error, EmptyQueue!"); return -1; } FirstCell = Q->Front; if(Q->Rear == Q->Front) { Q->Rear = Q->Front = NULL; } else { Q->Front = Q->Front->Next; } FirstElem = FirstCell->Data; free(FirstCell); return FirstElem; } void EnQueue(int X, PtrQ Q) { QNode *TmpCell; TmpCell = malloc(sizeof(struct Node)); TmpCell->Next = NULL; TmpCell->Data = X; if(Q->Front == NULL) { Q->Rear = TmpCell; Q->Front = TmpCell; } else if(Q->Front == Q->Rear) { Q->Rear = TmpCell; Q->Front->Next = Q->Rear; } else{ Q->Rear->Next = TmpCell; Q->Rear = TmpCell; } }
测试文件:
main.c:
#include <stdio.h> #include <stdlib.h> #include "queue.h" int main() { PtrQ Q; int a, b, c; Q = CreateQueue(); EnQueue(1, Q); EnQueue(2, Q); a = DeQueue(Q); b = DeQueue(Q); printf("%d ", a); EnQueue(3, Q); c = DeQueue(Q); printf("%d ", b); printf("%d", c); return 0; }
相关文章推荐
- Android 内存管理之优化建议
- Quartz2D基础
- qml学习--------------利用Canvas绘制简单图形
- XE8-indy10中TIdTCPConnection.Connected函数的源码
- sql执行效率检测 mysql explain
- siglongjmp、sigsetjmp
- Tyvj 题目1088 treat(DP+记忆化搜索)
- An invalid form control with name='xxx' is not focusable
- 黑马程序员---C语言学习笔记之语言模块化编程概念
- 深入理解PHP之内存引用一
- DynamicCRM清理PrincipalObjectAccess表,清理POA,清理WorkFlowLog
- 简单桶排序算法-python实现
- 希尔排序算法-python实现
- Spring No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency
- Bootstrap学习笔记-响应式布局原理
- 更改所有工程的编译环境-jdk
- 医疗保障保障生活
- 浮动窗体 点击鼠标就出错
- 3D魔方场景
- 怎样将多个CSS文件导入一个CSS文件中