C语言基础(二十一)

devtools/2024/10/22 11:06:08/

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/devtools/104151.html

相关文章

[GESP202312 四级] 田忌赛马

题目描述 如果一个两位数是素数&#xff0c;且它的数字位置经过对换后仍为素数&#xff0c;则称为绝对素数&#xff0c;例如 1313。给定两个正整数 A,BA,B&#xff0c;请求出大于等于 AA、小于等于 BB 的所有绝对素数。 输入格式 输入 11 行&#xff0c;包含两个正整数 AA 和…

Redis中的数据类型及应用场景(面试版)

五种常用数据类型介绍 Redis中存储的都是key-value对结构的数据&#xff0c;其中key都是字符串类型&#xff0c;value有5种常用的数据类型&#xff1a; 字符串 string 哈希 hash 列表 list 集合 set 有序集合 sorted set / zset 各种数据类型特点 解释说明&#xff1a; …

求解组合优化问题的具有递归特征的无监督图神经网络

文章目录 ABSTRACT1 Introduction2 Related Work3 QRF-GNN方法4 数值实验4.1 MAX-CUTABSTRACT 介绍了一种名为QRF-GNN的新型算法,有效解决具有二次无约束二进制优化(QUBO)表述的组合问题。依赖无监督学习,从最小化的QUBO放松导出的损失函数。该架构的关键组成部分是中间GNN…

Docker Compose 部署 Kafka的KRaft模式 不用依赖 Zookeeper

前言 从 Kafka 2.8 开始&#xff0c;KRaft 模式允许 Kafka 在没有 Zookeeper 的情况下运行。本文将部署单机模式 1.新建 docker-compose.yml vim docker-compose.yml services:kafka:image: bitnami/kafka:3.6container_name: kafkaports:- "19092:9092"environm…

【机器学习】数据预处理-特征工程与特征选择

目录 一、特征工程 二、数据变换 1.变换 2.归一化 三、数据清洗 1.异常数据 2.数据清洗 四、特征选择 1.Filter过滤法 2.Wrapper包裹法 ... 3.Embedded嵌入法 ... 五、降维算法 1.SVD 2.PCA 一、特征工程 特征工程就是从原始数据提取特征的过程&#xff0c;这些…

C语言:字符串存在哪?

文章目录 0x10x11 栈上&#xff08;Stack&#xff09;0x12 堆上&#xff08;Heap&#xff09;0x13 常量存储区&#xff08;Constant&#xff09;0x20x21 常量区0x22 栈区 0x1 0x11 栈上&#xff08;Stack&#xff09; 局部变量&#xff08;包括在函数内部定义的数组和变量&am…

18044 成绩等级评分

### 思路 1. 从键盘输入一个整数成绩。 2. 判断成绩是否在0到100之间&#xff1a; - 如果不在范围内&#xff0c;输出“error”。 - 如果在范围内&#xff0c;根据成绩范围输出对应的等级&#xff1a; - 90分以上为A - 80到89分为B - 70到79分为C - …

可以根据手机的折叠状态改变播放音效:nova Flip 的妙趣音效

由于折叠机最基础的“可折叠”属性&#xff0c;导致折叠机的扬声器相对于人的位置来说会存在更多的变化&#xff0c;在不同的折叠状态下&#xff0c;听感方面可能就会大有不同。 nova Flip手机利用这一特性&#xff0c;首次根据折叠形态差异&#xff0c;自适应了不同形态的音效…