单链表的创建(有头结点和无头结点)

前言

在学链表的时候,对链表创建的过程一知半解。目前现在刷题的阶段,发现这部分很重要,所以这次完全解决这个知识点。

 

1 带头结点的链表

为了方便,创建带有10个结点的链表,链表的数据域为整数类型,取随机整数。链表结构如下图:

 

 1.1 头插法

头插法的思想如下图:

伪代码实现:

(1)创建一个头结点,ListNode *head = new ListNode(10) ;    //头结点数据域保存结点的个数

                                      head -> next = nullptr;

(2)插入结点1,LIstNode *s = new ListNode(rand());    // 创建结点1

         s - > next = head -> next;                                        

         head -> next = s;                       

(3)插入结点2,LIstNode *s = new ListNode(rand());    // 创建结点2

         s - > next = head -> next;     // 这里 head - > next 其实就是结点1                                    

         head -> next = s;                 

(4)重复上述流程,创建完成

 

1.2 头插法代码实现 

struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr){
    }
}


// 头插法
ListNode* CreateList(int length){
    if (length < 1)
        return nullptr;
    ListNode *head = new ListNode(length);
    head -> next = nullptr;
    int k = 1;
    ListNode *s = nullptr;
    srand(unsigned(time(0)));
    while (k <= length){
        s = new ListNode(rand());
        s -> next = head -> next;
        head -> next = s;
        k++;
    }
    return head;
}

 

1.3 尾插法

尾插法的思想如下图:

伪代码实现:

(1)创建一个头结点,ListNode *head = new ListNode(10) ;    //头结点数据域保存结点的个数

                                      ListNode *s = head;                       

(2)插入结点1,LIstNode *r = new ListNode(rand());    // 创建结点1

         s -> next = r;                                      

         s = r;                     

(3)插入结点2,LIstNode *r = new ListNode(rand());    // 创建结点2

         s - > next = r;                                

         s = r;         

(4)重复上述流程,最后时,让s -> next = nullptr,返回head。

 

1.4 尾插法代码实现

struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr){
    }
}

// 尾差法
ListNode* CreateList(int length){
    if (length < 1)
        return nullptr;
    ListNode *head = new ListNode(length);
    ListNode *s = head;
    int k = 1;
    ListNode *r = nullptr;
    srand(unsigned(time(0)));
    while (k <= length){
        r = new ListNode(rand());
        s -> next = r;
        s = r;
        k++;
    }
    s -> next = nullptr;
    return head;
}

 

2 无头结点的链表

为了方便,创建带有10个结点的链表,链表的数据域为整数类型,取随机整数。链表结构如下图:

 

2.1 头插法

头插法思路如下:

 

伪代码实现:

(1)创建结点1,ListNode *head = new ListNode(10) ;    //头结点数据域保存结点的个数

                                      head -> next = nullptr;

                                      ListNode *s = nullptr;                       

(2)插入结点2,LIstNode *s = new ListNode(rand());    // 创建结点1

         s -> next = head;                                      

         head = s;             

(3)插入结点3,LIstNode *s = new ListNode(rand());    // 创建结点2

         s -> next = head;                                      

         head = s;   

(4)重复上述流程,创建完成

 

2.2 头插法代码实现

struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr){
    }
}

// 头插法
ListNode* CreateList(int length){
    if (length < 1)
        return nullptr;
    srand(unsigned(time(0)));
    ListNode *head = new ListNode(rand());
    head -> next = nullptr;
    ListNode *s = nullptr;
    int k = 1;
    while (k <= length - 1){
        s = new ListNode(rand());
        s -> next = head;
        head = s;
        k++
    }
    return head;
}

 

2.3 尾插法

尾插法思路如下:

伪代码实现:

(1)创建结点1,ListNode *head = new ListNode(10) ;    //头结点数据域保存结点的个数

                             ListNode *s = head;                     

(2)插入结点2,LIstNode *r = new ListNode(rand());    // 创建结点1

         s -> next = r;                                      

         s = r;             

(3)插入结点3,LIstNode *r = new ListNode(rand());    // 创建结点2

         s -> next = r;                                      

         s = r;     

(4)重复上述流程,最后时,让s -> next = nullptr,返回head。

 

2.4 尾插法代码实现

struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr){
    }
}

// 头插法
ListNode* CreateList(int length){
    if (length < 1)
        return nullptr;

    srand(unsigned(time(0)));
    ListNode *head = new ListNode(rand()); 
    ListNode *s = head, *r = nullptr;
    int k = 1;
    while (k <= length - 1){
        r = new ListNode(rand());
        s -> next = r;
        s = r;
        k++
    }
    s -> next = nullptr;
    return head;
}


// 将头结点放在循环里面
ListNode* CreateList(int length){
    if (length < 1)
        return nullptr;

    srand(unsigned(time(0)));
    ListNode *head = nullptr, *s = nullptr, *r = nullptr;
    int k = 1;
    while (k <= length){
        r = new ListNode(rand());
        if (head == nullptr)
            head = r;
        else
            s -> next = r;
        s = r;
        k++
    }
    s -> next = nullptr;
    return head;
}

 

### 创建单链表头结点的实现 在单链表中,创建头结点的操作可以通过分配内存并初始化其指针域来完成。以下是具体的代码实现以及解释。 #### 带头结点单链表头结点创建 对于带头结点单链表,通常会先定义一个结构体表示链表中的节点,然后通过动态分配内存的方式创建头结点,并将其指针域设置为 `NULL` 表示当前链表为空[^1]。 ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构 typedef struct Node { int data; // 节点存储的数据 struct Node* next; // 指向下一个节点的指针 } LinkNode; // 定义单链表类型 typedef struct { LinkNode* head; // 指向头结点的指针 } LinkList; // 创建带空头结点单链表 bool CreateHeadNode(LinkList& L) { L.head = (LinkNode*)malloc(sizeof(LinkNode)); // 动态分配头结点空间 if (L.head == NULL) { // 判断内存分配是否成功 return false; } L.head->next = NULL; // 初始化头结点的指针域为NULL return true; } void test() { LinkList L; if (CreateHeadNode(L)) { printf("头结点创建成功\n"); } else { printf("头结点创建失败\n"); } } ``` 上述代码实现了带头结点单链表头结点创建过程。其中,`CreateHeadNode` 函数负责动态分配内存给头结点,并将它的 `next` 字段置为 `NULL`,表明此时链表为空[^2]。 --- ### 不带头结点单链表头结点创建 如果不使用头结点,则可以直接将整个链表视为由一系列数据节点组成,初始状态下链表为空时可以用 `L = NULL` 来表示。 ```c // 不带头结点单链表初始化函数 bool InitListWithoutHeader(LinkList*& L) { L = NULL; // 链表为空时,直接令L等于NULL即可 return true; } void test_without_header() { LinkList* L; if (InitListWithoutHeader(L)) { printf("不带头结点单链表初始化成功\n"); } else { printf("不带头结点单链表初始化失败\n"); } } ``` 在这种情况下,不需要显式地创建头结点,而是直接让链表指针指向 `NULL`,以此标志该链表目前为空。 --- ### 总结 无论是带头结点还是不带头结点单链表,在实际应用中都需要根据具体需求选择合适的实现方式。如果希望简化某些操作逻辑(如统一处理首元节点其他中间节点),则推荐采用带头结点的形式。 ---
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值