【问题标题】:What is wrong with my linked list, and why won't it properly set my head to NULL in c?我的链接列表有什么问题,为什么它不能在 c 中正确地将我的头设置为 NULL?
【发布时间】:2022-12-15 14:06:31
【问题描述】:

这是我用 c 编写的程序,我创建了一个结构和一个头,我正在尝试制作一个链表,但我不断遇到读取访问冲突,而且似乎我没有正确传递我的头指针并且它一直有当它试图添加到我的列表时出现问题。


#define _CRT_SECURE_NO_WARNINGS // Since I want to strictly use ANSI C and not Microsoft C without getting the warning message, I'm adding this line of code before I include header files.
#include <stdio.h>              // "#include" includes the contents of another file, commonly called header file, into the source code file.
#include <string.h>             // This library contains a variety of functions to manipulate strings.
#include <stdlib.h>             // Header file which has the necessary information to include the input/output related functions in our program.
#define MAX 100

typedef struct node {
    char model[MAX];
    float price;
    int miles;
    struct node* next;
} *NODEPTR;

NODEPTR getNode();
void freeNode(NODEPTR p);
void printTotalMiles(NODEPTR);
void addLast(NODEPTR* list, char c[], float pri, int num);


int main(void) { //It is the first function of every C program that is responsible for starting the execution and termination of the program. 

    int i = 0;
    NODEPTR head = NULL;
    if (head == NULL) {
        printf("NULL");
    }
    //head = (NODEPTR) malloc(sizeof(struct node));
    //head->next = NULL;
    //addFront(head, 2600.00, 48000);
    //addFront(head, 1400.00, 22000);
    //printf("first, %d", head->price);
    addLast(head, "64 Impala", 1800.00, 12000);
    addLast(head, "56 Ford", 500.00, 23000);
    //printTotalMiles(head);


    //printArray(p);

    return 0; // This statement indicates "main()" is returning the value 0 upon completion.
} //  Curly brace marks the end of the function.


NODEPTR getNode(void) {
    NODEPTR p;

    p = (NODEPTR)malloc(sizeof(struct node));
    if (p == NULL) {
        printf("List Overflow.");
    }
    return (p);
}

void freeNode(NODEPTR p) {
    free(p);
}

void addFront(NODEPTR* list, float pri, int num) {
    NODEPTR p, q;

    p = getNode();
    //strcpy(p->model, c);
    // memset(p->model, '\0', sizeof(c))
    //printf("%s\n", p->model);
    p->price = pri;
    p->miles = num;
    p->next = *list;
    *list = p;

    q = *list;
    printf("hey %.2f hey\n", q->price);
}

void printTotalMiles(NODEPTR* list) {
    int total = 0;
    NODEPTR p;
    while (*list) {
        p = *list;
        printf(" Car: \tPrice: %.2f\tI drove it: %d\n", p->price, p->miles);
        total += p->miles;
        list = p->next;
    }
    printf("The Total Miles: %d", total);
}


void addLast(NODEPTR* list, char c[], float pri, int num) {
    NODEPTR p, q;

    p = getNode();
    memset(p->model, '\0', sizeof(c));
    strcpy(p->model, c);
    p->price = pri;
    p->miles = num;
    p->next = NULL;
    if (*list == NULL) {
        *list = p;
    }
    else {
        q = *list;
        while (q->next) {
            q = q->next;
        }
        q->next = p;
    }
}

//void printArray(struct node cars[]) { //function definition
//    break;
//}

我怎样才能得到它,以便我可以正确地将节点添加到这个列表中?

我只希望它使用字符、浮点数和整数将节点添加到列表中。我尝试弄乱指针,我尝试先设置 head 并将 head->next 设置为 null 但似乎没有任何效果。每次尝试处理 null 时,它都会出错。

【问题讨论】:

    标签: c function linked-list reference null


    【解决方案1】:
    void addLast(NODEPTR* list, char c[], float pri, int num);
    

    addLast 想要一个指向指针的指针(读作 Is it a good idea to typedef pointers?),但是你在这里传递了一个指针:

    addLast(head, "64 Impala", 1800.00, 12000);
    addLast(head, "56 Ford", 500.00, 23000);
    

    切换到

    addLast(&head, "64 Impala", 1800.00, 12000);
    addLast(&head, "56 Ford", 500.00, 23000);
    

    和这里:

    void addLast(NODEPTR* list, char c[], float pri, int num) {
        NODEPTR p, q;
    
        p = getNode();
        memset(p->model, '
    猜你喜欢
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多