C - 添加功能不是任何新节点或显示功能仅显示第一个节点信息



enter code here在主函数中,我调用 add 函数和显示函数来查看所有当前节点及其存储到链表中的数据。但每次它只向我显示第一个节点值,而不是其他节点。我在代码中找不到任何错误。任何身体都可以帮助...

这是我的代码:-

struct student* add(struct student*);
struct student* search(struct student*);
struct student* modify(struct student*);
void display(struct student* head);
struct student
{
int roll;
char name[50];
float percentage;
struct student *address;
};
int nodes=0;//nodes variable keeps the data of the total no of nodes present in to the inked list.
int main()
{
struct student *head;
int choice;
char mychoice='y';
head=NULL;
do
{
printf("Enter 1 to add a node.nEnter 2 to display all existing record.nEnter 3 to search a record.nEnter 4 to modify an existing record.nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=add(head);
display(head);
//break;
case 2:
display(head);
break;
case 3:
head=search(head);
break;
case 4:
head=modify(head);
break;
default:
printf("Enter any of the correct input.n");
break;
}
printf("nDo you want to continue? [Y/N]");
scanf(" %c",&mychoice);
}while(mychoice=='y'||mychoice=='Y');
return 0;
}
struct student* add(struct student* head)
{
struct student *node,*temp;
node=(struct student*)malloc(sizeof(struct student));
temp=head;
printf("Enter the name of the student: ");
scanf("%s",&(node->name));
printf("Enter the roll of the student: ");
scanf("%d",&(node->roll));
printf("Enter the percentage: ");
scanf("%f",&(node->percentage));
node->address=NULL;
if(head==NULL) // Implies an empty list.
head=node;
else
{
temp=head;
while(temp!=NULL)//Traversing to the last node.
temp=temp->address;
temp=node;
}
nodes++;
return head;
}
struct student* search(struct student* head)
{
int m;
printf("Enter the no of node you wanna search: ");
scanf("%d",&m);
if(m>nodes)
printf("%dth node does not exist.",m);
else
{
for(i=0;i<m;i++)// Traversing node to node to go to the mth node.
temp=temp->address;
printf("nThe name of the student is: %snThe roll no of the student is: %d nThe percentage of the student is: %5.2f nn",temp->name,temp->roll,temp->percentage);            
}
return head;
}
struct student* modify(struct student* head)
{
int m,i;
struct student *temp;
temp=head;
printf("Enter the index no of node you wanna change: ");
scanf("%d",&m);
if(m>nodes)
printf("%dth node does not exist.",m);
else
{
for(i=0;i<m;i++)// Traversing node to node to go to the mth node.
temp=temp->address;
printf("Enter the new name of the student: ");
scanf("%s",&(temp->name));
printf("Enter the new roll of the student: ");
scanf("%d",&(temp->roll));
printf("Enter the new percentage: ");
scanf("%f",&(temp->percentage));
}
return head;
}
void display(struct student* head)
{
struct student *temp;
temp=head;
while(temp!=NULL)
{
printf("nThe name of the student is: %snThe roll no of the student is: %d nThe percentage of the student is: %5.2f nn",temp->name,temp->roll,temp->percentage);
temp=temp->address;
}
}

对于add函数,您的问题在这里:

temp=head;
while(temp!=NULL)//Traversing to the last node.
temp=temp->address;
temp=node;

您永远不会使当前最后一个节点指向新节点。您要做的是将新节点分配给temp然后返回。返回时temp超出范围,新节点丢失。

若要插入新节点,需要更新最后一个节点的address。在伪代码中,您需要执行以下操作:

// Pseudo code to insert a new tail node
current_last_node->address = new_node;

尝试这样的事情:

temp=head;
while(temp->address!=NULL)//Traversing to the last node.
temp=temp->address;
temp->address=node;

顺便说一句:调用指向列表下一个元素的指针以进行address是不正常的。约定是使用名称next

另外两条评论:

1(由于您要添加到列表的末尾,因此最好有一个tail指针以获得更好的性能。

2(你应该收集headnodestail到一个结构中。

像这样:

struct student_list
{
struct student *head;
struct student *tail;
int nodes;
};
int main()
{
struct student_list list = {NULL, NULL, 0};
add(&list);
...
...
}

那么add函数将是:

void add(struct student_list* list)
{
struct student *node;
node=malloc(sizeof *node);
// Read data into node
node->address=NULL;
if(list->head==NULL) // Implies an empty list.
{
list->head=node;
}
else
{
list->tail->address=node;
}
list->tail=node;
list->nodes++;
}

现在函数中没有循环,所以添加元素是 O(1(,即更好的性能。

相关内容

  • 没有找到相关文章

最新更新