简体   繁体   中英

What is wrong with my linked list, and why won't it properly set my head to NULL in C?

This is my program I have written in C, I created a structure and a head and I am trying to make a linked list, but I keep getting read access violations and it seems I am not passing in my head pointer properly and it keeps having problems when it is trying to add to my list.

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

How can I get it so I can properly add nodes to this list?

I just want it to add nodes to the list with the character, float and int. I tried messing with the pointers, I tried setting head first and setting head->next to null as well but nothing seems to work. It keeps having errors every time it tries to deal with the null.

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

addLast wants a pointer to pointer (read Is it a good idea to typedef pointers? ), but you pass a single pointer here:

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

switch to

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

And here:

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);

sizeof(c) is the size of a pointer (read What is 'array decaying' in C? ).

Use the size of the member, in this case MAX :

     memset(p->model, '\0', MAX);

or better yet: delete the whole line, you don't need it if you call strcpy on the next line.

One more:

void printTotalMiles(NODEPTR* list) {

differs from the propotype:

void printTotalMiles(NODEPTR);

Compile with warnings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM