C语言基础(二十一)

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

测试代码1:

cpp 复制代码
#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:

cpp 复制代码
#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;  
}

运行结果如下:

相关推荐
今日待办1 小时前
Arduino Nano33 BLESense Rev2【室内空气质量检测语音识别蓝牙调光台灯】
c语言·单片机·嵌入式硬件·mcu·语音识别·ardunio·arduinonano33
望未来无悔1 小时前
系统学习算法 专题十八 队列+宽搜
java·算法
Dovis(誓平步青云)1 小时前
《探索C++11:现代语法的性能优化策略(中篇)》
开发语言·c++
xz2024102****1 小时前
最大似然估计:损失函数的底层数学原理
人工智能·算法·机器学习·概率论
再努力"亿"点点1 小时前
爬取m3u8视频完整教程
开发语言·python
一个响当当的名号1 小时前
c++primer 个人学习总结-模板和泛型编程
开发语言·c++·学习
落羽的落羽1 小时前
【C++】C++11的可变参数模板、emplace接口、类的新功能
开发语言·c++·学习
滴滴滴嘟嘟嘟.1 小时前
Qt对话框与文件操作学习
开发语言·qt·学习
Xの哲學1 小时前
Linux RCU (Read-Copy-Update) 机制深度分析
linux·网络·算法·架构·边缘计算
数模加油站2 小时前
25高教社杯数模国赛【C题国一学长思路+问题分析】第二弹
算法·数学建模·数模国赛·高教社杯全国大学生数学建模竞赛