链表

有头节点的单链表的增删查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 链表
#include
#include

typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;

LNode* initList(){ // 初始化
LNode *L = (LNode *)malloc(sizeof(LNode));
if(L == NULL){
exit(-1);
}
L->next = NULL; // 头节点没有数据
return L;
}
LNode* makeNode(int data){ // 生成节点
LNode *Node = (LNode *)malloc(sizeof(LNode));
Node->next = NULL;
Node->data = data;
return Node;
}
LNode * createList(struct LNode *L){ // 创建单链表
int a[] = {12,32,1,4,54,6,34};
int i =0;
LNode *tile = NULL;
LNode *head = NULL;
head = L;
for(i =0;i<=sizeof(a)/sizeof(int);i++){
LNode * temp = makeNode(a[i]);
if(!head->next){
head->next = temp;
tile= temp;
}else{
tile->next = temp;
tile = temp;
}
tile = temp;
tile->next = NULL;
}
return head;

}
void printList(LinkList L){ // 打印链表
LNode *p = L->next;
while(p->next != NULL){
printf("%10d",p->data);
p = p->next;
}
}
LNode* search(struct LNode *L,int data){ // 查找
LNode *p = L->next;
while(p->next != NULL){
if(p && p->data == data){
return p;
}else{
p =p->next;
}
}
}

int getListLength(LinkList L){ // 计算链表的长度
int length =0,i;
LNode *p =L->next;
while(p->next != NULL){
p = p->next;
length++;
}
return length;
}
void InsertList(LinkList L,int data,int index){ // 插入链表
LNode *p = L;
if(index<0 || index >getListLength(L)){
printf("blunder\n");
return;
}
int j =0;
while(p&& j
p =p->next;
j++;
}
LNode *newNode = (LinkList)malloc(sizeof(LNode));
newNode->data = data;
newNode->next = NULL;
newNode->next = p->next;
p->next = newNode;
}

void deleteList(LinkList L,int data){ 
LNode *p = L->next; // p 要删除元素的指针
LNode *temp = L->next; // 删除元素的前一个位置
int length=0;       
while(p->next != NULL){   // 查找元素位置
if(p && p->data == data){
break;
}else{
p =p->next;
length++;
}
}
int i;
for(i=0;i-1;i++){ // 找到元素的前一个位置

temp = temp->next;
}
// printf(" length %d\n",length);
// printf("p 的位置 %d\n",p->data);
// printf("q 的位置 %d\n",q->data);
// printf("temp 的位置 %d\n",temp->data);
temp->next = p->next; // 将此元素的前一个位置的指针指向此元素的后一个元素
free(p);
}
int main(){
LNode *List;
List = initList();
List = createList(List);
printList(List);
printf("\n");
printf("search %d\n",search(List,1)->data);
printList(List);
printf("\n");
deleteList(List,34);
deleteList(List,1);

InsertList(List,100,0);
printf("InsertList\n");
printList(List);
printf("\n");
}
1
// 链表: 适合很多次插入删除操作

本文标题:链表

文章作者:philxling

发布时间:2020年10月15日 - 13:45

最后更新:2020年10月15日 - 14:12

原始链接:http://philxling.club/2020/10/15/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E9%93%BE%E8%A1%A8/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

philxling wechat
ex. subscribe to my blog by scanning my public wechat account
-------------本文结束感谢您的阅读-------------
谢谢你给我糖吃!!!
0%