【问题标题】:How to fix passing parameter address error in linked list如何修复链表中的传递参数地址错误
【发布时间】:2019-06-29 04:04:27
【问题描述】:

我正在尝试创建一个链表,我想通过传递 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;
}

谁能帮我弄清楚我做错了什么?

【问题讨论】:

  • 你已经引用了tail,从头到尾迭代是没有意义的!!
  • 您只需要if (*headref == NULL) *headref = newnode; else (*tailref)-&gt;next = newnode; *tailref = newnode;
  • 无关,无论文本/站点/讲师告诉您fflush(stdin); 是如何清除stdin 缓冲区,他们都错了。 fflush 仅在启用 output 的流上受标准支持。好消息:不管怎样,这段代码中的大多数都是毫无价值的,即使它们确实按你期望的那样工作,所以花点时间摆脱它们并通过消耗和丢弃正确清理stdin在少数地方通过换行符确实是需要的。

标签: c linked-list


【解决方案1】:

但是,插入函数结束后,头地址没有改变,我的链表永远是NULL。

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

你这样做:

insertNode(&head, &tail, studentNIM, studentName, attendance);

head 是函数addNode 中的本地 变量,因此它不能更改main 中的head。换句话说,您对insertNode 的调用只会改变addNode:head - 而不是main:head

也许你想做:

insertNode(headref, &tail, studentNIM, studentName, attendance);
           ^^^^^^^

【讨论】:

  • 更重要的是,addNode 中的headtail 毫无价值。 OP 应该摆脱它们并在此后修复代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多