为什么我的 LinkedList 在我的列表末尾读取一个额外的节点?
Why is my LinkedList reading an extra node at the end of my list?
我正在尝试为我的 LinkedList class 编写函数 moveLasttoFront,但它似乎在列表末尾的 nullptr 之前读取另一个节点.
例如,假设我有一个链表 {0, 1, 2, 8, 3, 4}。我想 运行 指针遍历列表,直到它等于 nullptr 并停止。但是它没有在 4 之后停止,而是再次迭代,然后停止(我认为)。这是我的代码:
void moveLasttoFront ( ) {
Node * last = head;
Node * secLast = nullptr;
while ( (*last).next != nullptr ) {
secLast = last;
last = (*last).next;
}
(*last).next = head;
(*secLast).next = nullptr;
head = (*last).next;
}
例如,在上面的列表中使用时,secLast 最终指向节点 4,而 last 最终指向 0。我不明白这个“0”是从哪里来的,因为在 4 之后不应该有另一个节点? secLast 应指向 3,last 应指向 4。
如有任何帮助,我们将不胜感激。
谢谢!
P.S。对不起,如果这太过分了 vague/I 用错了。这是我第一次在这里提问:)
我用 C 做了这个例子。
Main.c
#include "linked_list.h"
Node* moveLastToFront(Node* head) {
Node* secondLast;
Node* last = head;
while (last->next != NULL) {
secondLast = last;
last = last->next;
}
(*secondLast).next = NULL;
(*last).next = head;
head = last;
return head;
}
int main(void) {
Node* head = NULL;
for (int i = 1; i < 10; i++)
head = addnode(head, i);
listprint(head);
printf("\n");
head = moveLastToFront(head);
listprint(head);
return 0;
}
linked_list.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
typedef struct Node {
int data;
struct Node* next;
} Node;
Node *addnode(Node* node, int data);
void listprint(Node* node);
void freelist(Node* node);
#endif // LINKED_LIST_H
linked_list.c
#include "linked_list.h"
#include <stdio.h>
#include <stdlib.h>
Node* addnode(Node* node, int data) {
if (node == NULL) {
node = malloc(sizeof (Node));
node->data = data;
node->next = NULL;
}
else
node->next = addnode(node->next, data);
return node;
}
void freelist(Node* node) {
if (node != NULL) {
freelist(node->next);
free(node);
}
}
void listprint(Node* node) {
static int i = 0;
if (node != NULL) {
printf("Node [%d] points to node [%d] which contains: %d\n", i, i + 1, node->data);
i++;
listprint(node->next);
}
else
i = 0; // Reset i in case function is called again with another list
}
输出:
Node [0] points to node [1] which contains: 1
Node [1] points to node [2] which contains: 2
Node [2] points to node [3] which contains: 3
Node [3] points to node [4] which contains: 4
Node [4] points to node [5] which contains: 5
Node [5] points to node [6] which contains: 6
Node [6] points to node [7] which contains: 7
Node [7] points to node [8] which contains: 8
Node [8] points to node [9] which contains: 9
Node [0] points to node [1] which contains: 9
Node [1] points to node [2] which contains: 1
Node [2] points to node [3] which contains: 2
Node [3] points to node [4] which contains: 3
Node [4] points to node [5] which contains: 4
Node [5] points to node [6] which contains: 5
Node [6] points to node [7] which contains: 6
Node [7] points to node [8] which contains: 7
Node [8] points to node [9] which contains: 8
如有任何疑问,请随时提出!
我正在尝试为我的 LinkedList class 编写函数 moveLasttoFront,但它似乎在列表末尾的 nullptr 之前读取另一个节点.
例如,假设我有一个链表 {0, 1, 2, 8, 3, 4}。我想 运行 指针遍历列表,直到它等于 nullptr 并停止。但是它没有在 4 之后停止,而是再次迭代,然后停止(我认为)。这是我的代码:
void moveLasttoFront ( ) {
Node * last = head;
Node * secLast = nullptr;
while ( (*last).next != nullptr ) {
secLast = last;
last = (*last).next;
}
(*last).next = head;
(*secLast).next = nullptr;
head = (*last).next;
}
例如,在上面的列表中使用时,secLast 最终指向节点 4,而 last 最终指向 0。我不明白这个“0”是从哪里来的,因为在 4 之后不应该有另一个节点? secLast 应指向 3,last 应指向 4。
如有任何帮助,我们将不胜感激。
谢谢!
P.S。对不起,如果这太过分了 vague/I 用错了。这是我第一次在这里提问:)
我用 C 做了这个例子。
Main.c
#include "linked_list.h"
Node* moveLastToFront(Node* head) {
Node* secondLast;
Node* last = head;
while (last->next != NULL) {
secondLast = last;
last = last->next;
}
(*secondLast).next = NULL;
(*last).next = head;
head = last;
return head;
}
int main(void) {
Node* head = NULL;
for (int i = 1; i < 10; i++)
head = addnode(head, i);
listprint(head);
printf("\n");
head = moveLastToFront(head);
listprint(head);
return 0;
}
linked_list.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
typedef struct Node {
int data;
struct Node* next;
} Node;
Node *addnode(Node* node, int data);
void listprint(Node* node);
void freelist(Node* node);
#endif // LINKED_LIST_H
linked_list.c
#include "linked_list.h"
#include <stdio.h>
#include <stdlib.h>
Node* addnode(Node* node, int data) {
if (node == NULL) {
node = malloc(sizeof (Node));
node->data = data;
node->next = NULL;
}
else
node->next = addnode(node->next, data);
return node;
}
void freelist(Node* node) {
if (node != NULL) {
freelist(node->next);
free(node);
}
}
void listprint(Node* node) {
static int i = 0;
if (node != NULL) {
printf("Node [%d] points to node [%d] which contains: %d\n", i, i + 1, node->data);
i++;
listprint(node->next);
}
else
i = 0; // Reset i in case function is called again with another list
}
输出:
Node [0] points to node [1] which contains: 1
Node [1] points to node [2] which contains: 2
Node [2] points to node [3] which contains: 3
Node [3] points to node [4] which contains: 4
Node [4] points to node [5] which contains: 5
Node [5] points to node [6] which contains: 6
Node [6] points to node [7] which contains: 7
Node [7] points to node [8] which contains: 8
Node [8] points to node [9] which contains: 9
Node [0] points to node [1] which contains: 9
Node [1] points to node [2] which contains: 1
Node [2] points to node [3] which contains: 2
Node [3] points to node [4] which contains: 3
Node [4] points to node [5] which contains: 4
Node [5] points to node [6] which contains: 5
Node [6] points to node [7] which contains: 6
Node [7] points to node [8] which contains: 7
Node [8] points to node [9] which contains: 8
如有任何疑问,请随时提出!