02 链表的插入实现:头插、尾插、指定位置插入(Linked List 链表)

本文介绍了如何实现链表的插入操作,包括在头部插入、尾部插入以及指定位置插入的方法,详细讲解了相关代码实现过程。
摘要由CSDN通过智能技术生成

实现代码

#include <stdio.h>
#include <stdlib.h>

/*
 * 定义一个链表节点
 */
typedef struct LinkedNode {
    int data;
    struct LinkedNode *next;
} LinkedNode;

/*头插法和尾插法,均是传入头指针,然后进行操作的,所以在入参的时候,用到的是双指针*/
/*
 * 头插法
 * Following are the 4 steps to add node at the front.
 * Given a reference (pointer to pointer) to the head of a list
 * and an int,  inserts a new node on the front of the list.
 * 注意这里传的参数是指针的指针
 * Time complexity of push() is O(1) as it does constant amount of work.
 */
void push(LinkedNode **head_ref, int new_data
);

/*
 * 尾插法
 * Add a node at the end: (6 steps process)
 * Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.
 * Given a reference (pointer to pointer) to the head
 * of a list and an int, appends a new node at the end
 */
void append(LinkedNode **head_ref, int new_data);

/**
 * 给定一个节点,在这个节点后
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值