【问题标题】:Creating and adding data in a linked Llist在链表中创建和添加数据
【发布时间】:2015-04-16 19:39:21
【问题描述】:

我已经开始学习链接列表,通过视频和多个示例,我已经非常了解链接列表是什么以及它如何在现实生活中进行类比。但是当涉及到编码时,我会迷失方向,我想通过所有我有点困惑的指针,我花了一些时间来更好地掌握数组,所以我认为它与链接列表相同。所以这是我的代码

/*
•   The program will use dynamic memory to create a singly linked list(NO ARRAYS PERMITTED)
•   The program will store unlimited number of student records(limited only by RAM).
•   A student record will consist of Student Name, Age, and GPA…you may need to add additional fields to make this work(Next).
•   The program will have a way for the user to add records(in order by name).You can assume that no two students have the same name.The list will always be in order.
•   The program will have a way for the user to display ALL records.
•   The program needs a way to quit.
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable: 4996)// disables warning
typedef struct{
    char name[40];
    int age;
    float gpa;
    struct NODE* next;
}NODE;
void addStudent();

int main(void){
    NODE* head = NULL;
    int userinput;
    printf("       **********************************\n");
    printf("       *        MENU                    *\n");
    printf("       *  1. Add Student                *\n");
    printf("       *  2. Display all student records*\n");
    printf("       *  3. Quit                       *\n");
    printf("       **********************************\n");
    scanf_s("%d%*[^\n]", &userinput); '\n' == getchar();
    switch (userinput)
    {
    case 1: do
    {
        addStudent(head);
        printf("Add another record? 1(y) 2(n)\n");
        scanf_s("%d%*[^\n]", &userinput); '\n' == getchar();
    } while (userinput == 1);
    break;

    }





    }

void addStudent(NODE* head){

    head = malloc(sizeof(NODE));
    if (head == NULL)
    {
        return;
    }
    NODE * current = head;
    printf("Please Enter student name:\n");
    fgets(current->name, 40, stdin);
    printf("Enter student age:\n");
    scanf("%d%*[^\n]", &current->age); '\n' == getchar();
    printf("Enter student gpa:\n");
    scanf("%f%*[^\n]", &current->gpa); '\n' == getchar();
    current->next;
    current->next = NULL;

    while (current != NULL){
        current = head;
        printf("%s\n", current->name);
        printf("%d\n", current->age);
        printf("%0.2f\n", current->gpa);
        current = current->next;
    }
}

当我编译时,它总是会打印我认为它的头部,因为 current = head 在 while 循环中,我理解为什么它打印头部但我不知道如何安排这段代码这样我就可以创建一个当我通过循环添加和打印所有节点时的新节点。

【问题讨论】:

    标签: c arrays data-structures linked-list


    【解决方案1】:

    问题是您永远不会创建新节点来添加到列表中,而总是只是更新头部。为了使其正常工作,您应该:

    1. 分配一个新的NODE

      NODE *newNode = malloc(sizeof(NODE));
      
    2. 将数据加载到该节点中

      printf("Please Enter student name:\n");
      fgets(&newNode->name, 40, stdin);
      printf("Enter student age:\n");
      scanf("%d%*[^\n]", &newNode->age); '\n' == getchar();
      printf("Enter student gpa:\n");
      scanf("%f%*[^\n]", &newNode->gpa); '\n' == getchar();
      
    3. 更新节点指向HEAD当前指向的节点

      newNode->next = head
      
    4. 更新头部以指向新节点

      head = newNode;
      

    【讨论】:

    • 第 3 步和第 4 步是我遇到问题的地方,newNode-&gt;next = head 不是将新节点指向头部,然后 head = newNode; 将 newnode 设置为头部吗?这就是您将节点添加到列表开头的方式吗?如果我想在列表中添加更多内容,最终会变成尾巴怎么办?
    • 在当前实现中,您确实将新节点添加到列表的开头(对于单链表可以用作堆栈实现)。如果您想将节点添加到列表的末尾(类似列表的队列),那么您还必须存储指向列表的最后一个元素(尾部)的指针,并将该元素链接到您的新元素'想添加。
    • 好的,我明白了,那么如何在列表中间的其他位置添加项目?
    • [a]新建一个节点N,[b]找到节点X,在该节点之后应该添加新节点;假设节点 X 当前连接到 Y,[c] 将 N 链接到 Y(即N-&gt;next = X-&gt;next),[d] 将 X 链接到 N(即 @987654329 @)。
    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2015-05-12
    • 1970-01-01
    相关资源
    最近更新 更多