实现代码
#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);
/**
* 给定一个节点,在这个节点后