数据结构 C++ 代码

时间: 2023-10-19 11:29:41 浏览: 35
当然,我可以为您提供一些常见的数据结构的C代码示例。以下是几个常见的数据结构及其对应的C代码示例: 1. 数组(Array): ``` #include <stdio.h> int main() { int arr[5] = {1, 2, 3, 4, 5}; // 访问数组元素 for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } ``` 2. 链表(Linked List): ``` #include <stdio.h> #include <stdlib.h> // 定义链表结点 struct Node { int data; struct Node* next; }; // 打印链表 void printLinkedList(struct Node* head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { // 创建链表结点 struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; head = (struct Node*)malloc(sizeof(struct Node)); second = (struct Node*)malloc(sizeof(struct Node)); third = (struct Node*)malloc(sizeof(struct Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = NULL; // 打印链表 printLinkedList(head); return 0; } ``` 3. 栈(Stack): ``` #include <stdio.h> #define MAX_SIZE 100 int stack[MAX_SIZE]; int top = -1; // 入栈 void push(int item) { if (top >= MAX_SIZE - 1) { printf("Stack Overflow\n"); } else { stack[++top] = item; } } // 出栈 int pop() { if (top < 0) { printf("Stack Underflow\n"); return -1; } else { return stack[top--]; } } // 打印栈顶元素 int peek() { if (top < 0) { printf("Stack is empty\n"); return -1; } else { return stack[top]; } } // 判断栈是否为空 int isEmpty() { return top < 0; } int main() { push(1); push(2); push(3); printf("Top element: %d\n", peek()); printf("Elements: "); while (!isEmpty()) { printf("%d ", pop()); } printf("\n"); return 0; } ``` 这些是常见的数据结构的C代码示例。希望对您有所帮助!如果你需要其他数据结构或更多代码示例,请告诉我。

相关推荐

最新推荐

recommend-type

C++数据结构与算法之双缓存队列实现方法详解

主要介绍了C++数据结构与算法之双缓存队列实现方法,结合实例形式分析了双缓存队列的原理、实现方法与相关注意事项,需要的朋友可以参考下
recommend-type

C++语言数据结构 串的基本操作实例代码

主要介绍了C语言数据结构 串的基本操作实例代码的相关资料,需要的朋友可以参考下
recommend-type

数据结构所有代码C++实现

精心整理 一:线性表 1.单链表应用 例子一:尾插入创立单链表,并且删除相同元素,即表示一个集合 #include using namespace std; struct node { int num; struct node *next; }; node *head;...}
recommend-type

数据结构(c++英文版)

1)数据结构(c++英文版),Data structures using c++;里面对STL讲解的还是不错的,比普通的STL源码级的还好; 2)重点还是侧重在数据结构的描述,核心函数的代码实现也很不错。 3)很好的一本工具书。
recommend-type

北邮 数据结构第三次实验 图 实验报告

北邮信通院C++数据结构第三次实验——图 1.实验要求 2.程序分析 3.程序运行结果 4.总结 5.代码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。