0

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");
}
Yitzak Hernandez
  • 355
  • 4
  • 23
  • Basically how to create a linked list filled with an array – Yitzak Hernandez Oct 19 '15 at 03:07
  • yes, but does this not compile, give unexpected results. whats the actual issue with the code posted – amdixon Oct 19 '15 at 03:11
  • enqueue , dequeue operations on link list ? – ameyCU Oct 19 '15 at 03:15
  • Alright I changed but it still doesn't compile (I'm working with a queue linked list). Basically I'm stuck trying to figure out on how to implement an array into a linked list. – Yitzak Hernandez Oct 19 '15 at 03:18
  • `memcpy(pNew->data, d, 4*sizeof*d);`. That will cause undefined behaviour. `pNew->data` is an unintialised pointer. You need to either make it a static array or dynamically allocate memory for it. – kaylum Oct 19 '15 at 03:20
  • I thought the malloc on pNew->data was the dynamic memory for it? I'm still learning linked lists :/ – Yitzak Hernandez Oct 19 '15 at 03:23
  • 1
    It would be if you had a `malloc` that is assigned to `pNew->data`. But you don't. You have a `malloc` that is assigned to `pNew`. – kaylum Oct 19 '15 at 03:25
  • Ah I see, ty! What about printing the list now? – Yitzak Hernandez Oct 19 '15 at 03:28
  • `printf("-->%d", list->data);`. That needs to be in a `for` loop printing each element in the array: `for (i = 0; i < 4; i++) printf("%d ", list->data[i]);` – kaylum Oct 19 '15 at 03:42

0 Answers0