Question

HERE IS MY INSTRUCTIONS FOR THIS FUNCTION: Here an unsigned integer listsize is passed to this function you are to create a link list of size listsize. This will be performed by repeated use of malloc and calling setData to initialize the data into the struct plane fields. Each time you place the process in the list you need to place it so the list is sorted by the field distance (in ascending order). you return the head of the list

struct plane* list_intialize(unsigned int num)
{
struct plane *ptr,*head;
int i=0;

ptr = (struct plane*) malloc(num * sizeof(struct plane));

for (i = 0; i < num; ++i)
    setData(ptr+i);

return ptr;
}

This started as a function skeleton inside an already completed program....I'm to complete the function so that it creates a link list. The setData is given function that inserts data to the structure elements.....MY problem is that after I run the current function it only returns one plane with information instead of num amount....am I using setData wrong or should my current setup work

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


#ifndef _MY_DEF_
#define _MY_DEF_

enum dir {NE=0, EN, NW, WN, SE, ES, SW, WS};
enum loc {LNE=0,  LNW,LSE,LSW};
struct plane{
short flightCode;
 long xCord;
 long yCord;
double distance;
char direction;
enum dir flightPattern;
enum loc location;
struct plane *nextPlane;
};

#endif


struct plane* sortByDist(struct plane*);
struct plane * radarPrint(struct plane*head);
int checkPlane(struct plane *);
int checkForCollision(struct plane*);
void  setData(struct plane *pLane);
Was it helpful?

Solution

You need to allocate your list by allocating each node. One way of doing that while chaining the list forward is the code below:

struct plane* list_intialize(unsigned int num)
{
    struct plane *head, **pp = &head;

    int i=0;
    for (i=0; i<num; ++i)
    {
        *pp = malloc(sizeof(**pp));
        setData(*pp);
        pp = &(*pp)->nextPlane;
    }
    *pp = NULL;

    return head;
}

How It Works

This uses a pointer-to-pointer to always hold the address of the location where the nextPlane dynamo node address is stored. It starts with the address of the head pointer. With each new node, pp is filled the address of that node's nextPlane member. Once finished, it holds the address of the last node's nextPlane pointer, which it sets to NULL. The first node, pointed to by head, is returned. (and yes, this works even if you passed num = 0 for the requested size, in which case you would get back zero nodes: i.e. NULL).

Note: Don't forget, you need to free each node when releasing the list, extracting a single node out, etc. For example, to delete an entire list:

void list_delete(struct plane **lst)
{

    while (*lst)
    {
        struct node *victim = *lst;
        *lst = victim->nextPlane;
        free(victim);
    }
}

Invoked like this:

struct plane *lst = list_initialize(N);

// use list.., maybe adding nodes, removing them, changing, etc...

list_delete(&lst);

How to print your list:

void list_print(const struct plane *lst)
{
    while (lst)
    {
        // TODO: print list node pointed to by lst. 
        // Ex: (x,y) coords
        printf("(%d,%d) ",lst->xCord, lst->yCord);

        lst = lst->nextPlane;
    }
    printf("\n");
}

OTHER TIPS

You are not setting the links between the objects. In the for loop, you need:

 ptr[i]->nextPlane = ptr[i+1];

At the end of the loop, make sure the last object points to NULL.

ptr[i-1] = NULL;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top