It seems in the web I can mostly find examples of an array of linked list but never of a linked list of arrays. Most linked list examples I find are just with one variable and I'm trying to create one with multiple variables. Below is my professors code which I'm trying to manipulate to take in an integer array. Anyone mind helping me or pointing me to the right direction?
EDIT: Changed code but does not print. Am I implementing the array correctly to the linked list?
#include <stdio.h>
#include <stdlib.h>
struct node {
int* data;
struct node *next;
};
struct node* Addrear(struct node *list, int* d) ;
void PrintList(struct node *list);
int main( ) {
struct node *pList=NULL;
int number[4];
number[0] = 1;
number[1] = 2;
number[2] = 3;
number[3] = 4;
pList = Addrear( pList, number );
// Print everything out.
printf("items in linked list\n");
PrintList (pList);
system("PAUSE");
return 1;
}
// Pre-condition: list points to the head of a linked list.
// Post-condition: returns a pointer to the new head of a linked list that
// adds a node with d to the end of the original list.
struct node* Addrear(struct node *list, int* d) {
struct node *pNew = NULL;
struct node *current = NULL;
// Create the new node.
pNew = (struct node*)malloc(sizeof(struct node));
memcpy(pNew->data, d, 4*sizeof*d);
pNew->next = NULL;
// Store front of the linked list
current = list;
// if list is empty then this becomes the first node.
if (list == NULL)
return pNew;
// Iterate so that current points to the last node in the list.
while (current ->next != NULL)
current = current->next;
// Link the last node to the new node.
current->next = pNew;
// Return a pointer to the edited list.
return list;
}
// Pre-condition: list points to the head of a linked list.
// Post-condition: Prints out each item in the list pointed to by list, in order.
void PrintList( struct node *list) {
// Iterate through each item and print it!
while (list != NULL) {
printf("-->%d", list->data);
list = list->next;
}
printf("\n");
}