数据结构单链表树图
单链表:
include <stdio.h>
include <stdlib.h>
// 定义单链表节点结构体
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
// 创建单链表节点
ListNode* createNode(int val) {
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->val = val;
newNode->next = NULL;
return newNode;
}
// 在链表头部插入节点
ListNode* insertAtHead(ListNode* head, int val) {
ListNode* newNode = createNode(val);
newNode->next = head;
return newNode;
}
// 在链表指定位置插入节点(假设位置从1开始计数)
ListNode* insertAtIndex(ListNode* head, int val, int index) {
if (index == 1) {
return insertAtHead(head, val);
}
ListNode* curr = head;
for (int i = 1; i < index - 1 && curr!= NULL; i++) {
curr = curr->next;
}
if (curr == NULL) {
printf("插入位置超出链表范围\n");
return head;
}
ListNode* newNode = createNode(val);
newNode->next = curr->next;
curr->next = newNode;
return head;
}
// 删除链表中指定值的节点
ListNode* deleteNode(ListNode* head, int val) {
if (head == NULL) {
return NULL;
}
if (head->val == val) {
ListNode* temp = head;
head = head->next;
free(temp);
return head;
}
ListNode* curr = head;
while (curr->next!= NULL && curr->next->val!= val) {
curr = curr->next;
}
if (curr->next!= NULL) {
ListNode* temp = curr->next;
curr->next = curr->next->next;
free(temp);
}
return head;
}
// 遍历单链表并打印节点值
void printList(ListNode* head) {
ListNode* curr = head;
while (curr!= NULL) {
printf("%d ", curr->val);
curr = curr->next;
}
printf("\n");
}
// 释放单链表内存
void freeList(ListNode* head) {
ListNode* curr = head;
while (curr!= NULL) {
ListNode* next = curr->next;
free(curr);
curr = next;
}
}
int main() {
ListNode* head = NULL;
head = insertAtHead(head, 3);
head = insertAtHead(head, 2);
head = insertAtHead(head, 1);
printList(head);
head = insertAtIndex(head, 4, 2);
printList(head);
head = deleteNode(head, 3);
printList(head);
freeList(head);
return 0;
}
二叉树:
include <stdio.h>
include <stdlib.h>
// 二叉树节点结构体
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 创建二叉树节点
TreeNode* createNode(int val) {
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
node->val = val;
node->left = NULL;
node->right = NULL;
return node;
}
// 创建二叉树
TreeNode* createBinaryTree() {
TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
return root;
}
// 前序遍历
void preorderTraversal(TreeNode* root) {
if (root) {
printf("%d ", root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// 中序遍历
void inorderTraversal(TreeNode* root) {
if (root) {
inorderTraversal(root->left);
printf("%d ", root->val);
inorderTraversal(root->right);
}
}
// 后序遍历
void postorderTraversal(TreeNode* root) {
if (root) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->val);
}
}
int main() {
TreeNode* root = createBinaryTree();
printf("前序遍历: ");
preorderTraversal(root);
printf("\n");
printf("中序遍历: ");
inorderTraversal(root);
printf("\n");
printf("后序遍历: ");
postorderTraversal(root);
printf("\n");
return 0;
}
图:
include <stdio.h>
include <stdlib.h>
define MAX_VERTICES 100
// 图结构体,使用邻接矩阵表示
typedef struct Graph {
int vertices;
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
} Graph;
// 初始化图
Graph* createGraph(int vertices) {
Graph* graph = (Graph *)malloc(sizeof(Graph));
graph->vertices = vertices;
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
graph->adjMatrix[i][j] = 0;
}
}
return graph;
}
// 添加边
void addEdge(Graph* graph, int src, int dest) {
graph->adjMatrix[src][dest] = 1;
graph->adjMatrix[dest][src] = 1;
}
// 打印图的邻接矩阵
void printGraph(Graph* graph) {
for (int i = 0; i < graph->vertices; i++) {
for (int j = 0; j < graph->vertices; j++) {
printf("%d ", graph->adjMatrix[i][j]);
}
printf("\n");
}
}
// 深度优先搜索辅助函数,用于标记已访问的顶点
void DFSUtil(Graph* graph, int vertex, int visited[]) {
visited[vertex] = 1;
printf("%d ", vertex);
for (int i = 0; i < graph->vertices; i++) {
if (graph->adjMatrix[vertex][i] == 1 && visited[i] == 0) {
DFSUtil(graph, i, visited);
}
}
}
// 深度优先搜索
void DFS(Graph* graph, int startVertex) {
int visited[MAX_VERTICES] = {0};
DFSUtil(graph, startVertex, visited);
}
// 广度优先搜索辅助函数,使用队列来实现
void BFSUtil(Graph* graph, int startVertex, int visited[]) {
int queue[MAX_VERTICES];
int front = 0, rear = 0;
visited[startVertex] = 1;
queue[rear++] = startVertex;
while (front < rear) {
int currentVertex = queue[front++];
printf("%d ", currentVertex);
for (int i = 0; i < graph->vertices; i++) {
if (graph->adjMatrix[currentVertex][i] == 1 && visited[i] == 0) {
visited[i] = 1;
queue[rear++] = i;
}
}
}
}
// 广度优先搜索
void BFS(Graph* graph, int startVertex) {
int visited[MAX_VERTICES] = {0};
BFSUtil(graph, startVertex, visited);
}
int main() {
Graph* graph = createGraph(5);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
printf("邻接矩阵表示的图:\n");
printGraph(graph);
printf("深度优先搜索(从顶点0开始):\n");
DFS(graph, 0);
printf("\n");
printf("广度优先搜索(从顶点0开始):\n");
BFS(graph, 0);
printf("\n");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架