C 如何修复链表中传递参数地址错误
我试图建立一个链表,我想通过传递head的地址作为参数,在末尾插入一个新节点 但是,在insert函数结束后,头地址没有改变,我的链表将始终为空。 以下是插入函数:C 如何修复链表中传递参数地址错误,c,linked-list,C,Linked List,我试图建立一个链表,我想通过传递head的地址作为参数,在末尾插入一个新节点 但是,在insert函数结束后,头地址没有改变,我的链表将始终为空。 以下是插入函数: // function to insert new node after the tail void insertNode(node**headref, node**tailref, char studentNIM[], char studentName[], int attendance){ node*newNode = (nod
// function to insert new node after the tail
void insertNode(node**headref, node**tailref, char studentNIM[], char studentName[], int attendance){
node*newNode = (node*)malloc(sizeof(node));
printf("%p\n%p\n", *headref, *tailref);
node*last = *headref;
strcpy(newNode->studentName, studentName);
strcpy(newNode->studentNIM, studentNIM);
newNode->attendace = attendance;
newNode->next = NULL;
if((*headref) == NULL){
*headref = newNode;
*tailref = newNode;
printf("%p\n%p\n", *headref, *tailref);
printf("%s", newNode->studentName);
return;
}
while(last->next != NULL)
last = last->next;
last->next = newNode;
*tailref = newNode;
printf("%s", newNode->studentNIM);
}
这是我的全部代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>
typedef struct data{
char studentNIM[13];
char studentName[33];
int attendace;
struct data*next;
}node;
//function prototypes
void printList(node*head);
void clear();
void enterCheck();
void addNode(node**headref, node**tailref);
void insertNode(node**headref, node**tailref, char studentNIM[], char studentName[], int attendance);
bool digitCheck(char studentNIM[]);
int main(){
int choice;
node*head = NULL;
node*tail ;
printf("%p\n%p\n", head, tail);
do{
printf("\t\tStudent Attendance\n");
printf("\t\t==================\n\n");
printf("1. Add new student attendance.\n");
printf("2. View student attendance.\n");
printf("3. Exit.\n\n");
printf("Choose menu: ");
scanf("%d", &choice);
fflush(stdin);
if(choice == 1)
addNode(&head, &tail);
printf("%p\n%p\n", head, tail);
if(choice == 2)
printList(head);
if(choice == 3)
return 0;
}while(choice != 3);
}
void addNode(node**headref, node**tailref){
char studentName[33];
int attendance;
char studentNIM[13];
node*head = *headref;
node*tail = *tailref;
printf("%p\n%p\n", head, tail);
do{
printf("Enter student name(3 - 30 characters): ");
fgets(studentName, 33, stdin);
fflush(stdin);
}while(strlen(studentName) < 4 || strlen(studentName) > 31);
do{
printf("Enter student NIM(in number with length of 10 characters): ");
fgets(studentNIM, 13, stdin);
fflush(stdin);
}while(digitCheck(studentNIM) == false || strlen(studentNIM) != 11);
printf("Enter number of presence: ");
scanf("%d", &attendance);
fflush(stdin);
insertNode(&head, &tail, studentNIM, studentName, attendance);
}
bool digitCheck(char studentNIM[]){
int i;
for(i = 0;i < strlen(studentNIM) - 1;i++){
if(isdigit(studentNIM[i]) == false)
return false;
}
return true;
}
// function to insert new node after the tail
void insertNode(node**headref, node**tailref, char studentNIM[], char studentName[], int attendance){
node*newNode = (node*)malloc(sizeof(node));
printf("%p\n%p\n", *headref, *tailref);
node*last = *headref;
strcpy(newNode->studentName, studentName);
strcpy(newNode->studentNIM, studentNIM);
newNode->attendace = attendance;
newNode->next = NULL;
if((*headref) == NULL){
*headref = newNode;
*tailref = newNode;
printf("%p\n%p\n", *headref, *tailref);
printf("%s", newNode->studentName);
return;
}
while(last->next != NULL)
last = last->next;
last->next = newNode;
*tailref = newNode;
printf("%s", newNode->studentNIM);
}
void printList(node*head){
if(head == NULL){
printf("No data.\n");
return;
}
谁能帮我找出我做错了什么
但是,在insert函数结束后,头地址没有改变,我的链表将始终为空
原因是您使用错误的参数调用insertNode
你可以:
insertNode(&head, &tail, studentNIM, studentName, attendance);
但是head是函数addNode中的一个局部变量,因此它不能在main中更改head。换句话说,对insertNode的调用只会更改addNode:head,而不会更改main:head
也许你想做:
insertNode(headref, &tail, studentNIM, studentName, attendance);
^^^^^^^
检查这个链接,先生,你已经有一个关于尾巴的参考了,从头到尾的迭代是没有意义的!!您所需要的只是如果*headref==NULL*headref=newnode;else*tailref->next=newnode*tailref=newnode;无关,无论文本/站点/讲师告诉您什么;是如何清除stdin缓冲区的,他们错了。fflush是支持输出的流上唯一的标准支持。好消息是:这段代码中的大多数代码都毫无价值,即使它们确实如您所期望的那样工作,所以花点时间去除它们,并通过换行符在一些真正需要的地方正确地使用和丢弃stdin。更重要的是,addNode中的head和tail是毫无价值的。OP应该去掉它们,然后修复代码。