带头结点的链队列实现(C语言)
来源:互联网 发布:电脑usb端口无法识别 编辑:程序博客网 时间:2024/05/16 15:12
点击打开链接
#include <stdio.h>#include <string.h>#include <malloc.h>#include <stdlib.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0typedef struct queue_node{ int data; struct queue_node *next; }qnode; typedef struct link_queue { qnode *head; qnode *tail; }lqueue; /* 函数功能: 链队列初始化 */ int init_link_queue(lqueue *Q) { qnode *tmp_node = (qnode *)malloc(sizeof(qnode)); if(!tmp_node) { return ERROR; } tmp_node->next = NULL; Q->head = tmp_node; Q->tail = tmp_node; return OK; } /* 函数功能: 队列是否为空 返 回 值: 1 空; 0 非空 */ int is_queue_empty(lqueue Q) { return ((Q.head == Q.tail) ? TRUE : FALSE); } /* 函数功能: 元素num入队 */ int enqueue(lqueue *Q, int num) { qnode *tmp_node = (qnode *)malloc(sizeof(qnode)); if(!tmp_node) { return ERROR; } tmp_node->next = NULL; tmp_node->data = num; Q->tail->next = tmp_node; Q->tail = tmp_node; return OK; } /* 函数功能: 出队,队头元素存入num */ int dequeue(lqueue *Q, int *num) { qnode *tmp_node = NULL; if(is_queue_empty(*Q)) { return ERROR; } tmp_node = Q->head->next; Q->head->next = tmp_node->next; //队头出队之后,结点减少一个,更新队头位置 *num = tmp_node->data; if(Q->head->next == NULL) { Q->tail = Q->head; } free(tmp_node); return OK; } /* 函数功能: 获取队头元素,存入num */ int get_elem(lqueue Q, int *num) { if(is_queue_empty(Q)) { return ERROR; } *num = Q.head->next->data; return OK; } /* 函数功能: 打印队列元素 */ void print_queue(lqueue Q) { lqueue tmpQ = Q; tmpQ.head = (qnode *)malloc(sizeof(qnode)); tmpQ.head = Q.head->next; printf("Head->*->"); while(tmpQ.head != NULL) { printf("%d->", tmpQ.head->data); tmpQ.head = tmpQ.head->next; } free(tmpQ.head); printf("TAIL\n"); } /* 函数功能: 销毁队列 */ void destory_queue(lqueue *Q) { qnode *tmp_node = NULL; while(Q->head->next != NULL) { tmp_node = Q->head->next; Q->head->next = tmp_node->next; free(tmp_node); } } /* 函数功能: 获得队列长度 返 回 值: 长度 */ int get_queue_length(lqueue Q) { int length = 0; lqueue tmpQ; tmpQ.head = (qnode *)malloc(sizeof(qnode)); tmpQ.head = Q.head->next; if(is_queue_empty(Q)) { return length; } do { length++; tmpQ.head = tmpQ.head->next; }while(tmpQ.head != NULL); free(tmpQ.head); return length; } int main(int argc, char *argv[]) { int num = 0; lqueue queue; init_link_queue(&queue); enqueue(&queue, 7); enqueue(&queue, 8); enqueue(&queue, 9); enqueue(&queue, 6); printf("length = %d\n", get_queue_length(queue)); print_queue(queue); dequeue(&queue, &num); printf("length = %d, dequeue num = %d\n", get_queue_length(queue), num); print_queue(queue); destory_queue(&queue); return 0; }
运行结果:
- 带头结点的链队列实现(C语言)
- 带头结点的链队列实现(C语言)
- 带头结点的链队列实现(C语言)
- C语言实现双向非循环链表(带头结点尾结点)的基本操作
- C语言实现双向非循环链表(带头结点尾结点)的节点插入
- 带头结点的链队列实现
- 不带头结点的单链表的实现(C语言)
- C语言实现单链表(带头结点)的基本操作
- C语言实现单链表节点的删除(带头结点)
- C语言实现单链表的节点插入(带头结点)
- C语言实现单链表的逆序打印(带头结点)
- 用c语言实现链栈 (带头结点)
- 双指针不带头结点的链栈的实现(C语言)
- 双指针不带头结点的链栈的实现(C语言)
- C语言实现双向非循环链表(不带头结点)的基本操作
- C语言实现双向非循环链表(不带头结点)的逆序打印
- C语言实现双向非循环链表(不带头结点)的节点插入
- C语言实现双向非循环链表(不带头结点)的清空
- 从此以后 陌路此生。伤感日志
- 记录下吧 JS 瀑布流
- gzip:stdin:not in gzip format的解决办法
- java网络编程02-Socket用法详解
- python编码与反编码 decode('unicode-escape')
- 带头结点的链队列实现(C语言)
- 用Visual studio11在Windows8上开发驱动实现注册表监控和过滤
- 笔试题:创建一个单链表,结点包含学生的学号,姓名,性别,年龄信息.写几个程序,实现按学生学号插入,查询,删除等操作.
- seleniumIDE+firebug+seleniumRC
- C#实现程序启动唯一实例的一种方法
- mastudio 从晚上获取表格
- Cocos2d-x初入学堂(3)-->TexturePacker非常棒的图像处理工具
- mysql reading initial communication packet
- mastudio 下载附件