# 链表遍历，链表查找和统计节点，链表插入新节点，链表删除节点，链表修改指定节点,链表头插法，尾插法总结

## 1.链表静态增加和动态遍历

#include <stdio.h>

struct Test
{
int data;
struct Test *next;
};

{
}
}

int main()
{
struct Test p1 = {11,NULL};
struct Test p2 = {22,NULL};
struct Test p3 = {33,NULL};
struct Test p4 = {44,NULL};

p1.next = &p2;
p2.next = &p3;
p3.next = &p4;

return 0;
}


## 2.统计链表节点各数及链表查找

#include <stdio.h>

struct Test
{
int data;
struct Test *next;
};

{
}
}

{
int cnt;
cnt++;
}
return cnt;
}

{
return 1;
}
}        return 0;
}

int main()
{
struct Test p1 = {11,NULL};
struct Test p2 = {22,NULL};
struct Test p3 = {33,NULL};
struct Test p4 = {44,NULL};

p1.next = &p2;
p2.next = &p3;
p3.next = &p4;

if(ret2 == 1){
printf("hava newdata\n");
}
else{
printf("no newdata\n");
}

return 0;
}


## 3.链表从指定节点后方插入新节点

struct Test* insertToBehind(struct Test *head, int data, struct Test *new)
{

while (p != NULL) {
if ( data == p->data) {
new->next = p->next;
p->next = new;
}
p = p->next;
}
}


## 4.链表从指定节点前方插入新节点


struct Test* insertToFront(struct Test *head, int data, struct Test *new)
{

if (data == p->data) {
printf("insert success\n");
return new;
}

while (p->next != NULL) {
if (data == p->next->data) {
new->next = p->next;
p->next = new;
printf("insert success\n");
}
p = p->next;
}
}


## 5.链表删除指定节点

include <stdio.h>

struct Test
{
int data;
struct Test *next;
};

{
}
}

{
int cnt;
cnt++;
}
return cnt;
}

{
return 1;
}
}
return 0;
}

struct Test* deleteNode(struct Test *head,int data)
{
}

}
}
}

int main()
{
struct Test p1 = {11,NULL};
struct Test p2 = {22,NULL};
struct Test p3 = {33,NULL};
struct Test p4 = {44,NULL};
struct Test new = {520,NULL};

p1.next = &p2;
p2.next = &p3;
p3.next = &p4;

return 0;
}

## 6.链表修改指定节点

#include <stdio.h>

struct Test
{
int data;
struct Test *next;
};

{
}
}

{
int cnt;
cnt++;
}
return cnt;
}

int changeNode(struct Test *head,int data,int newdata)
{
return 1;
}
}
return 0;
}

int main()
{
struct Test p1 = {11,NULL};
struct Test p2 = {22,NULL};
struct Test p3 = {33,NULL};
struct Test p4 = {44,NULL};
struct Test new = {520,NULL};

p1.next = &p2;
p2.next = &p3;
p3.next = &p4;

return 0;
}

## 7.头插法动态创建链表

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

struct Test
{
int data;
struct Test *next;
};

{
}
}

{
}
else{
}
}

{
struct Test *new;
while(1){
new = (struct Test*)malloc(sizeof(struct Test));
scanf("%d",&(new->data));
if(new->data == 0){
printf("end\n");
free(new);
}
}
}

int main()
{

return 0;
}

## 8.尾插法动态创建链表

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

struct Test
{
int data;
struct Test *next;
};

{
}
}

struct Test* insertfromend(struct Test *head,struct Test *new)
{
if(p == NULL){
}

while(p!=NULL){
if(p->next == NULL){
p->next = new;
}
p = p->next;
}
}

{
struct Test *new;
while(1){
new = (struct Test*)malloc(sizeof(struct Test));
scanf("%d",&(new->data));
if(new->data == 0){
printf("end\n");
free(new);
}
}
}

int main()
{

return 0;
}

Linux入门到精通

|
7天前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案，使用双指针法找到并返回链表中倒数第k个节点。
23 5
|
7天前
|
Python
【Leetcode刷题Python】剑指 Offer 18. 删除链表的节点
Leetcode题目"剑指 Offer 18. 删除链表的节点"的Python解决方案，通过使用双指针法找到并删除链表中值为特定数值的节点，然后返回更新后的链表头节点。
16 4
|
20天前
|

27 2
|
5天前
|

LeetCode初级算法题：反转链表+统计N以内的素数+删除排序数组中的重复项Java详解
LeetCode初级算法题：反转链表+统计N以内的素数+删除排序数组中的重复项Java详解
7 0
|
1月前
|

14 4
|
2月前
|

19 0
|
3月前
|

67 6
|
2月前
|

【数据结构与算法 刷题系列】求带环链表的入环节点（图文详解）
【数据结构与算法 刷题系列】求带环链表的入环节点（图文详解）
20 0
|
2月前
24. 两两交换链表中的节点
24. 两两交换链表中的节点
13 0
|
2月前
|

18 0