当前位置: 首页 > news >正文

C语言数据结构

C 语言是一种强大的编程语言,它提供了许多数据结构的实现。在本文档中,我们将讨论一些常见的数据结构,并提供相应的代码示例。

  1. 数组(Array)

数组是一种线性数据结构,它可以存储相同类型的元素。数组的大小在创建时被确定,并且可以通过索引访问和修改元素。

#include <stdio.h>int main() {int arr[5] = {1, 2, 3, 4, 5};// 访问数组元素printf("第一个元素: %d\n", arr[0]);// 修改数组元素arr[0] = 10;printf("修改后的第一个元素: %d\n", arr[0]);return 0;
}
  1. 链表(Linked List)

链表是一种动态数据结构,它由节点(Node)组成,每个节点包含一个元素和一个指向下一个节点的指针。

#include <stdio.h>
#include <stdlib.h>// 链表节点
typedef struct Node {int data;struct Node* next;
} Node;int main() {// 创建链表Node* head = NULL;Node* second = NULL;Node* third = NULL;head = (Node*)malloc(sizeof(Node));second = (Node*)malloc(sizeof(Node));third = (Node*)malloc(sizeof(Node));head->data = 1;head->next = second;second->data = 2;second->next = third;third->data = 3;third->next = NULL;// 遍历链表Node* current = head;while (current != NULL) {printf("%d ", current->data);current = current->next;}return 0;
}
  1. 栈(Stack)

栈是一种后进先出(LIFO)的数据结构。它只允许在栈顶进行插入和删除操作。

#include <stdio.h>
#define MAX_SIZE 100// 栈结构
typedef struct Stack {int arr[MAX_SIZE];int top;
} Stack;// 初始化栈
void init(Stack* stack) {stack->top = -1;
}// 判断栈是否为空
int isEmpty(Stack* stack) {return stack->top == -1;
}// 判断栈是否已满
int isFull(Stack* stack) {return stack->top == MAX_SIZE - 1;
}// 入栈
void push(Stack* stack, int data) {if (isFull(stack)) {printf("栈已满\n");return;}stack->arr[++stack->top] = data;
}// 出栈
int pop(Stack* stack) {if (isEmpty(stack)) {printf("栈为空\n");return -1;}return stack->arr[stack->top--];
}int main() {Stack stack;init(&stack);// 入栈push(&stack, 1);push(&stack, 2);push(&stack, 3);// 出栈printf("%d\n", pop(&stack));printf("%d\n", pop(&stack));printf("%d\n", pop(&stack));return 0;
}
  1. 队列(Queue)

队列是一种先进先出(FIFO)的数据结构。可以在队尾插入元素,在队头删除元素。

#include <stdio.h>
#define MAX_SIZE 100// 队列结构
typedef struct Queue {int arr[MAX_SIZE];int front;int rear;
} Queue;// 初始化队列
void init(Queue* queue) {queue->front = -1;queue->rear = -1;
}// 判断队列是否为空
int isEmpty(Queue* queue) {return queue->front == -1;
}// 判断队列是否已满
int isFull(Queue* queue) {return (queue->rear + 1) % MAX_SIZE == queue->front;
}// 入队
void enqueue(Queue* queue, int data) {if (isFull(queue)) {printf("队列已满\n");
http://www.lryc.cn/news/266043.html

相关文章:

  • PHP之Trait理解, Trait介绍
  • SpringMVC:执行原理详解、配置文件和注解开发实现 SpringMVC
  • 增量式旋转编码器在STM32平台上的应用
  • INFINI Gateway 如何防止大跨度查询
  • 【模式识别】探秘分类奥秘:最近邻算法解密与实战
  • 【Redis】分布式锁
  • Linux访问firefox 显示Error: no DISPLAY environment variable specified
  • 线性回归简介
  • Log4net 教程
  • test-01-java 单元测试框架 junit 入门介绍
  • Linux系统中跟TCP相关的系统配置项
  • python图片批量下载多线程+超时重试
  • 冒泡排序之C++实现
  • 【Spring实战】04 Lombok集成及常用注解
  • ubuntu-22.04.3 配置
  • [工具]java_sublime的快速使用
  • 【银行测试】银行金融测试+金融项目测试点汇总...
  • 将PPT的图保持高分辨率导入到Word / WPS中
  • 如何在Spring Boot中优雅地进行参数校验
  • 图还能有数据库?一文带你了解图数据库是个什么东西!
  • 力扣思维题——寻找重复数
  • 基于Kubernetes的jenkins上线
  • 每日一题——轮转数组
  • Unity手机移动设备重力感应
  • nodejs微信小程序+python+PHP基于推荐算法的电影推荐系统-计算机毕业设计推荐django
  • Linux 配置 swap 区
  • AG16KDDF256 User Manual
  • w15初识php基础
  • powerbuilder Primary! Delete! Filter! 三个缓冲区的作用
  • Confluent 与阿里云将携手拓展亚太市场,提供消息流平台服务