C 在链表add函数中传递值
在过去的2.5个小时里,我一直在创建这个链表,并试图理解为什么它没有将内存地址传递给链表的开头。在我开始学习java中的数据结构之前,我试图理解C中的链表。我研究了其他问题,我不明白为什么它不起作用。请原谅我的评论,我一直在努力理解一切。提前感谢您的时间和帮助 head变量在新赋值后等于NULL head=addFamMember(空);在主线程中。但我可以通过打印add函数的成员(名称、年龄和下一个指针)看到内存已在add函数中分配。以下是输出:C 在链表add函数中传递值,c,function,linked-list,pass-by-value,pass-by-pointer,C,Function,Linked List,Pass By Value,Pass By Pointer,在过去的2.5个小时里,我一直在创建这个链表,并试图理解为什么它没有将内存地址传递给链表的开头。在我开始学习java中的数据结构之前,我试图理解C中的链表。我研究了其他问题,我不明白为什么它不起作用。请原谅我的评论,我一直在努力理解一切。提前感谢您的时间和帮助 head变量在新赋值后等于NULL head=addFamMember(空);在主线程中。但我可以通过打印add函数的成员(名称、年龄和下一个指针)看到内存已在add函数中分配。以下是输出: Enter command to add, p
Enter command to add, print, or quit: add
Enter name and age: brett 28
Added:brett Age:28 POINTING to:(null)
Enter command to add, print, or quit:
代码是这样的:我留下他的评论可能有助于描述我的想法,并指出我错在哪里
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct S_Family{
char name[16];
int age;
struct S_Family *next;
}Family;
//prototypes
Family *addFamMember (Family *previous);
void CleanUp(Family *start);
void PrintList(Family *start);
int main(){
setvbuf(stdout, NULL, _IONBF, 0);
printf("Enter command to add, print, or quit: ");
char input[16]; //fgets var to store input
char command[16]; //sscanf var to store read info from input
Family *head = NULL; //For a linked list we need to set up the first node and point it NULL
Family *newest = NULL; //We also need a pointer dedicated to updating the latest created node
//This while loop will continue to get input until the command "quit" is typed
//It includes the functionality of printing the list and adding new nodes
while( fgets(input, 15, stdin)){
sscanf(input, "%s", command);
if ( strcmp(command, "quit") == 0) {
printf("\n\nBreaking.........");
break;
} else if ( strcmp(command, "print") == 0) {
PrintList(head);
} else if ( strcmp(command, "add") == 0) {
if ( head = NULL){
head = addFamMember(NULL); //If there are no nodes give head a memory address so now we do (recursion somewhat?)
printf("head:%s ", head->name); //this doesn't print!! Head = NULL above for some reason.
newest = head; //newest cannot stay equal to NULL. this allows us to pass it as a param to add function w/out the start being NULL anymore
printf("newest:%s ", newest->name);
} else {
newest = addFamMember(newest); //Recursion where the new node gets a mem address and gets the previous node as guide to cont. the list
} //now we go to the add function
}
printf("\nEnter command to add, print, or quit: ");
}
CleanUp(head);
return 0;
}
/*We want to return a new family member structure
so we start of with a Family return type. The param
as mentioned before needs to be a pointer to the address
of the previous node. That node will be pushed away from the
NULL in a singly or doubly linked list */
Family *addFamMember (Family *previous) {
/*Now we need to get the member variable info for that newFamMember
and store into the newly created data structure newFamMember*/
char input[16];
printf("Enter name and age: ");
fgets(input,15, stdin);
Family *newFamMember = malloc(sizeof(Family)); //create new address for newFamMember
sscanf(input, "%s %d", newFamMember->name, &newFamMember->age); //takes the input (first a string then a integer) and stores it into its proper place
printf("Added:%s Age:%d POINTING to:%S \n\n",newFamMember->name,newFamMember->age, newFamMember->next->name);
newFamMember->next = NULL; //initialize it's pointer member var but more importantly maintains the linked list by pointing to null.
/*Now we tell the computer what to do if this isn't the first node
or not. If it is then there isn't a previous node so there is no
way to set any other nodes' pointers to point to something else*/
if ( previous != NULL){
previous->next = newFamMember; //if previous is not equal to NULL set the previous' next pointer to newFamMember
printf("previous:%s ", previous->next->name);
}
return newFamMember; //we always want to return a newly added family member. That's this function's purpose
}
/*now we can print the list*/
void PrintList (Family *head) { //start is a pointer so we can pass the value of start
Family *currentMember = head; //we create currentMember and set it equal to start so we can iterate through the list and print each one
int count = 0;
if (currentMember == NULL){
printf("There are no family members\n");
}
while (currentMember != NULL) {
count++;
printf("\n\nmember:%d Name:%s Age:%2d POINTING TO:%s\n",
count, currentMember->name,
currentMember->age,
currentMember->next->name);
currentMember = currentMember->next; //move to the next node in the list headed towards NULL
}
}
void CleanUp(Family *head){
Family *freeMe = head;
Family *holdMe = NULL;
while(freeMe != NULL) {
holdMe = freeMe->next;
printf("\nFree Name:%s Age:%d\n",
freeMe->name,
freeMe->age);
free(freeMe);
freeMe = holdMe;
//PrintList(*start);
}
}
在“添加”部分,如果(head==NULL),则很可能是
,即比较而不是赋值。不幸的是,这不会改变任何事情。我也试过了@Moehm真的@Moehm?您更改了PrintList函数中的if(head==NULL)?Notepad++只是文本编辑器。警告来自编译器;它不会给你警告。您正在使用编译器进行编译;这应该给你一些警告。了解如何打开它们,然后打开它们并注意警告。将警告视为错误-C编译器比您在现阶段更了解C。