C语言链表创建详解

链表简介

链表是一种常见的数据结构,它是由一系列节点组成,每个节点包含两部分:数据域和指针域,数据域用于存储数据,指针域用于存储下一个节点的地址,链表的第一个节点称为头节点,最后一个节点的指针域为空,链表的一个重要特点是插入和删除操作非常灵活,不需要像数组那样预先分配固定大小的空间。

链表的类型

1、单向链表:每个节点只有一个指针域,指向下一个节点。

2、双向链表:每个节点有两个指针域,分别指向前一个节点和后一个节点。

3、循环链表:最后一个节点的指针域指向头节点,形成一个环。

4、静态链表:在编译时就确定链表的大小,不能动态分配空间。

5、动态链表:在运行时可以动态分配空间,增加或减少节点。

链表的基本操作

1、创建链表:根据实际需求定义链表的结构体,然后通过malloc或calloc函数为节点分配内存空间。

2、添加节点:在链表的指定位置插入一个新节点,需要修改前后节点的指针域。

3、删除节点:删除链表中的某个节点,需要修改前后节点的指针域。

4、查找节点:遍历链表,找到指定数据的节点。

5、排序链表:对链表中的数据进行排序,可以使用冒泡排序、选择排序等算法。

6、打印链表:遍历链表,输出每个节点的数据。

C语言实现单向链表创建

1、定义链表结构体:

typedef struct Node {
    int data; // 数据域
    struct Node *next; // 指针域,指向下一个节点
} Node;

2、创建新节点:

Node *createNode(int data) {
    Node *newNode = (Node *)malloc(sizeof(Node)); // 为新节点分配内存空间
    newNode->data = data; // 设置新节点的数据域
    newNode->next = NULL; // 新节点的指针域初始化为空
    return newNode; // 返回新节点的地址
}

c语言链表创建 c语言链表创建新节点

3、向链表中添加节点:

void addNode(Node **head, int data) {
    Node *newNode = createNode(data); // 创建新节点
    if (*head == NULL) { // 如果链表为空,将新节点设置为头节点
        *head = newNode;
    } else { // 如果链表不为空,找到最后一个节点,将其指针域指向新节点
        Node *temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

4、示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
    int data; // 数据域
    struct Node *next; // 指针域,指向下一个节点
} Node;
Node *createNode(int data) {
    Node *newNode = (Node *)malloc(sizeof(Node)); // 为新节点分配内存空间
    newNode->data = data; // 设置新节点的数据域
    newNode->next = NULL; // 新节点的指针域初始化为空
    return newNode; // 返回新节点的地址
}
void addNode(Node **head, int data) {
    Node *newNode = createNode(data); // 创建新节点
    if (*head == NULL) { // 如果链表为空,将新节点设置为头节点
        *head = newNode;
    } else { // 如果链表不为空,找到最后一个节点,将其指针域指向新节点
        Node *temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}
void printList(Node *head) { // 打印链表的函数,暂不实现递归打印功能
    Node *temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data); // 输出当前节点的数据域的值,并移动到下一个节点的位置
        temp = temp->next; // 更新当前节点为下一个节点的位置,继续遍历链表直到结束或遇到NULL指针为止(即到达了尾结点)																																		//printList(temp->next);








// }






// printList(head);



// return 0;


// }
// printList(&head);
// return 0;
// }
// printList(&head);
// return 0;
// }
// printList(&head);
// return 0;
// }
// printList(&head);
// return 0;
// }
// printList(&head);
// return 0;
// }// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;}// printList(&head); return 0;]*/