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;  
}

运行结果如下:

相关推荐
ShineWinsu2 小时前
对于Qt:QTabWidget常见属性的解析
开发语言·数据库·c++·qt·浏览器·qtabwidget·标签页
Pniubi2 小时前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表
普通人62 小时前
WebGL与WebGPU入门指南
开发语言·webgl·1024程序员节
꧁细听勿语情꧂3 小时前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法
SomeOtherTime3 小时前
Rust集合(AI回答)
开发语言·后端·rust
曦樂~3 小时前
【Web Development】03JavaScript 基础和事件
开发语言·前端·javascript
郝学胜-神的一滴3 小时前
LangChain Skill 实现(渐进披露 On-Demand Skill,LangChain 官方 Deep Agent Skill 模式)
开发语言·python·langchain·numpy
俊俊几年的3 小时前
动态规划(Dynamic Programming)详解:从入门到 LeetCode Hot 100
linux·c++·算法·leetcode·嵌入式·动态规划·c
程序员Query4 小时前
day01-认识Go语言 + golang环境的安装
开发语言·后端·golang
小雉系统12 小时前
BcoreOS安全通信架构:FsSession 抗算法后门安全设计
算法·安全·架构