本文主要介绍链表的相关代码实现。如下:
复制代码
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148//---------------------------链表综合示例---------------- typedef struct Node { int data; struct Node* next; }SLIST; SLIST* Slist_Create() { SLIST *pHead = NULL,*pCur = NULL,*PNew = NULL; pHead = (SLIST*)malloc(sizeof(SLIST)); if(pHead == NULL) return NULL; pHead->data = 0; pHead->next = NULL; int data = 0; pCur = pHead; printf("Please input data:"); scanf("%d",&data); while(data != -1) { PNew = (SLIST*)malloc(sizeof(SLIST)); if(PNew == NULL) return NULL; PNew->data = data; PNew->next = NULL; pCur->next = PNew; pCur = PNew; printf("Please input data:"); scanf("%d",&data); } return pHead; } int Slist_Print(SLIST* pHead) { if(pHead == NULL) return -1; SLIST *tmp = pHead->next; while(tmp) { printf("%d ",tmp->data); tmp = tmp->next; } return 0; } int Slist_NodeInsert(SLIST* pHead, int x, int y) { if(pHead == NULL) return -1; SLIST* tmp = (SLIST*)malloc(sizeof(SLIST)); tmp->data = y; tmp->next = NULL; SLIST *pPre = pHead; SLIST *pCur = pHead->next; while(pCur) { if(pCur->data == x) break; pPre = pCur; pCur = pCur->next; } tmp->next = pCur; pPre->next = tmp; return 0; } int Slist_NodeDel(SLIST* pHead, int x) { if(pHead == NULL) return -1; SLIST *pDel = (SLIST*)malloc(sizeof(SLIST)); if(pDel == NULL) return -1; pDel->data = x; pDel->next = NULL; SLIST *pPre = pHead; SLIST *pCur = pHead->next; while(pCur) { if(pCur->data == x) { pDel = pCur; break; } pPre = pCur; pCur = pCur->next; } if(pCur == NULL) { printf("not find the number of :%d",x); return -2; } pPre->next = pCur->next; if(pDel != NULL) free(pDel); return -1; } int Slist_Destory(SLIST* pHead) { if(pHead == NULL) return -1; SLIST* tmp = pHead; while(pHead) { tmp = pHead->next; free(pHead); pHead = tmp; } return 0; } int Slist_Reverse(SLIST* pHead) { if(pHead == NULL || pHead->next == NULL || pHead->next->next == NULL) return -1; SLIST *pPre = pHead->next; SLIST *pCur = pHead->next->next; pPre->next = NULL; while(pCur) { SLIST *pNext = pCur->next; pCur->next = pPre; pPre = pCur; pCur = pNext; } pHead->next = pPre; return 0; } int main() { SLIST* myPHead; int ret = 0; myPHead = Slist_Create(); if(myPHead == NULL) return -1; ret = Slist_Print(myPHead); printf("n"); ret = Slist_NodeInsert(myPHead,10,12); ret = Slist_Print(myPHead); printf("n"); ret = Slist_NodeDel(myPHead,12); ret = Slist_Print(myPHead); printf("n"); ret = Slist_Reverse(myPHead); ret = Slist_Print(myPHead); printf("n"); ret = Slist_Destory(myPHead); return 0; }
下面是剑指offer中有关链表的处理算法:
复制代码
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218//---------------------------链表综合---剑指offer中---------------- //-----------------O(1)时间删除一个链表结点---------------- typedef struct LinkedListNode { int m_data; struct LinkedListNode *m_next; }LinkedListNode; void DeleteNode(LinkedListNode **pHead, LinkedListNode *pDelNode) { if(pHead == NULL || *pHead == NULL || pDelNode == NULL) return; if(pDelNode->m_next != NULL)//删除的结点不是尾结点 { LinkedListNode *pNext = pDelNode->m_next; pDelNode->m_data = pNext->m_data; pDelNode->m_next = pNext->m_next; delete pDelNode; pNext = NULL; } else if(*pHead == pDelNode)//链表只有一个节点 { delete pDelNode; pDelNode = NULL; *pHead = NULL; } else //删除的结点为尾结点 { LinkedListNode *pNode = *pHead; while(pNode->m_next != pDelNode) { pNode = pNode->m_next; } pNode->m_next = NULL; delete pDelNode; pNode = NULL; } } //----------------在排序的链表中删除重复的结点(包含该节点)---------------- void DeleteDuplication(LinkedListNode **pHead) { if(pHead == NULL || *pHead == NULL) return; LinkedListNode *pPre = nullptr; LinkedListNode *pNode = *pHead; while(pNode != nullptr) { LinkedListNode *pNext = pNode->m_next; bool flag = false; if(pNext != nullptr && pNext->m_data == pNode->m_data) flag = true; if(!flag) { pPre = pNode; pNode = pNext; } else { int value = pNode->m_data; LinkedListNode *pDelNode = pNode; while(pDelNode != nullptr && pDelNode->m_data == value) { pNext = pDelNode->m_next; delete pDelNode; pDelNode = nullptr; pDelNode = pNext; } if(pPre == nullptr) *pHead = pNext; else pPre->m_next = pNext; pNode = pNext; } } } //----------------在排序的链表中删除重复的结点(保留一个该节点)---------------- void DeleteDuplication2(LinkedListNode **pHead) { if(pHead == NULL || *pHead == NULL) return; LinkedListNode *pPre = *pHead;//--------- LinkedListNode *pNode = *pHead; while(pNode != nullptr) { LinkedListNode *pNext = pNode->m_next; bool flag = false; if(pNext != nullptr && pNext->m_data == pNode->m_data) flag = true; if(!flag) { pPre = pNode; pNode = pNext; } else { int value = pNode->m_data; LinkedListNode *pDelNode = pNode->m_next;//----- pre = pNode;//----- while(pDelNode != nullptr && pDelNode->m_data == value) { pNext = pDelNode->m_next; delete pDelNode; pDelNode = nullptr; pDelNode = pNext; } //------ pPre->m_next = pNext; pNode = pNext; } } } //----------------链表中倒数第k个结点---------------- //方法一 LinkedListNode* FindKthToTail(LinkedListNode *pHead, unsigned int k) { if(pHead == NULL || k <=0) return NULL; LinkedListNode *pAhead = pHead; LinkedListNode *pBehind = nullptr; for(unsigned int i=0;i<k-1;i++) { //pAhead = pAhead->m_next; if(pAhead->m_next != nullptr) pAhead = pAhead->m_next; else return nullptr; } pBehind = pHead; while(pAhead->m_next != nullptr) { pAhead = pAhead->m_next; pBehind = pBehind->m_next; } return pBehind; } //方法二 LinkedListNode* FindKthToTail2(LinkedListNode *pHead, unsigned int k) { if(pHead == NULL || k <=0) return NULL; LinkedListNode *pAhead = pHead; LinkedListNode *pBehind = nullptr; for(unsigned int i=0; i<k;i++) { pAhead = pAhead->m_next; if(pAhead == nullptr) return nullptr; } pBehind = pHead; while(pAhead!= nullptr) { pAhead = pAhead->m_next; pBehind = pBehind->m_next; } return pBehind; } //----------------求链表的中间节点----------------********************************* //思路:定义两个节点,一个一次走一步,一个一次走两步,当真的快的到末尾时,慢的就为中间节点 LinkedListNode *MidNode(LinkedListNode *pHead) { if(pHead == NULL) return NULL; LinkedListNode *p1 = pHead; LinkedListNode *p2 = pHead; while(p2->m_next != NULL) { p1 = p1->m_next; p2 = p2->m_next; if(p2->m_next) p2 = p2->m_next; } return p1; } //----------------环形链表中环的入口节点---------------- ListNode* MeetingNode(ListNode* pHead) { if(pHead == NULL) return NULL; ListNode* pSlow = pHead->m_next; if(pSlow == NULL) return NULL; ListNode* pFast = pSlow->m_next; while(pFast != NULL && pSlow != NULL) { if(pFast == pSlow) return pFast; pSlow = pSlow->m_next; pFast = pFast->m_next; if(pFast!=NULL) pFast = pFast->m_next; } return NULL; } ListNode* EntryNodeOfLoop(ListNode* pHead) { ListNode* meetNode = MeetingNode(pHead);//1 确定是环 if(meetNode == NULL) return NULL; ListNode* pNode = meetNode; int numLoop = 1; while(pNode->m_next != meetNode)//2 确定环的节点数目 { numLoop++; pNode = pNode->m_next; } pNode = pHead; for(int i=0;i<numLoop;i++) pNode = pNode->m_next; ListNode* pNode2 = pHead; while(pNode != pNode2) { pNode = pNode->m_next; pNode2 = pNode2->m_next; } return pNode; }
最后
以上就是神勇摩托最近收集整理的关于链表的相关操作知识点的全部内容,更多相关链表内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复