rybak3
rybak3

Reputation: 1

Allocating memory for a node with a struct as a value

I have thought of a couple of ways to do this but I am wondering which is correct.

The assignment requires us to traverse a 2d array (maze) and print out each coordinate of the final solution path from start to finish.

The coordinates are to be stored in a linked list that has stack operations acting upon it (push, pop, etc). Therefore, only one node at a time is being looked at (the head will point to a null node, and then values will be "pushed" in front of the initial node, then popped as is required.

My problem is the proper allocation of memory for the node.

The solution that I came up with would be generalized as:

struct coordinates{
    int x;
    int y;
};

struct linkedStruct
{
 int* elem;
 struct linkedStruct*  next;
};

typedef struct linkedStruct linked;
typedef linked* linkedPtr; 

So in main I assume I would do this

linkedPtr ptr = malloc(sizeof(linked));
ptr->elem = coordinates;

Is this correct? My first time doing linked lists.

Upvotes: 0

Views: 89

Answers (2)

Th30n
Th30n

Reputation: 303

I won't go into much details of linked list algorithms since this seems to be your homework and should probably read the book and/or course materials.

Since your element value is a struct coordinates you store a pointer to it inside your linked list node. Or to avoid having to worry as much about freeing stuff you can have a struct coordinates in a node instead of a pointer.

struct linkedStruct
{
    struct coordinates elem;
    struct linkedStruct*  next;
};

typedef struct linkedStruct linked;
typedef linked* linkedPtr;

When you add a node you need to set the next pointer to newly allocated node as you do in your code. And you set the pointer to your struct.

linkedPtr ptr = malloc(sizeof(linked));
ptr->elem.x = 3;
ptr->elem.y = 4;
// assuming you are appending to the end of linked list and last_node->next is NULL
ptr->next = NULL
last_node->next = ptr;

You also need to set the next pointer of the newly created node ptr to NULL if it is at the end (there is no next node after it). You need to cleanup the nodes with free when done. Another suggestion is to have 2 structs, one being the linked list which stores pointers to start and end of the list (for easier appending) and a struct which represents the node (with element and next pointer) as you have done. Using that suggestion the above code could be in a function:

typedef struct {
    linkedPtr *head;
    linkedPtr *last;
} linked_list; // Linked list with pointers to first and last node.

int *append(linked_list *list, int x, int y)
{
    linkedPtr ptr = malloc(sizeof(linked)); // This might fail to allocate
    if (ptr == NULL) {
        return -1; // This will be the error code for failure.
    }
    ptr->elem.x = x;
    ptr->elem.y = y;
    ptr->next = NULL;
    if (list->first == NULL) { // If list is empty.
        list->first = ptr;
        list->last = ptr;
    } else {
        list->last->next = ptr; // Point the last node to new node
        list->last = ptr; // Set the last to new last node
    }
    return 0; // Code success
}

To use append you need to have created linked_list.

linked_list *list = malloc(sizeof(linked_list)); // This might also fail.
list->head = NULL;
list->last = NULL;
if (append(list, 3, 4) == -1) {
  printf("Unable to allocate memory for appending to list\n");
}

Don't forget to free all that when you are done.

Upvotes: 0

R Sahu
R Sahu

Reputation: 206607

Change linkedStruct to contain an object of coordinates in it:

struct linkedStruct
{
   coordinates elem;
   struct linkedStruct*  next;
};

Then, after malloc, you can set the members of the new linkedStruct.

linkedPtr ptr = malloc(sizeof(linked));
ptr->elem.x = 1;
ptr->elem.y = 2;
ptr->next = NULL;

Upvotes: 2

Related Questions