• 单链表
• 头插法
• 尾插法
• C语言 实现

# 单链表

## 头插法

1. 假设链表中已经有一个节点:
2. 生成一个新的节点
新节点的插入分为两步:
注意！操作顺序不要错！
3. 插入完成

## 尾插法

1. 假设当前有一个节点存在：
2. 创建一个新节点:
1. 首先将新节点的next指向NULL
2. 接下来将前一个节点的next指向新节点
3. 插入完成

# C语言 实现

``````//
//  Data_structure
//
//  Created by 真的木有鱼丸啊 on 2020/05/23.
//

#include
#include
#include

typedef struct node
{
int data;
struct node* next;
}List;

{
temp->next = NULL;
temp->data = -1;
}

{
}

{
{
printf("List is NULL\n");
assert(0);
}
else
{
List* new_node = (List*)malloc(sizeof(List));

new_node->data = num;
new_node->next = temp->next;
temp->next = new_node;

}
}

{
{
printf("List is NULL\n");
assert(0);
}
else
{
while(temp->next!=NULL)
{
temp = temp->next;
}

List* new_node = (List*)malloc(sizeof(List));
new_node->data = num;
new_node->next = NULL;
temp->next = new_node;
}
}
{
{
printf("List is NULL\n");
assert(0);
}
else{
while(finder != NULL)
{
if(finder->data == num)
{
slow->next = finder->next;
free(finder);
break;
}
slow = slow->next;
finder = finder ->next;
}
}
return 0;//没找到指定元素
}

{
{
printf("List is NULL\n");
assert(0);
}
else
{
List* fast = slow->next;

while(slow->next != NULL)
{
List* finder = fast;
List* slower = slow;
while(finder != NULL)
{
if(slow->data == finder->data)
{
slower->next = finder->next;
free(finder);
break;
}
else
{
finder = finder->next;
slower = slower->next;
}
}
slow = slow->next;
fast = fast->next;
}
}

return 0;
}
{
while(temp!=NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

{
int count = 0;
while(temp!=NULL)
{
count++;
temp = temp->next;
}
return count;
}
int main()
{
List * l;
l = (List*)malloc(sizeof(List));

list_init(l);

for(int i = 0; i < 5; i++)
{
insert_one_by_one(l,i);
}
print_list(l);

delete_element(l, 0);

print_list(l);
printf("the size of list : %d \n", get_list_length(l));

insert_one_by_one(l, 3);
print_list(l);

delete_same_element(l);
print_list(l);

for(int i = 0; i < 5; i++)
{
insert_element(l,i);
}
print_list(l);

return 0;

}

``````