-2

I have a .c and a .h file. All the linked list methods are supplied by my professor. I wanted to test the linked list out by creating a main function and trying to add to the linked list and then displaying the linked list. How would I do that in the main function? Here is what I have:

int main() {
    linkedList* test = createLinkedList();
    addToLinkedList(test, value);
    displayLinkedList(test);
}

I also tried this code:

int main() {
    linkedList* hello = createLinkedList();
    struct tnode* test = "hello";
    addToLinkedList(hello, test);
    return (0);
}

However, what I have doesn't work.

Here is the code the prof gave us:

TESTlinkedlist.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TESTlinkedList.h"

/* Creates a linked list */
linkedList* createLinkedList() {
    return NULL;
}

/* Displays the linked list */
void displayLinkedList(linkedList* ll) {
    linkedList* p = ll;

    printf("[");
    while (p != NULL) {
        printf(" % d " , p -> node -> c);
        p = p -> next;
    }
    printf("]\n");
}

/* Adds a tree node to the linked list */
void addToLinkedList(linkedList** ll, tnode* val) {
    linkedList* nn = (linkedList*)malloc(sizeof(linkedList));
    nn -> node = val;
    nn -> next = NULL;

    linkedList* p = *ll;
    if (p == NULL) {
        p = nn;
    } else {
        while (p -> next != NULL) {
            p = p -> next;
        }
        p -> next = nn;
    }
}

TESTlinkedlist.h:

/* Include Guards to prevent double inclusion with include directive */
#ifndef TESTLINKEDLIST_H
#define TESTLINKEDLIST_H

/* Typedef Structures */

typedef struct tnode {
    double weight;
    int c;
    struct tNode* right;
    struct tNode* left;
    struct tNode* parent;
} tnode;

typedef struct ll {
    tnode* node;
    struct ll* next;
} linkedList;

/* Methods */

linkedList* createLinkedList();
void displayLinkedList(linkedList* ll);
void addToLinkedList(linkedList** ll, tnode* val);
void addInOrder(linkedList **ll, tnode* nn);

#endif /* LINKEDLIST_H */

Any clue how I can make a new linked list, create a t node and add it to that linked list, and then display it, given the methods that my prof put down?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Team43
  • 7
  • 1
  • 5

2 Answers2

1

The code your professor gave you is broken, for example struct tnode* test = "hello"; causes an error during compilation.

Take it back to him and fail him. While you're there, tell him return is not a function and he shouldn't cast malloc.

On another professor-malloc-related note, he should be checking the return value. For example:

linkedList* nn = malloc(sizeof *nn);
if (!nn) {
    return;
}
nn -> node = val;
nn -> next = NULL;

There is another error that you're probably both at fault for; there are two main entry points and your compiler might get confused between them. Remove one of them.

In both of those entry points, addToLinkedList is used incorrectly; the first argument is intended to be a linkedList ** where-as what is being given is a linkedList *. Perhaps (in your first main entry point) you meant to write addToLinkedList(&test, value);. Note the additional ampersand.

Community
  • 1
  • 1
autistic
  • 1
  • 3
  • 35
  • 80
  • i think the main in the code belongs to the OP – pm100 Mar 15 '16 at 00:43
  • @pm100 The OP says that code came from the professor. I would consider it quite disrespectful to call him a liar. Additionally, that entry point is different to the entry point he's given. I think that entry point came from the professor. – autistic Mar 15 '16 at 00:47
  • @pm100 Think about it, how many professors would write a module of code like this without an entry point to demonstrate it? It doesn't happen at this point in the course. It should, but it doesn't. – autistic Mar 15 '16 at 00:48
  • @Seb Nope, those were my entries. I tried two different approaches, haha. Forgot to remove that from the .c file. – Team43 Mar 15 '16 at 00:48
  • @Team43 Oh, well the fault still exists whether it's your professors fault or not, so this still remains at least a valid part of an answer. – autistic Mar 15 '16 at 00:50
  • @Seb Any clue how I could make his functions work using a main method? – Team43 Mar 15 '16 at 00:54
  • @Team43 Yes. I've modified my answer; read it in full again and fix all of those mistakes, then get back to me to let me know how you went... – autistic Mar 15 '16 at 01:02
0

the functions addToLinkedList and displayLinkedList need to get a pointer to a pointer as their fist parameter. So, the main should look like this:

int main() {
    linkedList* test = createLinkedList();
    addToLinkedList(&test, value);
    displayLinkedList(&test);
}

Hope this helps.

  • How would I create the tnode and add it into there? – Team43 Mar 15 '16 at 00:37
  • 1
    You need to create a Binary tree add function – Shahar Lahav Mar 15 '16 at 00:40
  • @Team43 The only thing the display function cares about is `c`. So you could `malloc` a node, set all the pointers to NULL, set the weight to 0, and then set `c` to whatever suits you. – user3386109 Mar 15 '16 at 00:59
  • Technically, this commits the same error but elsewhere. `void displayLinkedList(linkedList* ll)` yet `linkedList* test = ...; displayLinkedList(&test);`. – autistic Mar 15 '16 at 01:00