使用链表实现队列------《数据结构与算法分析-C语言描述》
2013-08-27 19:01
381 查看
经过ubuntu的gcc验证
一、头文件 que_link.h
二、c文件:que_link.c
三、打印输出
一、头文件 que_link.h
#ifndef _QUE_LINK_H_ #define _QUE_LINK_H_ struct que_record; typedef struct que_record* que; struct link_node; typedef struct link_node* node; typedef int elementType; int IsFull(que q); int IsEmpty(que q); que creatQue(int max_num); void makeEmpty(que q); void enque(elementType x,que q); void deque(que q); elementType front_que(que q); elementType front_deque(que q); void dispose_que(que q); struct que_record { node front; node rear; int size; }; struct link_node { elementType data; struct link_node* next; }; #endif
二、c文件:que_link.c
#include <stdio.h>
#include <stdlib.h>
#include "que_link.h"
#define MAXSIZE 10 int IsFull(que q) { return q->size == MAXSIZE; } int IsEmpty(que q) { return q->size == 0; } que creatQue(int max_num) { que q; q = (que)malloc(sizeof(struct que_record)); q->size = 0; q->front = q->rear = (node)malloc(sizeof(struct link_node)); q->front->next = q->rear->next = NULL; return q; } void makeEmpty(que q) { if(NULL == q) { printf("the que is not exsit \n"); exit(-1); } while(q->size) deque(q); } void deque(que q) { node ptr = NULL; ptr = q->front->next; free(q->front); q->front = ptr; q->size--; if(q->size == 0) { //q->front->next = q->rear->next = NULL; q->front = q->rear = NULL; } } void enque(elementType x, que q) { if(q) { if(IsFull(q)) { printf("the que is full \n"); exit(-4); } printf("the enque x is %d\n",x); static int init_flag = 0; if(!init_flag) { q->rear->data = x; q->rear->next = NULL; q->size++; init_flag = 1; } else { node ptr=(node )malloc(sizeof(struct link_node)); ptr->data = x; q->rear->next = ptr; q->rear = q->rear->next; q->rear->next = NULL; q->size++; } } } elementType front_que(que q) { if(q) { if(IsEmpty(q)) { printf("the que is empty\n"); exit(-5); } return q->front->data; } } elementType front_deque(que q) { if(q) { if(IsEmpty(q)) { printf("the que is empty,so can't deque\n"); exit(-6); } elementType x; x = q->front->data; deque(q); return x; } } void dispose_que(que q) { if(q) { makeEmpty(q); free(q); } } int main(int argc,char *argv[]) { elementType val; int i = 0; que q; q = creatQue(10); while(i++ < 10 ) { printf("now ,please input the value:\n"); scanf("%d",&val); printf("the val is %d\n",val); enque(val,q); printf("the q size is %d\n",q->size); if(val == 0) break; } while(q->size) { val = front_deque(q); printf("the val is %d\n",val); sleep(1); } dispose_que(q); return 0; }
三、打印输出
hangma@ubuntu:~/test/test/protest/que_test$ gcc que_link.c -o que_link hangma@ubuntu:~/test/test/protest/que_test$ ./que_link now ,please input the value: 1 the val is 1 the enque x is 1 the q size is 1 now ,please input the value: 2 the val is 2 the enque x is 2 the q size is 2 now ,please input the value: 3 the val is 3 the enque x is 3 the q size is 3 now ,please input the value: 4 the val is 4 the enque x is 4 the q size is 4 now ,please input the value: 5 the val is 5 the enque x is 5 the q size is 5 now ,please input the value: 6 the val is 6 the enque x is 6 the q size is 6 now ,please input the value: 7 the val is 7 the enque x is 7 the q size is 7 now ,please input the value: 8 the val is 8 the enque x is 8 the q size is 8 now ,please input the value: 9 the val is 9 the enque x is 9 the q size is 9 now ,please input the value: 10 the val is 10 the enque x is 10 the q size is 10 the val is 1 the val is 2 the val is 3 the val is 4 the val is 5 the val is 6 the val is 7 the val is 8 the val is 9 the val is 10
相关文章推荐
- 队列——链表与数组实现(数据结构与算法分析C语言版)
- 数据结构——使用双端链表实现队列(java实现)
- Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)
- JAVA实现最简单的双向链表、队列与栈(内部使用链表形式存储)
- java使用数组和链表实现队列示例
- C#基础之温习--使用列表和链表实现优一个先级队列
- 使用带有头尾节点的单链表实现队列
- Lua中使用table实现的其它5种数据结构(数组、链表、队列、集合和包、StringBuild)
- 用链表实现栈----《数据结构与算法分析----C语言描述》
- 栈和队列(使用链表实现)
- 数据结构与算法分析(Java 语言描述)(35)—— 使用两个栈实现一个队列
- cpp: 使用类模板以及链表实现队列Queue
- Java中队列的实现(2)-使用单向链表以及实现
- java使用链表实现队列
- 对象的集合--队列,使用链表来实现
- 链表实现的队列程序(仅供自己学习使用)
- 数据结构与算法-----队列-使用链表(链式结构)实现
- 数据结构与算法分析(Java 语言描述)(36)—— 使用两个队列实现一个栈
- 使用数组实现队列----《数据结构与算法分析---C语言描述》
- 使用链表实现队列