![](/img/trans.png)
[英]How to use pointer to pointer pointers to pointers insert item in this linked list
[英]how to use a pointer to pointer to insert in a linked list
Think是一个按名称顺序插入新元素的函数。 如果我使用if分隔开始和其他人插入的条件,我知道该怎么做。 但是我被要求将if和while合并为一个while循环。 我如何将插入函数与一个指向指针的指针集成到一个while循环中?
person* insert_sorted(person *people, char *name, int age)
{
person *p=NULL;//,*t=NULL,*q=NULL;
person *ptr= people;
person **ptr2ptr=&ptr;
p=malloc(sizeof(person));
if ( p == NULL ){
printf("malloc() failed\n");
return NULL;
}
else {
p->name = name;
p->age = age;
if ( people == NULL ){ // empty list
people = p;
people->next =NULL;
}
else{
*ptr2ptr = ptr;
while( (*ptr2ptr) !=NULL )
{
if ( compare_people(p, people)<=0 ) // insert at the start
break;
else if ( (*ptr2ptr)->next == NULL) //insert at the end
break;
else if ( compare_people(*ptr2ptr, p) <=0 && compare_people( p, (*ptr2ptr)->next)<=0 )//insert at the middle
break;
*ptr2ptr = (*ptr2ptr)->next;
}
//insert at the end
p->next = (*ptr2ptr)->next;
(*ptr2ptr)->next = p;
}
}
e与其尝试在列表中没有后继的person
元素中查找,不如尝试查找第一个空指针。 像这样(未经测试):
void insert_sorted(person **p, char *name, int age)
{
while (*p) {
p = &(*p)->next;
}
*p = malloc( ... );
/* ... */
}
通常最好用笔,纸,然后画几个方框和箭头来解决这种问题。 这个想法是,您的“ p”指针不再指向特定的person
,而是指向某person
某个指针。
可能有一些选择。
如果可以更改,我将在compare_people函数内部移动if。 毕竟,在列表中添加第一个元素就像添加一个新的“列表顶部”元素(至少是列表中的元素)一样。 我知道这可以看作是“作弊”。 确实是这样!
您可以创建一个“假”列表元素,该元素将始终被测试为已排序列表的第一个(或最后一个)(例如,名称为空)。 因此,列表永远不会为空,也不会进行“检查空列表”测试。 当然,该假项目的内容需要符合compare_people函数的语义。
实际上,您可以使用临时支持结构(如指针数组)和来自stdlib.h的qsort()来以稍微高于当前O(n),O(n * log(n))的代价进行操作,保持列表排序。
最后,实现插入排序 ,它将利用在插入新元素之前已经对原始集合进行排序的事实。
该函数可以按以下方式编写(无需测试,因为我不知道列表的某些定义)
person * insert_sorted( person **people, char *name, int age )
{
person *p = malloc( sizeof( person ) );
if ( p == NULL )
{
printf( "malloc() failed\n" );
}
else
{
p->name = name;
p->age = age;
person *prev = NULL;
person *current = *people;
while ( current && !( compare_people( p, current ) < 0 ) )
{
prev = current;
current = current->next;
}
p->next = current;
if ( prev == NULL ) *people = p;
else prev->next = p;
}
return p;
}
函数应该像
insert_sorted( &people, name, age );
^^^^^^^
未经测试:
person* insert_sorted(person** people, char *name, int age) {
person* added = malloc(sizeof(person));
added->name = name;
added->age = age;
added->next = NULL;
person* previous = NULL;
person* current = *people;
while (current && compare_people(current, added) <= 0) {
previous = current;
current = current->next;
}
if (!people) {
*people = added;
} else {
previous->next = added;
added->next = current;
}
return added;
}
使用指针指向指针的方式没有利用间接寻址。 您只能在通常会写成“ ptr”的地方写(*ptr2ptr)
。
使用指向节点指针的指针的想法是,通过添加一个间接级别,您可以从调用函数访问和修改头指针。 如果仅传递节点指针,则对该指针的所有更改都在insert
函数的本地进行,并且在必要时不会更新列表的开头指针。
您的函数签名应该已经将指针传递给节点指针:
void insert(person **p, const char *name, int age);
并这样称呼它:
person *head = NULL;
insert(&head, "Betty", 26);
insert(&head, "Ralph", 23);
insert(&head, "Chuck", 19);
insert(&head, "Alice", 42);
insert(&head, "Simon", 34);
输入功能时, p
是调用函数中head
的地址。 当您遍历列表时
p = &(*p)->next;
*p
保留前一个节点的next
指针的地址。 p
是“因此”指针:它保存指向您正在处理的ode的指针的地址。 这意味着空列表不再是一种特殊情况。
您的函数需要返回新的头指针。 忘记分配它很容易,它也增加了呼叫的冗余度。 指针到指针方法也可以解决此问题。
这是使用将指针的指针作为参数的函数的插入代码的样子:
struct person {
const char *name;
int age;
person *next;
};
int compare(const person *p1, const person *p2)
{
return strcmp(p1->name, p2->name);
}
person *person_new(const char *name, int age)
{
person *p = malloc(sizeof(*p));
p->name = name;
p->age = age;
p->next = NULL;
return p;
}
void insert(person **p, const char *name, int age)
{
person *pnew = person_new(name, age);
while (*p && compare(*p, pnew) < 0) {
p = &(*p)->next;
}
pnew->next = *p;
*p = pnew;
}
在这里,我找到了这个问题的最有用答案: http : //www.mvps.org/user32/linkedlist.html
ptr2ptr = &people;
while ( *ptr2ptr!=NULL && compare_people(*ptr2ptr,p) ) {
ptr2ptr = &(*ptr2ptr)->next;
}
p->next = *ptr2ptr;
*ptr2ptr = p;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.