C语言二叉树与队列实现基础代码
来源:互联网 发布:cda数据分析师怎么考证 编辑:程序博客网 时间:2024/06/06 03:36
http://blog.csdn.net/xzongyuan/article/details/21830097
c语言队列基本操作代码
基于是上一篇做了修改,主要修改结构提,因为准备加入树的操作,代码会越来越繁杂,为了还原整个改写过程,分步显示,#include<stdio.h>#include<stdlib.h>typedef struct _node{ int value; struct _node *left; struct _node *right;}TNode,*Tree;//add a *next in q_node is my purpose//other wise , we need to add in the Tree node struct//So, for the sake of doesn't modify the struct of tree//I design a q_node struct to include it//we can use define command to make it as a template.typedef struct q_node{ TNode *t_node; struct q_node *next;}QNode;typedef struct _Queue{ QNode *head; QNode *tail;}Queue;Queue* init_queue(){ Queue *queue=(Queue*)malloc(sizeof(Queue)); queue->head = queue->tail = NULL; return queue;}int enQueue(Queue *pQueue,QNode *pQNode){ if(pQueue->head == NULL) {//when it's empty pQueue->head = pQNode; pQueue->tail = pQNode; } else { pQueue->tail->next = pQNode; pQueue->tail = pQNode; }}QNode* deQueue(Queue *pQueue){if(pQueue->head == NULL){ return NULL;}QNode *deNode= pQueue->head;pQueue->head = pQueue->head->next; return deNode;}TNode* init_node(int value){ TNode *new_node = (TNode*)malloc(sizeof(TNode)); new_node->value=value; new_node->left = new_node->right = NULL; return new_node;}//0:emptyint ifEmpty(Queue *pQueue){ if(pQueue->head == NULL) { printf("empty tree\n"); return 0; } printf("queue is not empty\n"); return 1;}int main(){ Queue *queue=init_queue(); int i; ifEmpty(queue); printf("insert node to queue\n"); for(i=1; i<7;i++) { TNode *t_node = init_node(i); QNode *qNode = (QNode*)malloc(sizeof(QNode)); qNode->t_node = t_node; enQueue(queue,qNode); // free(node); } // Node *node = init_node(1);// printf("node->value = %d\n",node->value);// enQueue(queue,node); ifEmpty(queue); for(i=0;i<7;i++) { QNode *deNode = deQueue(queue); if(deNode==NULL) { printf("NULL\n"); } else { printf("deNode->value = %d\n",deNode->t_node->value); } } free(queue); return 0;}
0 0
- C语言二叉树与队列实现基础代码
- C语言二叉树知识点讲解与实现代码
- 优先队列二叉堆 C语言实现
- 数据结构中栈与队列的c语言代码实现
- C语言实现链队列代码
- 简单的C语言二叉树的实现代码
- 二叉树的实现与操作(C语言实现)
- c语言实现二叉树层次遍历(借助队列实现)
- C语言实现二叉树
- C语言实现二叉树
- C语言实现二叉树
- C语言实现二叉树
- 二叉树的创建与遍历(C语言实现)
- c语言二叉树的存储表示与实现
- 二叉树构造与遍历的C程序实现代码
- C语言基础 16 二叉树 算法
- 队列 C语言实现
- 队列C语言实现
- NSRunLoop之夜
- 对于缓存的理解和分析。
- 连载:面向对象葵花宝典:思想、技巧与实践(7) - “对象”新解
- C# 委托实例(跨窗体操作控件)
- 黑马程序员_工具使用02_Xcode的快捷键及代码格式化
- C语言二叉树与队列实现基础代码
- RSHC PHP MVC框架編寫手記[2]
- java throws 用法
- 对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
- 【SHOI2009】善意的投票
- 畅通工程续_floyd实现
- how to intall stardict on the ubuntu
- Android-NDK-r9编译ffmpeg-0.11.3
- Java中连接SAP进行RFC调用