在C语言中,初识单链表的头插法、尾插法、头删法、尾删法的相关操作

头文件(.h)

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

// 链表节点结构体
typedef struct Node {
    int data;
    struct Node* next;
} Node;

Node* insertHead_linList(Node* head, int value); //头插法函数声明

Node* insertTail_linList(Node* head, int value); //尾插法函数声明

Node* deleteHead_linList(Node* head); //头删法函数声明

Node* deleteTail_linList(Node* head); //尾删法函数声明

#endif

main函数部分

int main() {
    Node* head = NULL;
    
    // 头插法插入节点
    head = insertHead_linList(head, 1);
    head = insertHead_linList(head, 2);
    head = insertHead_linList(head, 3);
    
    printf("头插法插入节点后的链表: ");
    printList(head);
    
    // 头删法
    head = deleteHead_linList(head);
    printf("头删法删除节点后的链表: ");
    printList(head);
    
    // 重新初始化链表
    head = NULL;
    
    // 尾插法插入节点
    head = insertTail_linList(head, 1);
    head = insertTail_linList(head, 2);
    head = insertTail_linList(head, 3);
    
    printf("尾插法插入节点后的链表: ");
    printList(head);
    
    // 尾删法
    head = deleteTail_linList(head);
    printf("尾删法删除节点后的链表: ");
    printList(head);
    
    return 0;
}

func部分(重要部分)

// 头插法
Node* insertHead_linList(Node* head, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = head;
    return newNode;
}

// 尾插法
Node* insertTail_linList(Node* head, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    
    if (head == NULL) {
        return newNode;
    }
    
    Node* current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = newNode;
    
    return head;
}

// 头删法
Node* deleteHead_linList(Node* head) {
    if (head == NULL) {
        return NULL;
    }
    
    Node* temp = head;
    head = head->next;
    free(temp);
    
    return head;
}

// 尾删法
Node* deleteTail_linList(Node* head) {
    if (head == NULL) {
        return NULL;
    }
    
    if (head->next == NULL) {
        free(head);
        return NULL;
    }
    
    Node* current = head;
    while (current->next->next != NULL) {
        current = current->next;
    }
    
    free(current->next);
    current->next = NULL;
    
    return head;
}

// 打印链表内容
void printList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

func函数各步骤个人理解:

头插法将新节点插入到链表的头部。它先分配一个新节点,设置节点的数据为所需的值,将新节点的下一个指针指向当前的头节点,然后返回新的头节点。

尾插法将新节点插入到链表的尾部。它先分配一个新节点,设置节点的数据为所需的值,将新节点的下一个指针设置为 NULL。如果链表为空,则将新节点作为头节点返回。否则,遍历链表找到最后一个节点,然后将新节点链接到最后一个节点的后面。

头删法从链表的头部删除一个节点。如果链表为空,则直接返回 NULL。否则,将头节点保存到临时变量中,然后将头节点的下一个节点作为新的头节点,最后释放原头节点所占用的内存。

尾删法从链表的尾部删除一个节点。如果链表为空,则直接返回 NULL。如果链表只有一个节点,则删除并释放头节点,并返回 NULL。否则,遍历链表找到倒数第二个节点,将其后面的节点删除并释放,然后将倒数第二个节点的下一个指针设置为 NULL。

结果如下:

头插法插入节点后的链表: 3 -> 2 -> 1 -> NULL
头删法删除节点后的链表: 2 -> 1 -> NULL
尾插法插入节点后的链表: 1 -> 2 -> 3 -> NULL
尾删法删除节点后的链表: 1 -> 2 -> NULL
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值