在链表中创建和添加数据
Creating and adding data in a linked Llist
我已经开始学习链表,通过视频和多个示例,我已经非常了解链表是什么以及它如何在现实生活中进行类比。但是当涉及到编码时我迷路了我想通过所有我有点困惑的指针,我花了一点时间来更好地掌握数组所以我认为它与链接列表相同。所以这是我的代码
/*
• 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]", ¤t->age); '\n' == getchar();
printf("Enter student gpa:\n");
scanf("%f%*[^\n]", ¤t->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 循环中,我明白为什么它会打印头部,但我不知道如何安排这段代码所以当我通过循环添加和打印所有节点时,我可以创建一个新节点。
问题是您永远不会创建新节点来添加到列表中,而总是只更新头部。为了使其正常工作,您应该:
分配一个新的NODE
,
NODE *newNode = malloc(sizeof(NODE));
将数据加载到该节点
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();
更新节点指向HEAD当前指向的节点
newNode->next = head
更新头部指向新节点
head = newNode;
我已经开始学习链表,通过视频和多个示例,我已经非常了解链表是什么以及它如何在现实生活中进行类比。但是当涉及到编码时我迷路了我想通过所有我有点困惑的指针,我花了一点时间来更好地掌握数组所以我认为它与链接列表相同。所以这是我的代码
/*
• 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]", ¤t->age); '\n' == getchar();
printf("Enter student gpa:\n");
scanf("%f%*[^\n]", ¤t->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 循环中,我明白为什么它会打印头部,但我不知道如何安排这段代码所以当我通过循环添加和打印所有节点时,我可以创建一个新节点。
问题是您永远不会创建新节点来添加到列表中,而总是只更新头部。为了使其正常工作,您应该:
分配一个新的
NODE
,NODE *newNode = malloc(sizeof(NODE));
将数据加载到该节点
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();
更新节点指向HEAD当前指向的节点
newNode->next = head
更新头部指向新节点
head = newNode;