mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 12:16:29 +08:00
style(codes/c): update comment format
This commit is contained in:
parent
5271276f4e
commit
be2d109c5b
1 changed files with 25 additions and 28 deletions
|
@ -4,47 +4,44 @@
|
||||||
* Author: Zero (glj0@outlook.com)
|
* Author: Zero (glj0@outlook.com)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../include/include.h"
|
#include "../include/include.h"
|
||||||
|
|
||||||
/* 在链表的结点 n0 之后插入结点 P */
|
/* 在链表的结点 n0 之后插入结点 P */
|
||||||
void insertNode(ListNode* n0, ListNode* P) {
|
void insertNode(ListNode* n0, ListNode* P) {
|
||||||
ListNode *n1;
|
ListNode *n1 = n0->next;
|
||||||
n1 = n0 -> next;
|
n0->next = P;
|
||||||
n0 -> next = P;
|
P->next = n1;
|
||||||
P -> next = n1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 删除链表的结点 n0 之后的首个结点 */
|
/* 删除链表的结点 n0 之后的首个结点 */
|
||||||
void removeNode(ListNode* n0) {
|
void removeNode(ListNode* n0) {
|
||||||
|
if (!n0->next)
|
||||||
|
return;
|
||||||
// n0 -> P -> n1
|
// n0 -> P -> n1
|
||||||
ListNode *n1, *P;
|
ListNode *P = n0->next;
|
||||||
P = n0 -> next;
|
ListNode *n1 = P->next;
|
||||||
n1 = P -> next;
|
n0->next = n1;
|
||||||
n0 -> next = n1;
|
// 释放内存
|
||||||
|
|
||||||
free(P);
|
free(P);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 访问链表中索引为 index 的结点 */
|
/* 访问链表中索引为 index 的结点 */
|
||||||
ListNode* accessNode(ListNode* head, int index) {
|
ListNode* accessNode(ListNode* head, int index) {
|
||||||
ListNode* n;
|
while (head && head->next && index) {
|
||||||
while(head && head->next && index) {
|
|
||||||
head = head->next;
|
head = head->next;
|
||||||
index --;
|
index--;
|
||||||
}
|
}
|
||||||
n = head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 在链表中查找值为 target 的首个结点 */
|
/* 在链表中查找值为 target 的首个结点 */
|
||||||
int findNode(ListNode* head, int target) {
|
int findNode(ListNode* head, int target) {
|
||||||
ListNode* n;
|
int index = 0;
|
||||||
int idx=0;
|
while (head) {
|
||||||
while(head) {
|
if (head->val == target)
|
||||||
if(head->val == target) {
|
return index;
|
||||||
return idx;
|
head = head->next;
|
||||||
}
|
index++;
|
||||||
head = head -> next;
|
|
||||||
idx ++;
|
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -55,10 +52,10 @@ int main() {
|
||||||
/* 初始化链表 */
|
/* 初始化链表 */
|
||||||
// 初始化各个结点
|
// 初始化各个结点
|
||||||
ListNode* n0 = newListNode(1);
|
ListNode* n0 = newListNode(1);
|
||||||
ListNode* n1 = newListNode(2);
|
ListNode* n1 = newListNode(3);
|
||||||
ListNode* n2 = newListNode(3);
|
ListNode* n2 = newListNode(2);
|
||||||
ListNode* n3 = newListNode(4);
|
ListNode* n3 = newListNode(5);
|
||||||
ListNode* n4 = newListNode(5);
|
ListNode* n4 = newListNode(4);
|
||||||
// 构建引用指向
|
// 构建引用指向
|
||||||
n0->next = n1;
|
n0->next = n1;
|
||||||
n1->next = n2;
|
n1->next = n2;
|
||||||
|
@ -82,8 +79,8 @@ int main() {
|
||||||
printf("链表中索引 3 处的结点的值 = %d\r\n", node->val);
|
printf("链表中索引 3 处的结点的值 = %d\r\n", node->val);
|
||||||
|
|
||||||
/* 查找结点 */
|
/* 查找结点 */
|
||||||
int index = findNode(n0, 5);
|
int index = findNode(n0, 2);
|
||||||
printf("链表中值为 5 的结点的索引 = %d\r\n", index);
|
printf("链表中值为 2 的结点的索引 = %d\r\n", index);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue