我必须做一个链接列表程序,有很多功能,比如删除,添加和修改数字。我的功能不记忆链接列表头
在我的代码中,当我在选择1的函数中放入一个数字后,当我想要显示所有数字时,我将头放在参数中,但是在Visual Studio中看到函数的参数don'没有任何东西。我可以做的,以传递参数链表的头,我的功能Displaynbr
#include <stdio.h>
#include <stdlib.h>
struct Mynbr
{
int nbr;
struct Mynbr* next;
} typedef Mynbr;
void Menu();
void choiceMenu(int choice, Mynbr* first);
Mynbr* Addnumber(Mynbr* first);
void Displaynbr(Mynbr* first);
int main(void)
{
Mynbr* head = NULL;
int choice = 0;
while (choice!=5)
{
Menu();
printf("Your choice : "); scanf("%d", &choice);
choiceMenu(choice, head);
}
system("PAUSE");
return 0;
}
void Menu()
{
printf("\n1.Add number to the list\n");
printf("2.Delete number from the list\n");
printf("3.Search number in the list\n");
printf("4.Display all the numbers from the list\n");
printf("5.Exit\n");
}
void choiceMenu(int choice, Mynbr* first)
{
switch (choice)
{
case 1:
Addnumber(first);
break;
case 2:
break;
case 3:
break;
case 4:
Displaynbr(first);
break;
case 5:
break;
}
}
Mynbr* Addnumber(Mynbr* first)
{
printf("\n===Function to add a number===\n");
Mynbr* head_nbr = first;
if (!head_nbr)
{
head_nbr = (Mynbr*)malloc(sizeof(Mynbr));
printf("Enter a number :"); scanf("%d", &(head_nbr->nbr));
head_nbr->next = NULL;
}
return head_nbr;
}
void Displaynbr(Mynbr* first)
{
printf("\n===Function to display number===\n");
Mynbr* curr = first;
if (curr->next)
{
printf("The number is : %d", curr->nbr);
Displaynbr(first->next);
}
}
C11草案标准n1570:* 6.5.2.2函数调用4参数可以是任何完整对象类型的表达式。在为函数调用 做准备时,将对参数进行评估,并为每个参数分配相应参数的值。 93)函数可能会改变其参数的值,但这些改变不会影响参数的值。* tl; dr:C是按值传递的值。 – EOF
题外话:你不需要投入malloc的返回 – sokkyoku