# 数据结构单向链表和循环链表的插入 | 第二套

• 插入空列表
• 在列表开头插入
• 在列表末尾插入
• 在节点之间插入

struct Node *addToEmpty(struct Node *last, int data)
{
if (last != NULL)
return last;
struct Node *temp =
(struct Node*)malloc(sizeof(struct Node));
temp -> data = data;
last = temp;
temp -> next = last;
return last;
}


1. 创建一个节点，例如 T。 \
2. 使 T -> next = last -> next。 \
3. 上一个 -> 下一个 = T。

struct Node *addBegin(struct Node *last, int data)
{
if (last == NULL)
struct Node *temp
= (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
return last;
}


1. 创建一个节点，例如 T。
2. 使 T -> next = last -> next;
3. last -> next = T。
4. last = T。

struct Node *addEnd(struct Node *last, int data)
{
if (last == NULL)
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
last = temp;
return last;
}


1. 创建一个节点，例如 T。
2. 搜索需要在其后插入 T 的节点，例如该节点是 P。
3. 使 T -> next = P -> next;
4. P -> next = T. 假设需要在节点值为10后插入12，

struct Node *addAfter(struct Node *last, int data, int item)
{
if (last == NULL)
return NULL;
struct Node *temp, *p;
p = last -> next;
do
{
if (p ->data == item)
{
temp = (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = p -> next;
p -> next = temp;
if (p == last)
last = temp;
return last;
}
p = p -> next;
} while (p != last -> next);
cout << item << " not present in the list." << endl;
return last;
}


#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
struct Node *addToEmpty(struct Node *last, int data)
{
if (last != NULL)
return last;
struct Node *temp =
(struct Node*)malloc(sizeof(struct Node));
temp -> data = data;
last = temp;
last -> next = last;
return last;
}
struct Node *addBegin(struct Node *last, int data)
{
if (last == NULL)
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
return last;
}
struct Node *addEnd(struct Node *last, int data)
{
if (last == NULL)
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
last = temp;
return last;
}
struct Node *addAfter(struct Node *last, int data, int item)
{
if (last == NULL)
return NULL;
struct Node *temp, *p;
p = last -> next;
do
{
if (p ->data == item)
{
temp = (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = p -> next;
p -> next = temp;
if (p == last)
last = temp;
return last;
}
p = p -> next;
} while(p != last -> next);
cout << item << " not present in the list." << endl;
return last;
}
void traverse(struct Node *last)
{
struct Node *p;
if (last == NULL)
{
cout << "List is empty." << endl;
return;
}
p = last -> next;
do
{
cout << p -> data << " ";
p = p -> next;
}
while(p != last->next);
}
int main()
{
struct Node *last = NULL;
traverse(last);
return 0;
}


2 4 6 8 10 12


|
4天前
【数据结构】双向带头（哨兵位）循环链表 —详细讲解（赋源码）
【数据结构】双向带头（哨兵位）循环链表 —详细讲解（赋源码）
12 4
|
24天前
【数据结构OJ题】环形链表

25 3
|
24天前
【数据结构OJ题】复制带随机指针的链表

33 1
|
24天前
【数据结构OJ题】环形链表II

13 1
|
24天前
【数据结构OJ题】相交链表

17 1
|
24天前
|

【7月更文挑战第21天】
27 2
|
1月前
【数据结构OJ题】链表分割

16 0
|
1月前
【数据结构OJ题】链表的回文结构

20 0
|
4天前
|

【数据结构】栈和队列--＞理解和实现（赋源码)
【数据结构】栈和队列--＞理解和实现（赋源码)
12 5
|
24天前
|

【数据结构】栈和队列

20 1