C语言基础(二十)

链表是一种常见的数据结构,通常用来存储一系列元素,每个元素由一个节点来表示。在链表中,每个节点包含两部分:数据元素本身和指向下一个节点的指针。这种结构使得链表中的元素在内存中不是连续存储的,而是通过指针连接起来。链表可以分为单向链表和双向链表两种类型。在单向链表中,每个节点只有一个指向下一个节点的指针;而在双向链表中,每个节点有一个指向前一个节点的指针和一个指向后一个节点的指针。

测试代码1:

cpp 复制代码
#include "date.h"
#include <stdio.h>
#include <stdlib.h>

// 定义链表节点结构
typedef struct ListNode {
    int val;
    struct ListNode* next;
} ListNode;

// 创建新节点
ListNode* createNode(int val) {
    ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
    if (newNode == NULL) {
        printf("Memory allocation failed!n");
        exit(1);
    }
    newNode->val = val;
    newNode->next = NULL;
    return newNode;
}

// 在链表末尾添加节点
void appendNode(ListNode** head, int val) {
    ListNode* newNode = createNode(val);
    if (*head == NULL) {
        *head = newNode;
    } else {
        ListNode* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}
// 打印链表节点地址的函数  
void printListAddress(ListNode* head) {  
    ListNode* current = head;  
  
    while (current != NULL) {  
        printf("Node value: %d, Address: %p\n", current->val, (void*)current);  
        current = current->next;  
    }  
}  
  
// 遍历链表并打印每个节点的值
void printList(ListNode* head) {
    ListNode* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->val);
        temp = temp->next;
    }
    printf("NULL\n");
}

// 主函数
int main() {
	
	int time = getTime();
    ListNode* head = NULL; // 初始化链表为空

    // 向链表添加节点
    appendNode(&head, 1);
    appendNode(&head, 5);
    appendNode(&head, 0);
    appendNode(&head, 9);
    appendNode(&head, 3);
    appendNode(&head, 8);
    // 遍历并打印链表
    printList(head);
    // 打印链表节点地址
    printListAddress(head);
   
    // 释放链表内存
    ListNode* current = head;
    ListNode* next;
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
    head = NULL;  // 将头指针置为空,防止野指针访问
    
    return 0;
}

运行结果如下:

测试代码2:

cpp 复制代码
#include "date.h"
#include <stdio.h>  
#include <stdlib.h>  
  
// 定义链表节点结构体  
typedef struct ListNode {  
    int val;            // 链表节点值 
    struct ListNode* next; // 指针域,指向下一个节点  
} ListNode;  
  
// 创建新节点  
ListNode* createNode(int val) {  
    ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));  
    if (newNode == NULL) {  
        printf("Memory allocation failed!\n");  
        exit(1); // 终止程序  
    }  
    newNode->val = val;  
    newNode->next = NULL;  
    return newNode;  
}  
  
// 遍历链表并打印信息  
void printList(ListNode* head) {  
    ListNode* current = head;  
    while (current != NULL) {  
        printf("Node Address: %p, Value: %d, Next Node Address: %p\n",  
               (void*)current, current->val, (void*)(current->next));  
        current = current->next;  
    }  
}  
  
// 主函数  
int main() { 
    int time = getTime(); 
    // 创建链表节点  
    // 创建链表节点  
    ListNode* head = createNode(1); // 头结点  
    ListNode* current = head; // 用于遍历链表的指针  
  
    // 将剩余节点添加到链表中  
    current->next = createNode(2);  
    current = current->next;  
    current->next = createNode(3);  
    current = current->next;  
    current->next = createNode(4);  
    current = current->next;  
    current->next = createNode(5);  
  
  
    // 打印链表信息  
    printf("Linked List:\n");  
    printList(head);  
  
    // 释放链表内存
    ListNode* temp;  
    while (head != NULL) {  
        temp = head;  
        head = head->next;  
        free(temp);  
    }  
    head = NULL;  // 将头指针置为空,防止野指针访问
    return 0;  
}

运行结果如下:

相关推荐
福兮说8 分钟前
URL 编码的七个坑:c++ 传到后端变空格、%25 套娃、截断 emoji 直接报错
开发语言·前端·javascript·node.js·url
mmmmath_321 分钟前
LeetCode.020.有效的括号
数据结构·算法·leetcode
小的时候可菜了31 分钟前
JS 异步的本质:从单线程到 thenable(一条进化链)
开发语言·前端·javascript
可乐鸡翅yeah_39 分钟前
hls.js 配置 loadStartPosition,点播起始播放位置踩坑
开发语言·javascript·网络协议·ecmascript·m3u8在线
Doubbbbbbble云42 分钟前
算法优化中的数学建模与理论界限分析4
算法·数学建模
垆边人似月.44 分钟前
华为机考题:字符个数统计
数据结构·算法·华为
en.en..1 小时前
Linux 进程退出exit(函数)
java·开发语言·算法
Cx330❀1 小时前
【Qt 进阶指南】详解 Qt 常用标准对话框与主窗口组件架构
开发语言·qt·ui·性能优化·架构·图形渲染
Light Gao1 小时前
Encoder 与 Decoder 详解:从 Embedding 到 Seq2Seq 的完整数据流
java·开发语言·embedding
HZZD_HZZD1 小时前
弱网下CoAP还省吗?合众致达Cat.1电表90天实测:MQTT重连流量占52%、续航差距从38%拉大到73%
开发语言·物联网·php·腾讯云