链接列表 - 全局变量与局部变量
Linked Lists - Global v. Local Variables
我有以下算法,用于反转链表。
typedef struct node
{
int data;
struct node* next;
}LL_t;
void insert(LL_t** head, int value)
{
LL_t* new_node = (LL_t*) malloc(sizeof(LL_t));
new_node->data = value;
new_node->next = (*head);
(*head) = new_node;
}
LL_t* head;
// Post: L contains the data in the original L, but in the reverse order.
void reverse(LL_t * L) {
LL_t *current, *prev, *next;
current = head;
prev = NULL;
while ( current != NULL )
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
int main ()
{
struct node* L = NULL;
insert( &L, 42 );
insert( &L, 36 );
insert( &L, 14 );
insert( &L, 17 );
insert( &L, 48 );
insert( &L, 36 );
print(L);
printf("\n");
reverse(L);
print(L);
return 0;
}
反向函数前后打印的列表看起来是一样的。我相信我不小心使用了局部变量来修改我的列表,而我想直接修改列表(L)。输出:
36 48 17 14 36 42
36 48 17 14 36 42
如何使用全局变量直接修改链表?
您的代码的问题是您将指针按值传递给函数 reverse 并且指针 l
仍然指向第一个元素,而不是您在退出后所想的最后一个元素reverse 函数,需要通过引用传递指针并更改列表的头部,如下所示:
void reverse(LL_t **L) {
LL_t *current, *prev, *next;
current = *L;
prev = NULL;
while ( current != NULL )
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
*L = head;
}
来电应该是
reverse(&L);
进行此更改,然后调用
print(L)
并看到你得到了一个反向链表。
如果你有一个固定的原型,就像你在评论中所说的那样,那就去
void reverse(LL_t *L) {
LL_t *current, *prev, *next;
current = L;
prev = NULL;
while ( current != NULL )
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
调用应该是
reverse(L);
然后打印调用
print(head);
head = L;//you don't assign to head.
reverse(L);//unused L at reverse.
print(head);
我有以下算法,用于反转链表。
typedef struct node
{
int data;
struct node* next;
}LL_t;
void insert(LL_t** head, int value)
{
LL_t* new_node = (LL_t*) malloc(sizeof(LL_t));
new_node->data = value;
new_node->next = (*head);
(*head) = new_node;
}
LL_t* head;
// Post: L contains the data in the original L, but in the reverse order.
void reverse(LL_t * L) {
LL_t *current, *prev, *next;
current = head;
prev = NULL;
while ( current != NULL )
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
int main ()
{
struct node* L = NULL;
insert( &L, 42 );
insert( &L, 36 );
insert( &L, 14 );
insert( &L, 17 );
insert( &L, 48 );
insert( &L, 36 );
print(L);
printf("\n");
reverse(L);
print(L);
return 0;
}
反向函数前后打印的列表看起来是一样的。我相信我不小心使用了局部变量来修改我的列表,而我想直接修改列表(L)。输出:
36 48 17 14 36 42
36 48 17 14 36 42
如何使用全局变量直接修改链表?
您的代码的问题是您将指针按值传递给函数 reverse 并且指针 l
仍然指向第一个元素,而不是您在退出后所想的最后一个元素reverse 函数,需要通过引用传递指针并更改列表的头部,如下所示:
void reverse(LL_t **L) {
LL_t *current, *prev, *next;
current = *L;
prev = NULL;
while ( current != NULL )
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
*L = head;
}
来电应该是
reverse(&L);
进行此更改,然后调用
print(L)
并看到你得到了一个反向链表。
如果你有一个固定的原型,就像你在评论中所说的那样,那就去
void reverse(LL_t *L) {
LL_t *current, *prev, *next;
current = L;
prev = NULL;
while ( current != NULL )
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
调用应该是
reverse(L);
然后打印调用
print(head);
head = L;//you don't assign to head.
reverse(L);//unused L at reverse.
print(head);