// 假设Node结构体已经界说,且链表有一个哑节点head | |
void insertAtHead(Node **head, int value) { | |
Node *newNode = (Node *)malloc(sizeof(Node)); | |
newNode->data = value; | |
newNode->next = (*head)->next; // 将新节点的next指向原来的第一个数据节点 | |
(*head)->next = newNode; // 更新哑节点的next为新节点 | |
} |
// 假设Node结构体已经界说,且链表有一个哑节点head | |
void insertAtTail(Node **head, int value) { | |
Node *newNode = (Node *)malloc(sizeof(Node)); | |
newNode->data = value; | |
newNode->next = NULL; | |
Node *current = *head; | |
// 遍历链表找到末了一个节点 | |
while (current->next != NULL) { | |
current = current->next; | |
} | |
// 将末了一个节点的next指向新节点 | |
current->next = newNode; | |
} |
// 假设Node结构体已经界说,且head直接指向链表的第一个数据节点 | |
void insertAtHead(Node **head, int value) { | |
Node *newNode = (Node *)malloc(sizeof(Node)); | |
newNode->data = value; | |
newNode->next = *head; // 将新节点的next指向原来的头节点 | |
*head = newNode; // 更新头指针为新节点 | |
} |
// 假设Node结构体已经界说,且head直接指向链表的第一个数据节点 | |
void insertAtTail(Node **head, int value) { | |
Node *newNode = (Node *)malloc(sizeof(Node)); | |
newNode->data = value; | |
newNode->next = NULL; | |
if (*head == NULL) { | |
// 如果链表为空,则新节点即为头节点 | |
*head = newNode; | |
} else { | |
Node *current = *head; | |
// 遍历链表找到末了一个节点 | |
while (current->next != NULL) { | |
current = current->next; | |
} | |
// 将末了一个节点的next指向新节点 | |
current->next = newNode; | |
} | |
} |
| A | B | C | D | E |
A | 0 | 0 | 1 | 1 | 0 |
B | 0 | 0 | 0 | 0 | 0 |
C | 0 | 1 | 0 | 0 | 0 |
D | 0 | 1 | 0 | 0 | 1 |
E | 0 | 0 | 0 | 0 | 0 |
A | ->C->D->NULL |
B | ->NULL |
C | ->B->NULL |
D | ->B->E |
E | ->NULL |
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |