C语言基础(二十一)

news/2024/9/23 10:20:27/

C语言中的链表是一种常见的数据结构,用于存储一系列的元素,但与数组不同的是,链表中的元素在内存中不是连续存储的。链表中的每个元素称为节点(Node),每个节点包含两个部分:一部分是存储数据的数据域(Data Field),另一部分是存储指向下一个节点地址的指针域(Pointer Field)。通过这种方式,链表中的节点可以动态地增加或删除。

测试代码1:

#include "date.h"
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <time.h>
// 定义花的结构体。 
typedef struct {  char name[50];  float price;  char origin[50];  
}Flower;  // 定义链表节点的结构体。  
typedef struct Node{  Flower flower;  struct Node* next;  
}Node;  // 创建新节点。  
Node* createNode(const char* name, float price, const char* origin) {  Node* newNode = (Node*)malloc(sizeof(Node));  if (newNode == NULL) {  printf("Memory allocation failed!\n");  exit(1);  }  strcpy(newNode->flower.name, name);  newNode->flower.price = price;  strcpy(newNode->flower.origin, origin);  newNode->next = NULL;  return newNode;  
}  // 向链表添加节点。 
void appendNode(Node** head, const char* name, float price, const char* origin) {  Node* newNode = createNode(name, price, origin);  if (*head == NULL) {  *head = newNode;  } else {  Node* current = *head;  while (current->next != NULL) {  current = current->next;  }  current->next = newNode;  }  
}  // 遍历链表并打印信息 。 
void traverseList(Node* head) {  Node* current = head;  while (current != NULL) {  printf("Node Address: %p\n", (void*)current);  printf("Flower Name: %s, Price: %.2f, Origin: %s\n", current->flower.name, current->flower.price, current->flower.origin);  printf("Next Node Address: %p\n", (void*)(current->next));  printf("\n");  current = current->next;  }  
}  // 释放链表内存。 
void freeList(Node* head) {  Node* temp;  while (head != NULL) {  temp = head;  head = head->next;  free(temp);  }  
}  int latencyTime() {time_t start_time, current_time;time(&start_time);  // 获取当前时间do {time(&current_time);  // 再次获取当前时间} while(difftime(current_time, start_time) < 5);  // 循环直到时间差达到5秒return 0;
}int main() {  int time = getTime();Node* head = NULL;  // 向链表添加数据。  // 向链表添加5种不同的花的信息。 appendNode(&head, "Rose", 5.99, "China");  appendNode(&head, "Tulip", 3.49, "Netherlands");  appendNode(&head, "Daisy", 2.99, "Europe");  appendNode(&head, "Lily", 4.99, "France");  // 添加第四种花  appendNode(&head, "Orchid", 9.99, "Southeast Asia");  // 添加第五种花  // 遍历链表并打印信息。  traverseList(head);  // 释放链表内存 。 freeList(head);  head = NULL; // 将头指针置为空,防止野指针访问 return 0;  
}

运行结果如下:

 

测试代码2:

#include "date.h"
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  // 定义链表节点结构体  
typedef struct Flower {  char name[50];  float price;  char origin[50];  struct Flower* next;  
} Flower;  // 创建新节点  
Flower* createNode(const char* name, float price, const char* origin) {  Flower* newNode = (Flower*)malloc(sizeof(Flower));  if (!newNode) {  fprintf(stderr, "Memory allocation failed!\n");  exit(1);  }  strcpy(newNode->name, name);  newNode->price = price;  strcpy(newNode->origin, origin);  newNode->next = NULL;  return newNode;  
}  // 在链表末尾插入新节点  
void insertAtEnd(Flower** head, const char* name, float price, const char* origin) {  Flower* newNode = createNode(name, price, origin);  if (*head == NULL) {  *head = newNode;  } else {  Flower* temp = *head;  while (temp->next != NULL) {  temp = temp->next;  }  temp->next = newNode;  }  
}  // 打印链表  
void printList(Flower* head) {  Flower* temp = head;  while (temp != NULL) {  printf("Name: %s, Price: %.2f, Origin: %s\n", temp->name, temp->price, temp->origin);  temp = temp->next;  }  
}  // 主函数  
int main() { int time = getTime(); Flower* head = NULL; // 初始化链表为空  // 向链表中添加五种花的信息  insertAtEnd(&head, "Rose", 5.99, "China");  insertAtEnd(&head, "Lily", 3.50, "Netherlands");  insertAtEnd(&head, "Tulip", 4.99, "Turkey");  insertAtEnd(&head, "Daisy", 2.99, "USA");  insertAtEnd(&head, "Chrysanthemum", 6.99, "Japan");  // 打印链表  printList(head);  // 释放链表内存  Flower* temp;  while (head != NULL) {  temp = head;  head = head->next;  free(temp);  
}head = NULL; // 将头指针置为空,防止野指针访问 return 0;  
}

运行结果如下:

 

 


http://www.ppmy.cn/news/1518980.html

相关文章

【大数据算法】时间亚线性算法之:串相等判定算法。

串相等判定算法 1、引言2、串相等判定算法2.1 定义2.2 核心原理2.3 应用场景2.4 算法公式2.4.1 Rabin-Karp算法2.4.2 哈希函数 2.5 代码示例 3、总结 1、引言 小屌丝&#xff1a;鱼哥&#xff0c; 啥是串相等判定算法啊 小鱼&#xff1a;这个… en…en… 小屌丝&#xff1a;咋…

asio之带缓冲区的流

简介 主要用于流式套接字 组成 #mermaid-svg-hX01koNjzbKEtBaq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-hX01koNjzbKEtBaq .error-icon{fill:#552222;}#mermaid-svg-hX01koNjzbKEtBaq .error-text{fill:#55…

windows 10安装GPU版本pytorch

一、下载Anaconda 1.由于anaconda的服务器都在国外&#xff0c;推荐大家使用镜像源进行下载&#xff0c;清华的conda镜像链接&#xff1a;​​​​​​ anaconda | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirrora 2.使用命令新建一个虚拟环境&#…

海康二次开发学习笔记12-从Group外部输入图像

从Group外部输入图像 用OpenCV从本地读图 当Group内部无图像源模块时,可以通过代码的方式将图片传入Group内部.实现方式有多种,可以使用OpenCV从本地读图,可在程序集搜索引用OpenCvSharp&#xff0c;同时将其复制本地的属性改为False. 1. 界面设计 增加加载图像按钮 2. 处理…

[排序和二分] 绝对差值和

给你两个正整数数组 nums1 和 nums2 &#xff0c;数组的长度都是 n 。 数组 nums1 和 nums2 的 绝对差值和 定义为所有 |nums1[i] - nums2[i]|&#xff08;0 < i < n&#xff09;的 总和&#xff08;下标从 0 开始&#xff09;。 你可以选用 nums1 中的 任意一个 元素来…

PKI+简单渗透测试随记

PKI 一、 PKI概述 1、名称 Public Key Infrastructure&#xff1a;公钥基础设施 2、作用 通过加密技术和数字签名保证信息的安全 3、组成 公钥加密技术、数字证书、CA、RA 二、信息安全三要素 机密性 完整性 身份验证/操作的不可否认性 三、哪些IT领域用到PKI&#xff1a; …

模拟登录页,华为账号一键登录

一、介绍 基于鸿蒙Next模拟账号一键登录&#xff0c;免去账号注册环节二、场景需求 1. 用户场景 新用户&#xff1a; 需要快速注册并登录&#xff0c;以体验华为的服务。 老用户&#xff1a; 希望快速登录&#xff0c;不用每次输入用户名和密码。 2. 界面设计 Logo和标题&#…

科研绘图系列:R语言PCoA图(PCoA plot)

介绍 PCoA(主坐标分析,Principal Coordinate Analysis)是一种多维数据的降维技术,它用于探索高维空间中样本之间的关系。PCoA通常用于生态学、遗传学和其他领域的数据分析,以揭示样本或个体之间的相似性或差异性。 PCoA图的作用: 数据降维:PCoA可以将高维数据(如物种…