【问题标题】:add function is not any new node or display function is showing none but the first node info only添加功能不是任何新节点或显示功能仅显示第一个节点信息
【发布时间】:2019-12-15 17:55:24
【问题描述】:

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

这是我的代码:-

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: %s\nThe roll no of the student is: %d \nThe percentage of the student is: %5.2f \n\n",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: %s\nThe roll no of the student is: %d \nThe percentage of the student is: %5.2f \n\n",temp->name,temp->roll,temp->percentage);
        temp=temp->address;
    }
}

【问题讨论】:

  • 将头部作为双指针从 main 传递到 add 函数。否则,它内部所做的任何更改都将只是本地的,不会被反映回来
  • 如果我全局声明头部并调用没有返回类型为 void 的对象的函数......
  • 其实我试过了,但是当我声明 head=NULL;全球范围内。它给了我一个错误,程序没有执行。
  • 首先,贴出的代码无法编译!它不编译的第一个原因是 struct student 在定义之前就被引用了。它无法编译的第二个原因是它缺少所需的头文件所需的 #include 语句。 (您是否希望我们猜测您的代码实际包含哪些头文件?请发minimal reproducible example,以便我们帮助您调试代码
  • 如果head不会改变,那为什么head=search(head);

标签: c linked-list pass-by-reference singly-linked-list


【解决方案1】:

对于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

另外两个cmets:

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),即更好的性能。

【讨论】:

  • 感谢您的建议。您以嵌套方式呈现两个结构的方式对我来说是相当新的。但我仍然理解这个想法。尽管考虑到更好的性能,但使用无循环概念很酷。您能否简要解释一下最后两行...关于这两行... else { list->tail->address=node; } 列表->尾=节点;列表->节点++;我已经理解了 if 部分。这是没有节点连接到列表的第一种情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
  • 2014-08-06
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多