数据结构链接汇总:
【数据结构-堆/栈 C语言实现】
【数据结构-链表/顺序表 C语言】
【数据结构-队列 C语言】
【数据结构-二叉树遍历 C语言】
1.二叉树-广度遍历实验代码
二叉树的广度优先遍历:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define size 100
typedef struct node//树的结点
{
char data;
struct node* rchild;
struct node* lchild;
}nodes;//树的节点信息
typedef struct queue//创建一个顺序队列
{
nodes* num[size];
int front;
int rear;
}queues;
nodes* pop(queues* ls)//出队
{
nodes* temp = ls->num[ls->front];
ls->front++;
return temp;
}
queues* push(queues* ls, nodes* key)//入队
{
ls->num[ls->rear] = key;
ls->rear++;
return ls;
}
int isempty(queues* ls)//判断队列是否为空
{
if (ls->front == ls->rear) return 1;
return 0;
}
void cengcibianli(nodes* t)//层次优先遍历,即是广度优先遍历
{
nodes* temp = (nodes*)malloc(sizeof(nodes));
queues* ls = (queues*)malloc(sizeof(queues));
ls->front = 0;
ls->rear = 0;
push(ls,t);
while (!isempty(ls)) {
temp = pop(ls);
printf("");
printf("%c ", temp->data);
if (temp->lchild != NULL) push(ls,temp->lchild);
if (temp->rchild != NULL) push(ls, temp->rchild);
}
}
int main()
{
nodes* nodeA, * nodeB, * nodeD, * nodeF, * nodeI, * nodeL;
nodeA = (nodes*)malloc(sizeof(nodes));
nodeB = (nodes*)malloc(sizeof(nodes));
nodeD = (nodes*)malloc(sizeof(nodes));
nodeF = (nodes*)malloc(sizeof(nodes));
nodeI = (nodes*)malloc(sizeof(nodes));
nodeL = (nodes*)malloc(sizeof(nodes));
memset(nodeA, 0, sizeof(nodes));
memset(nodeB, 0, sizeof(nodes));
memset(nodeD, 0, sizeof(nodes));
memset(nodeF, 0, sizeof(nodes));
memset(nodeI, 0, sizeof(nodes));
memset(nodeL, 0, sizeof(nodes));
nodeA->data = 'A';
nodeB->data = 'B';
nodeD->data = 'D';
nodeF->data = 'F';
nodeI->data = 'I';
nodeL->data = 'L';
nodeA->lchild = nodeB;//把nodeA作为根节点
nodeA->rchild = nodeD;
nodeB->rchild = nodeF;
nodeF->lchild = nodeL;
nodeD->lchild = nodeI;
printf("广度优先遍历结果:\n");
cengcibianli(nodeA);
return 0;
}
2.二叉树-深度遍历实验代码
二叉树的深度优先遍历:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
char data;
struct node* rchild;
struct node* lchild;
}nodes;//树的节点信息
void preorder(nodes* t)//深先序遍历
{
if (t == NULL) return;
printf("%c", t->data);//先输出根节点
if (t->lchild != NULL) preorder(t->lchild);
if (t->rchild != NULL) preorder(t->rchild);
}
void inorder(nodes* t)//深度中序遍历
{
if (t == NULL) return;
if (t->lchild != NULL) inorder(t->lchild);
printf("%c", t->data);//遍历完左子树在输出根节点
if (t->rchild != NULL) inorder(t->rchild);
}
void lastorder(nodes* t) //深度后续遍历
{
if (t == NULL) return;
if (t->lchild != NULL) lastorder(t->lchild);
if (t->rchild != NULL) lastorder(t->rchild);
printf("%c", t->data);//遍历完左右子树在输出根节点
}
int main()
{
printf("0");
nodes* nodeA, * nodeB, * nodeD, * nodeF, * nodeI, * nodeL;
nodeA = (nodes*)malloc(sizeof(nodes));
nodeB = (nodes*)malloc(sizeof(nodes));
nodeD = (nodes*)malloc(sizeof(nodes));
nodeF = (nodes*)malloc(sizeof(nodes));
nodeI = (nodes*)malloc(sizeof(nodes));
nodeL = (nodes*)malloc(sizeof(nodes));
memset(nodeA, 0, sizeof(nodes));
memset(nodeB, 0, sizeof(nodes));
memset(nodeD, 0, sizeof(nodes));
memset(nodeF, 0, sizeof(nodes));
memset(nodeI, 0, sizeof(nodes));
memset(nodeL, 0, sizeof(nodes));
nodeA->data = 'A';
nodeB->data = 'B';
nodeD->data = 'D';
nodeF->data = 'F';
nodeI->data = 'I';
nodeL->data = 'L';
nodeA->lchild = nodeB;
nodeA->rchild = nodeD;
nodeB->rchild = nodeF;
nodeF->lchild = nodeL;
nodeD->lchild = nodeI;
printf("(深度)先序遍历结果:\n");
preorder(nodeA);
printf("\n(深度)中序遍历结果:\n");
inorder(nodeA);
printf("\n(深度)后序遍历结果:\n");
lastorder(nodeA);
return 0;
}