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?