队列的链式存储---链表实现(有头结点)
2015-07-04 13:37
295 查看
/* 队列的链式存储 */ /* with header */ /* with no typedef */ struct Node{ ElementType Ele; struct Node *Next; }; struct LinQ{ struct Node *rear; struct Node *front; }; struct LinQ * CreateQ( void ) { struct LinQ *Ptr; struct Node *header; Ptr = malloc(sizeof(struct LinQ)); if(Ptr == NULL ) Error("out of space "); header = malloc( sizeof( struct Node ) ); if(header == NULL ) error("out of space "); header->Next = NULL; Ptr->front = Ptr->rear = header; } void AddQ( struct LinQ *PtrQ, ElementType X ) { struct Node *Ptr; Ptr = malloc(sizeof(struct Node )); if(Ptr == NULL) Error("out of space "); Ptr->Ele = X; Ptr->Next = NULL; PtrQ->rear->Next = Ptr; PtrQ->rear = Ptr; } int IsEmpty( struct LinQ * PtrQ ) { return PtrQ->front->Next == NULL; } ElementType DeleteQ( struct LinQ * PtrQ ) { struct Node * TmpCell; if( IsEmpty( PtrQ ) ) Error("empty Queue"); Tmpcell = PtrQ->front->Next; PtrQ->front->Next = TmpCell->Next; free( TmpCell ); }
View Code
CreateQ旨在创造一个front和rear都指向头结点header的LinQ结构
头结点处Next设为NULL,在判断是否空队时可用
front一直指到头结点
新增结点的Next一定改为NULL
结论:可以用typedef的地方就用,可以用头结点的地方就用
相关文章推荐
- linux中fork()函数详解
- JAVA JSON 开发记录
- POJ之路13-POJ2388
- chown与chomd区别
- 在vim中使用zencoding/Emmet
- 初探linux内存管理
- 自定制模版
- Lua的线程和状态
- Lucas定理
- wget批量下载
- MySQL分区性能初探
- 前端工具(sublime)
- wamp -- php集成环境搭建
- wamp -- php集成环境搭建
- wamp -- php集成环境搭建
- LR:Code - 60990,Code - 10343 问题解决
- C#同步SQL Server数据库Schema
- apache、php -- 环境配置问题
- apache、php -- 环境配置问题
- apache、php -- 环境配置问题