C 中的基本链表
Basic Linked List in C
我正在用 C 编写一个基本的链表程序,在删除时遇到了一些麻烦。这是我拥有的:
#include <stdio.h>
struct node * delete(struct node * head, struct node * toDelete);
void print(struct node * head);
struct node {
int value;
struct node *next;
};
int main(int argc, const char * argv[]) {
struct node node1, node2, node3;
struct node *head = &node1;
node1.value = 1;
node1.next = &node2;
node2.value = 2;
node2.next = &node3;
node3.value = 3;
node3.next = (struct node *) 0;
print(head);
delete(head, &node3);
print(head);
return 0;
}
struct node * delete(struct node * head, struct node * toDelete) {
//if to delete is head
if (head == toDelete) {
head = head->next;
} else {
//find node preceding node to delete
struct node *current = head;
while (current->next != toDelete) {
current = current->next;
}
current = current->next->next;
}
return head;
}
void print(struct node * head) {
struct node *current = head;
while (current != (struct node *) 0) {
printf("%i\n", current->value);
current = current->next;
}
}
问题 #1:
所以我试着写:
delete(head, node3);
但是xCode要我在"node3"前面加上“&”。是不是一般我定义函数取指针的时候,需要传入内存地址?
问题 #2:
我的打印函数可以打印出 3 个节点的值。在调用 delete 并尝试删除 node3 后,它仍然打印出 3 个节点。我不确定我哪里出错了。我找到我要删除的节点之前的节点,并将其下一个指针设置为节点之后的节点(非正式地:node.next = node.next.next)。
有什么想法吗?
感谢您的帮助,
克莱曼
只需尝试将 current = current->next->next;
更改为 current->next=current->next->next
。如果它不起作用,请告诉我。
你应该通过它 &node3
。要删除,请更改您的代码
current = current->next->next;
至 current->next = current->next->next;
but xCode wanted me to add "&" in front of "node3". Is it generally true that
when I define a function to take a pointer, I need to pass in the memory
address?
是的,如果你声明函数接受一个指针,你必须给它传递一个指针。
此外,当从链表中删除一个值时,您将要更改
current->next = current->next->next
Is it generally true that when I define a function to take a pointer, I need to pass in the memory address?
是的,xCode是对的。 node3
是一个 struct node
,但是你的函数 delete
将 struct node *
作为第二个参数,所以你必须将指针传递给 node3
,而不是变量本身.
After calling delete and trying to delete node3, it still prints out the 3 nodes.
这是因为您没有更改 next
的值。另外,为了内存安全,不要忘记检查指针是否为 NULL
:
while ((current->next != toDelete) && (current->next != NULL)) {
current = current->next;
}
if (current->next != NULL)
current->next = current->next->next;
我正在用 C 编写一个基本的链表程序,在删除时遇到了一些麻烦。这是我拥有的:
#include <stdio.h>
struct node * delete(struct node * head, struct node * toDelete);
void print(struct node * head);
struct node {
int value;
struct node *next;
};
int main(int argc, const char * argv[]) {
struct node node1, node2, node3;
struct node *head = &node1;
node1.value = 1;
node1.next = &node2;
node2.value = 2;
node2.next = &node3;
node3.value = 3;
node3.next = (struct node *) 0;
print(head);
delete(head, &node3);
print(head);
return 0;
}
struct node * delete(struct node * head, struct node * toDelete) {
//if to delete is head
if (head == toDelete) {
head = head->next;
} else {
//find node preceding node to delete
struct node *current = head;
while (current->next != toDelete) {
current = current->next;
}
current = current->next->next;
}
return head;
}
void print(struct node * head) {
struct node *current = head;
while (current != (struct node *) 0) {
printf("%i\n", current->value);
current = current->next;
}
}
问题 #1: 所以我试着写:
delete(head, node3);
但是xCode要我在"node3"前面加上“&”。是不是一般我定义函数取指针的时候,需要传入内存地址?
问题 #2:
我的打印函数可以打印出 3 个节点的值。在调用 delete 并尝试删除 node3 后,它仍然打印出 3 个节点。我不确定我哪里出错了。我找到我要删除的节点之前的节点,并将其下一个指针设置为节点之后的节点(非正式地:node.next = node.next.next)。
有什么想法吗?
感谢您的帮助, 克莱曼
只需尝试将 current = current->next->next;
更改为 current->next=current->next->next
。如果它不起作用,请告诉我。
你应该通过它 &node3
。要删除,请更改您的代码
current = current->next->next;
至 current->next = current->next->next;
but xCode wanted me to add "&" in front of "node3". Is it generally true that
when I define a function to take a pointer, I need to pass in the memory
address?
是的,如果你声明函数接受一个指针,你必须给它传递一个指针。
此外,当从链表中删除一个值时,您将要更改
current->next = current->next->next
Is it generally true that when I define a function to take a pointer, I need to pass in the memory address?
是的,xCode是对的。 node3
是一个 struct node
,但是你的函数 delete
将 struct node *
作为第二个参数,所以你必须将指针传递给 node3
,而不是变量本身.
After calling delete and trying to delete node3, it still prints out the 3 nodes.
这是因为您没有更改 next
的值。另外,为了内存安全,不要忘记检查指针是否为 NULL
:
while ((current->next != toDelete) && (current->next != NULL)) {
current = current->next;
}
if (current->next != NULL)
current->next = current->next->next;