C语言根本(二十一)

打印 上一主题 下一主题

主题 1044|帖子 1044|积分 3132

C语言中的链表是一种常见的数据结构,用于存储一系列的元素,但与数组不同的是,链表中的元素在内存中不是一连存储的。链表中的每个元素称为节点(Node),每个节点包罗两个部门:一部门是存储数据的数据域(Data Field),另一部门是存储指向下一个节点地址的指针域(Pointer Field)。通过这种方式,链表中的节点可以动态地增长或删除。
测试代码1:
  1. #include "date.h"
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <time.h>
  6. // 定义花的结构体。
  7. typedef struct {  
  8.     char name[50];  
  9.     float price;  
  10.     char origin[50];  
  11. }Flower;  
  12.   
  13. // 定义链表节点的结构体。  
  14. typedef struct Node{  
  15.     Flower flower;  
  16.     struct Node* next;  
  17. }Node;  
  18.   
  19. // 创建新节点。  
  20. Node* createNode(const char* name, float price, const char* origin) {  
  21.     Node* newNode = (Node*)malloc(sizeof(Node));  
  22.     if (newNode == NULL) {  
  23.         printf("Memory allocation failed!\n");  
  24.         exit(1);  
  25.     }  
  26.     strcpy(newNode->flower.name, name);  
  27.     newNode->flower.price = price;  
  28.     strcpy(newNode->flower.origin, origin);  
  29.     newNode->next = NULL;  
  30.     return newNode;  
  31. }  
  32.   
  33. // 向链表添加节点。
  34. void appendNode(Node** head, const char* name, float price, const char* origin) {  
  35.     Node* newNode = createNode(name, price, origin);  
  36.     if (*head == NULL) {  
  37.         *head = newNode;  
  38.     } else {  
  39.         Node* current = *head;  
  40.         while (current->next != NULL) {  
  41.             current = current->next;  
  42.         }  
  43.         current->next = newNode;  
  44.     }  
  45. }  
  46.   
  47. // 遍历链表并打印信息 。
  48. void traverseList(Node* head) {  
  49.     Node* current = head;  
  50.     while (current != NULL) {  
  51.         printf("Node Address: %p\n", (void*)current);  
  52.         printf("Flower Name: %s, Price: %.2f, Origin: %s\n", current->flower.name, current->flower.price, current->flower.origin);  
  53.         printf("Next Node Address: %p\n", (void*)(current->next));  
  54.         printf("\n");  
  55.         current = current->next;  
  56.     }  
  57. }  
  58.   
  59. // 释放链表内存。
  60. void freeList(Node* head) {  
  61.     Node* temp;  
  62.     while (head != NULL) {  
  63.         temp = head;  
  64.         head = head->next;  
  65.         free(temp);  
  66.     }  
  67. }  
  68. int latencyTime() {
  69.     time_t start_time, current_time;
  70.     time(&start_time);  // 获取当前时间
  71.     do {
  72.         time(&current_time);  // 再次获取当前时间
  73.     } while(difftime(current_time, start_time) < 5);  // 循环直到时间差达到5秒
  74.     return 0;
  75. }
  76.   
  77. int main() {  
  78.     int time = getTime();
  79.     Node* head = NULL;  
  80.   
  81.     // 向链表添加数据。  
  82.     // 向链表添加5种不同的花的信息。
  83.     appendNode(&head, "Rose", 5.99, "China");  
  84.     appendNode(&head, "Tulip", 3.49, "Netherlands");  
  85.     appendNode(&head, "Daisy", 2.99, "Europe");  
  86.     appendNode(&head, "Lily", 4.99, "France");  // 添加第四种花  
  87.     appendNode(&head, "Orchid", 9.99, "Southeast Asia");  // 添加第五种花  
  88.    
  89.     // 遍历链表并打印信息。  
  90.     traverseList(head);  
  91.   
  92.     // 释放链表内存 。
  93.     freeList(head);  
  94.     head = NULL; // 将头指针置为空,防止野指针访问
  95.   
  96.     return 0;  
  97. }
复制代码
运行结果如下:
 

测试代码2:
  1. #include "date.h"
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5.   
  6. // 定义链表节点结构体  
  7. typedef struct Flower {  
  8.     char name[50];  
  9.     float price;  
  10.     char origin[50];  
  11.     struct Flower* next;  
  12. } Flower;  
  13.   
  14. // 创建新节点  
  15. Flower* createNode(const char* name, float price, const char* origin) {  
  16.     Flower* newNode = (Flower*)malloc(sizeof(Flower));  
  17.     if (!newNode) {  
  18.         fprintf(stderr, "Memory allocation failed!\n");  
  19.         exit(1);  
  20.     }  
  21.     strcpy(newNode->name, name);  
  22.     newNode->price = price;  
  23.     strcpy(newNode->origin, origin);  
  24.     newNode->next = NULL;  
  25.     return newNode;  
  26. }  
  27.   
  28. // 在链表末尾插入新节点  
  29. void insertAtEnd(Flower** head, const char* name, float price, const char* origin) {  
  30.     Flower* newNode = createNode(name, price, origin);  
  31.     if (*head == NULL) {  
  32.         *head = newNode;  
  33.     } else {  
  34.         Flower* temp = *head;  
  35.         while (temp->next != NULL) {  
  36.             temp = temp->next;  
  37.         }  
  38.         temp->next = newNode;  
  39.     }  
  40. }  
  41.   
  42. // 打印链表  
  43. void printList(Flower* head) {  
  44.     Flower* temp = head;  
  45.     while (temp != NULL) {  
  46.         printf("Name: %s, Price: %.2f, Origin: %s\n", temp->name, temp->price, temp->origin);  
  47.         temp = temp->next;  
  48.     }  
  49. }  
  50.   
  51. // 主函数  
  52. int main() {
  53.     int time = getTime();
  54.     Flower* head = NULL; // 初始化链表为空  
  55.   
  56.     // 向链表中添加五种花的信息  
  57.     insertAtEnd(&head, "Rose", 5.99, "China");  
  58.     insertAtEnd(&head, "Lily", 3.50, "Netherlands");  
  59.     insertAtEnd(&head, "Tulip", 4.99, "Turkey");  
  60.     insertAtEnd(&head, "Daisy", 2.99, "USA");  
  61.     insertAtEnd(&head, "Chrysanthemum", 6.99, "Japan");  
  62.   
  63.     // 打印链表  
  64.     printList(head);  
  65.     // 释放链表内存  
  66.     Flower* temp;  
  67.     while (head != NULL) {  
  68.     temp = head;  
  69.     head = head->next;  
  70.     free(temp);  
  71. }
  72.     head = NULL; // 将头指针置为空,防止野指针访问
  73.    
  74.     return 0;  
  75. }
复制代码
运行结果如下:
 

 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

反转基因福娃

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表