链队列基本函数
2017-11-30 15:29
162 查看
头文件:
子函数:
主函数:
#ifndef LINKQUEUE_H_ #define LINKQUEUE_H_ #define SUCCESS 10000 #define FAILURE 10001 typedef int ElemType; typedef struct node { ElemType data; struct node *next; }Node; typedef struct queue { Node *front; Node *rear; }Queue; #endif
子函数:
#include <stdio.h> #include <stdlib.h> #include "LinkQueue.h" int InitQueue(Queue *L) { L->front =(Node *)malloc(sizeof(Node)); if(NULL == L->front) { return FAILURE; } L->front->next = NULL; L->rear = L->front; return SUCCESS; } int EnQueue(Queue *L,ElemType e,int count) { Node *n = (Node *)malloc (sizeof(Node)); n->next = NULL; n->data = e; L->rear->next = n; L->rear = n; return SUCCESS; } int QueueLength(Queue L) { Node *n = L.front; int count = 0; while(n->next != NULL) { count++; n = n->next; } return count; } int GetTop(Queue L) { while(NULL != L.front->next) { return L.front->next->data; } } int DeleteQueue(Queue *L) { ElemType e; Node *p = L->front->next; if(NULL == p) { return FAILURE; } L->front->next = p->next; e = p->data; free(p); if(L->front->next == NULL) { L->rear = L->front; } return e; } int ClearQueue(Queue *L) { Node *p = L->front->next; if(NULL == p) { return FAILURE; } while(NULL != p) { L->front->next = p->next; free(p); p = L->front->next; if(L->front->next == NULL) { L->rear = L->front; } } } int DestroyQueue(Queue *L) { free(L->front); L->front = NULL; L->rear = NULL; return SUCCESS; }
主函数:
#include <stdio.h> #include "LinkQueue.h" int main() { int ret,i; ElemType e; Queue sq; ret = InitQueue(&sq); if(FAILURE == ret) { printf("Init Failure!\n"); } else { printf("Init Success!\n"); } for(i = 0; i < 8; i++) { e = i + 1; ret = EnQueue(&sq,e); if(FAILURE == ret) { printf("Enter %d Failure!\n",e); } else { printf("Enter %d Success!\n",e); } } printf("Length is %d\n",QueueLength(sq)); printf("The top is %d\n",GetTop(sq)); for(i = 0; i < 7; i++) { ret = DeleteQueue(&sq); if(FAILURE == ret) { printf("Delete Failure!\n"); } else { printf("Delete %d Success!\n",ret); } } ret = ClearQueue(&sq); if(FAILURE == ret) { printf("Clear Failure!\n"); } else { printf("Clear Success!\n"); } ret = DestroyQueue(&sq); if(FAILURE == ret) { printf("Destroy Failure!\n"); } else { printf("Destroy Success!\n"); } return 0; }
相关文章推荐
- 栈和队列基本函数
- 循环队列的基本函数操作
- iOS边练边学--GCD的基本使用、GCD各种队列、GCD线程间通信、GCD常用函数、GCD迭代以及GCD队列组
- 基本sql语句与oracle函数
- 系统调用-进程创建函数基本用法1
- ROW_NUMBER() OVER函数的基本用法
- Linux下简单Shell实现(二)基本功能---主函数及init()函数
- 进程间通信系列 之 消息队列函数(msgget、msgctl、msgsnd、msgrcv)及其范例
- 单链表的操作和函数指针的基本使用
- 数据库基本操作及函数概括
- 【OpenCV笔记 01】OpenCV基本函数介绍
- “栈与队列”之栈--基本数据结构
- Lua中的基本函数库
- Oracle的基本学习(三)—函数
- JavaScript——1(基本语法,类型,变量与运算符,控制流程,函数)
- 利用两个栈实现队列的--->基本操作
- linux网络编程之System V 消息队列(一):消息队列内核结构和msgget、msgctl 函数
- 基本语言细节--inline函数的几点分析
- JS高级程序设计学习笔记之第三章基本概念(语法,数据类型,流控制语句,函数)——查漏补缺
- mysql学习笔记1--MySQL存储函数之基本常用函数的分类与使用