C链表removeLast使用指针
C linkedlist removeLast using pointers
我正在学习如何在 c 中制作 linked 列表,但我遇到了一个我不知道如何解决的问题。我的 linked 列表的 removeLast(link * ptr) 方法有问题,我相信这与我使用指针有关,但我真的不明白为什么程序失败从列表中删除下一个最后一个元素,不知何故我的列表被破坏了。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
// typedef is used to give a data type a new name
typedef struct node * link ;// link is now type struct node pointer
/*
typedef allows us to say "link ptr"
instead of "struct node * ptr"
*/
struct node{
int item ;// this is the data
link next ;//same as struct node * next, next is a pointer
};
//prints the link
void printAll(link ptr){
printf("\nPrinting Linked List:\n");
while(ptr != NULL){
printf(" %d ", (*ptr).item);
ptr = (*ptr).next;// same as ptr->next
}
printf("\n");
}
//adds to the head of the link
// link * ptr so that we may modify the head pointer
void addFirst(link * ptr, int val ){
link tmp = malloc(sizeof(struct node));// allocates memory for new node
tmp->item = val;
tmp->next = * ptr;
* ptr = tmp;
}
// removes and returns the last element in the link
// link * ptr so that we may modify the head pointer
link removeLast(link * ptr){
if(ptr == NULL) return NULL;
// traverse the link
link prev = NULL;// prev is pointer
while((*ptr)->next != NULL){
prev = *ptr;
*ptr = (*ptr)->next;
}
// if only one node on list
if(prev == NULL){
link tmp = malloc(sizeof(struct node));// allocates memory for new node
tmp = *ptr;
*ptr = NULL;
return tmp;
}
// if more than one node
prev->next = NULL;
return *ptr;
}
// testing
int main(void) {
link head = NULL;// same as struct node * head, head is a pointer type
//populating list
for(int i = 0; i<10; i++){
addFirst(&head, i);// "&" is needed to pass address of head
}
printAll(head);
while(head != NULL){
link tmp = removeLast(&head);
if(tmp != NULL)
printf(" %d ", tmp->item);
}
return 0;
}
这是我的输出:
Printing Linked List:
9 8 7 6 5 4 3 2 1
prev = 9
prev = 8
prev = 7
prev = 6
prev = 5
prev = 4
prev = 3
prev = 2
0 0
RUN SUCCESSFUL (total time: 136ms)
感谢您的宝贵时间和帮助。
您将指向 head
的指针传递给 removeLast()
(参数 ptr
)。在该函数中,您修改 *ptr
.
由于ptr
指向head
变量所在的内存位置,修改*ptr
会修改head
的内容。由于 head
的内容被函数调用修改,因此它不引用函数 returns.
之后的列表的实际头部
为避免这种情况,您应该在 removeLast()
中使用一个单独的局部变量来遍历列表,并且只有在您真正想要更改 head
.[=22= 时才修改 *ptr
]
我正在学习如何在 c 中制作 linked 列表,但我遇到了一个我不知道如何解决的问题。我的 linked 列表的 removeLast(link * ptr) 方法有问题,我相信这与我使用指针有关,但我真的不明白为什么程序失败从列表中删除下一个最后一个元素,不知何故我的列表被破坏了。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
// typedef is used to give a data type a new name
typedef struct node * link ;// link is now type struct node pointer
/*
typedef allows us to say "link ptr"
instead of "struct node * ptr"
*/
struct node{
int item ;// this is the data
link next ;//same as struct node * next, next is a pointer
};
//prints the link
void printAll(link ptr){
printf("\nPrinting Linked List:\n");
while(ptr != NULL){
printf(" %d ", (*ptr).item);
ptr = (*ptr).next;// same as ptr->next
}
printf("\n");
}
//adds to the head of the link
// link * ptr so that we may modify the head pointer
void addFirst(link * ptr, int val ){
link tmp = malloc(sizeof(struct node));// allocates memory for new node
tmp->item = val;
tmp->next = * ptr;
* ptr = tmp;
}
// removes and returns the last element in the link
// link * ptr so that we may modify the head pointer
link removeLast(link * ptr){
if(ptr == NULL) return NULL;
// traverse the link
link prev = NULL;// prev is pointer
while((*ptr)->next != NULL){
prev = *ptr;
*ptr = (*ptr)->next;
}
// if only one node on list
if(prev == NULL){
link tmp = malloc(sizeof(struct node));// allocates memory for new node
tmp = *ptr;
*ptr = NULL;
return tmp;
}
// if more than one node
prev->next = NULL;
return *ptr;
}
// testing
int main(void) {
link head = NULL;// same as struct node * head, head is a pointer type
//populating list
for(int i = 0; i<10; i++){
addFirst(&head, i);// "&" is needed to pass address of head
}
printAll(head);
while(head != NULL){
link tmp = removeLast(&head);
if(tmp != NULL)
printf(" %d ", tmp->item);
}
return 0;
}
这是我的输出:
Printing Linked List:
9 8 7 6 5 4 3 2 1
prev = 9
prev = 8
prev = 7
prev = 6
prev = 5
prev = 4
prev = 3
prev = 2
0 0
RUN SUCCESSFUL (total time: 136ms)
感谢您的宝贵时间和帮助。
您将指向 head
的指针传递给 removeLast()
(参数 ptr
)。在该函数中,您修改 *ptr
.
由于ptr
指向head
变量所在的内存位置,修改*ptr
会修改head
的内容。由于 head
的内容被函数调用修改,因此它不引用函数 returns.
为避免这种情况,您应该在 removeLast()
中使用一个单独的局部变量来遍历列表,并且只有在您真正想要更改 head
.[=22= 时才修改 *ptr
]