如何修复链表中的传递参数地址错误

时间:2019-06-29 04:04:27

标签: c linked-list

我正在尝试创建一个链表,我想通过将head地址作为参数传递到最后插入一个新节点。

但是,插入函数结束后,头地址没有更改,并且我的链接列表将始终为NULL。 这是插入函数:

// 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;
}

有人可以帮我弄清楚我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

  

但是,在插入函数结束之后,头地址没有更改,并且我的链接列表将始终为NULL。

原因是您使用错误的参数调用MyBean

您这样做:

insertNode

但是insertNode(&head, &tail, studentNIM, studentName, attendance); 是函数head中的 local 变量,因此它无法更改addNode中的head。换句话说,您对main的呼叫只会更改insertNode-不会更改addNode:head

也许您想做:

main:head