C指针编程之道(三)- 数据结构中指针的应用
来源:互联网 发布:男宝胶囊和知柏地黄丸 编辑:程序博客网 时间:2024/05/24 05:21
循环队列(数组实现)
# include <stdio.h># define CIRQUEUE_LENGTH 8typedef struct { int font; //对头标识 int rear; //队尾标识 int counter; //计数器 int data[CIRQUEUE_LENGTH]; //队列数据} cirqueue;int main() { cirqueue *queue; void init_queue(cirqueue *); int is_queue_full(cirqueue *); int is_queue_empty(cirqueue *); int in_queue(cirqueue *, int); int out_queue(cirqueue *, int *); void traverse_queue(cirqueue *); //初始化 init_queue(queue); //入队 in_queue(queue, 1); in_queue(queue, 2); in_queue(queue, 3); traverse_queue(queue); //出队 int out_data = 0; out_queue(queue, &out_data); printf("out_data=%d\n", out_data); out_queue(queue, &out_data); printf("out_data=%d\n", out_data); out_queue(queue, &out_data); printf("out_data=%d\n", out_data); traverse_queue(queue); return 0;}//队列初始化void init_queue(cirqueue *q){ q->font = 0; q->rear = 0; q->counter = 0;}//判断队满int is_queue_full(cirqueue *q) { return q->counter==CIRQUEUE_LENGTH;}//判断队空int is_queue_empty(cirqueue *q) { return q->counter==0;}//入队int in_queue(cirqueue *q, int data) { if (is_queue_full(q)) { printf("栈已满!\n"); return 0; } else { q->data[q->rear] = data; q->counter++; q->rear = (q->rear+1) % CIRQUEUE_LENGTH; return 1; }}//出队int out_queue(cirqueue *q, int *pdata) { if (is_queue_empty(q)) { return 0; } else { *pdata = q->data[q->font]; q->counter--; q->font = (q->font+1) % CIRQUEUE_LENGTH; return 1; }}//遍历队列void traverse_queue(cirqueue *q) { if (is_queue_empty(q)) { printf("栈为空!\n"); } else if (is_queue_full(q) || q->font > q->rear) { for (int i=q->font; i<q->rear+CIRQUEUE_LENGTH; i++) { printf("%d ", q->data[i%CIRQUEUE_LENGTH]); } printf("\n"); } else if (q->font < q->rear) { for (int i=q->font; i<q->rear; i++) { printf("%d ", q->data[i]); } printf("\n"); }}
堆栈(数组实现)
# include <stdio.h># define STACK_LENGTH 8typedef struct { int top; int data[STACK_LENGTH];} stack;int main() { stack *pstack; void init_stack(stack *); int is_stack_empty(stack *); int is_stack_full(stack *); int push_stack(stack *, int); int pop_stack(stack *, int *); void print_pop_stack(stack *, int *); //初始化 init_stack(pstack); //入栈 push_stack(pstack, 1); push_stack(pstack, 2); push_stack(pstack, 3); //出栈 int data = 0; print_pop_stack(pstack, &data); print_pop_stack(pstack, &data); print_pop_stack(pstack, &data); return 0;}//栈的初始化void init_stack(stack *pstack) { pstack->top = 0; //栈顶位置=栈底位置}//栈空判断int is_stack_empty(stack *pstack) { return pstack->top==0;}//栈满判断int is_stack_full(stack *pstack) { return pstack->top==STACK_LENGTH;}//进栈int push_stack(stack *pstack, int data){ if (is_stack_full(pstack)) { printf("堆栈已满!\n"); return 0; } else { pstack->data[pstack->top] = data; pstack->top++; return 1; }}//出栈int pop_stack(stack *pstack, int *data) { if (is_stack_empty(pstack)) { printf("堆栈为空!\n"); return 0; } else { pstack->top--; *data = pstack->data[pstack->top]; return 1; }}//打印出栈void print_pop_stack(stack *pstack, int *data) { int status = pop_stack(pstack, data); if (status) { printf("%d\n", *data); }}
链表
单链表的结构:
头结点 结点1 结点2 结点3 |---> data1 |---> data2 |---> data3add1--- add2--- add3--- NULL
循环链表的结构:
结点1 结点2 结点3 结点4 |--> data1 |---> data2 |---> data3 |--->data4 | add2--- add3--- add4--- add1--- | | ------------------------------------------------- [注] 相对于一般的链表,程序中使用较多的还是循环链表
满二叉树和完全二叉树
完全二叉树的链式存储结构:
原型 Root | | ----------------- | | ST1 ST2 ------- -------- | | ST1_1 ST2_2链式存储结构: Lchild Root Rchild | | | | Lchild ST1 NULL NULL ST2 Rchild | | | | NULL ST1_1 NULL NULL ST2_2 NULL
二叉树的构建:
# include <stdio.h># include <stdlib.h>typedef struct tree_node { char data; //值域 struct tree_node *lchild, *rchild; //左右指针域} Tnode;Tnode * create_tree(Tnode *);void pre_traverse_tree(Tnode *);void mid_traverse_tree(Tnode *);void post_traverse_tree(Tnode *);int main() { printf("please input tree data:\n"); Tnode * tree; tree = create_tree(tree); printf("\n前序遍历:\n"); pre_traverse_tree(tree); printf("\n中序遍历:\n"); mid_traverse_tree(tree); printf("\n后序遍历:\n"); post_traverse_tree(tree); printf("\n"); return 0;}//递归创建树Tnode * create_tree(Tnode *tnode) { char ch; ch = getchar(); if (ch == '*') { tnode = NULL; } else { tnode = (Tnode *) malloc(sizeof(Tnode)); tnode->data = ch; tnode->lchild = create_tree(tnode->lchild); tnode->rchild = create_tree(tnode->rchild); } return tnode;}//遍历一般分为三种:前序遍历、中序遍历、后序遍历,三种遍历均是以根节点为核心命名的//前序遍历:根节点--->左子树--->右子树//中序遍历:左子树--->根节点--->右子树//后序遍历:左子树--->右子树--->根节点void pre_traverse_tree(Tnode *tnode) { //前序遍历 if (tnode) { printf(" %c\t", tnode->data); pre_traverse_tree(tnode->lchild); pre_traverse_tree(tnode->rchild); }}void mid_traverse_tree(Tnode *tnode) { //中序遍历 if (tnode) { mid_traverse_tree(tnode->lchild); printf(" %c\t", tnode->data); mid_traverse_tree(tnode->rchild); }}void post_traverse_tree(Tnode *tnode) { //后序遍历 if (tnode) { post_traverse_tree(tnode->lchild); post_traverse_tree(tnode->rchild); printf(" %c\t", tnode->data); }}/* ------------------- a b c d e f g h j ------------------- 运行结果: please input tree data: abd**e*hj***cf**g** 前序遍历: a b d e h j c f g 中序遍历: d b e j h a f c g 后序遍历: d j h e b f g c a */
0 0
- C指针编程之道(三)- 数据结构中指针的应用
- C指针编程之道(一)- 指针与数组
- C指针编程之道(二)- 指针与函数
- 有关于C指针的一些知识点(又名C指针编程之道读书笔记)
- C语言中结构体与指针的若干问题(在数据结构中的应用)
- C语言指针笔记之 一级指针的应用(指针做函数参数)
- C指针编程之道(四)- 指向文件类型的指针
- C语言编程指针的应用场合
- 指针的简单应用(三) 之指针与数组续
- C语言:指针的三道编程练习
- 编程经验点滴(三)——《C、C++中指针加 1 的问题》
- C/C++中指针,多级指针的理解和应用
- c中指针的指针
- c语言之多级指针的应用
- C 中指针的复杂应用
- C语言中指针的应用
- c语言中函数指针的应用
- C/C++数据结构中利用指针的三种初始化方式
- Spring MVC 的基本工作原理:
- 一个uri有网络图片,把这个图片设置到imageview
- VS中的debug和releasse版本的区别
- 想在Images.xcassets 只能用 imageNamed 加载里边的素材 其他方法 你就别费老劲了
- VOC数据集具体格式
- C指针编程之道(三)- 数据结构中指针的应用
- Java动态代理与CGLIB
- UITableView中点击UIImageView,但是图片大小改变的问题
- iOS 开发笔记-AFNetWorking https SSL认证
- Unity制作扫雷游戏
- NumPy教程之一
- doT.js详细用法介绍
- 关于Vector等STL容器作为dll函数接口参数的问题
- C指针编程之道(四)- 指向文件类型的指针