在链表中通过引用变量使用指针
Using a pointer by reference variable in linked lists
如何在 take_input() 函数中引用 tail 变量以使其反映在 main 中?
下面是 main() 和 take_input() 函数
在这里,我试图通过引用 [ **tail ]
我如何在链表的 take_input() 函数中引用它 { *tail is causing an error }
非常感谢任何帮助!!!!
node* take_input(node **tail)
{
node* head = NULL;
int count = 0;
string name;
cout << "Enter the name of the president";
cin >> name;
while(!name.empty())
{
count++ ;
node *new_node = new node(name);
if(head == NULL)
{
head = new_node;
*tail = new_node;
}
else
{
*tail->next = new_node ;
*tail= *tail->next;
}
cout << "Enter the name of a member , the secretary or NULL to exit";
cin >> name ;
}
return head;
}
int main()
{
int n ;
string name;
node *tail = NULL;
node *head = take_input(&tail);
cin >> n;
cin >> name;
node *headfinal = insert_node(head, n ,name);
cout<< "\n Enter the member's name which needs to be deleted\n";
string del;
cin >> del;
node *head_final2 = delete_node(headfinal,del, &tail);
node *temp2 = head_final2 ;
while(temp2 != NULL)
{
cout << temp2->name;
temp2 = temp2->next;
}
这是我目前面临的错误:
表达式必须有一个指向 class 类型的指针 (c/c++) (131,5)
正如 Thomas Sabnik 在评论中的回答:
->
的优先级高于 *
。您必须使用括号:
将 *tail->next = new_node;
更改为 (*tail)->next = new_node;
并且
*tail = *tail->next;
到 *tail = (*tail)->next;
.
如何在 take_input() 函数中引用 tail 变量以使其反映在 main 中?
下面是 main() 和 take_input() 函数
在这里,我试图通过引用 [ **tail ]
我如何在链表的 take_input() 函数中引用它 { *tail is causing an error }
非常感谢任何帮助!!!!
node* take_input(node **tail)
{
node* head = NULL;
int count = 0;
string name;
cout << "Enter the name of the president";
cin >> name;
while(!name.empty())
{
count++ ;
node *new_node = new node(name);
if(head == NULL)
{
head = new_node;
*tail = new_node;
}
else
{
*tail->next = new_node ;
*tail= *tail->next;
}
cout << "Enter the name of a member , the secretary or NULL to exit";
cin >> name ;
}
return head;
}
int main()
{
int n ;
string name;
node *tail = NULL;
node *head = take_input(&tail);
cin >> n;
cin >> name;
node *headfinal = insert_node(head, n ,name);
cout<< "\n Enter the member's name which needs to be deleted\n";
string del;
cin >> del;
node *head_final2 = delete_node(headfinal,del, &tail);
node *temp2 = head_final2 ;
while(temp2 != NULL)
{
cout << temp2->name;
temp2 = temp2->next;
}
这是我目前面临的错误:
表达式必须有一个指向 class 类型的指针 (c/c++) (131,5)
正如 Thomas Sabnik 在评论中的回答:
->
的优先级高于 *
。您必须使用括号:
将 *tail->next = new_node;
更改为 (*tail)->next = new_node;
并且
*tail = *tail->next;
到 *tail = (*tail)->next;
.