0

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);
Kroganite
  • 5
  • 3
  • Assuming for a moment that `head` is here for some nefarious reason left over from code-gone-by, what validation are you using to suggest your resulting segment is only one structure wide? Your `setData` call looks correct for receiving a *single* `struct plane`. What it is *doing* with it remains a mystery to us, as we can't see your code. If this is supposed to be a linked list, this is the *wrong* way to do it. If its a dynamic *array* ? this has merit. ([And don't cast `malloc()` in C programs.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)). – WhozCraig May 03 '14 at 23:27
  • Ignoring issues of idiom, I think the fundamental problem here is that you are being asked to create a *linked list* but instead you are creating an *array*. Also, the requested sorting is not happening. Without seeing the definition of `struct plane` we cannot be any more helpful than that. – zwol May 03 '14 at 23:28
  • Yes it was something I tried previously, and when I run my program with the current function, (like I said it's an already completed program with another correct function to print the structures) it only displays one result – Kroganite May 03 '14 at 23:29
  • Post the declaration of `struct plane` in your question please. and verify whether this is, or is not, supposed to be a dynamic array or a linked list. – WhozCraig May 03 '14 at 23:30
  • Other functions arent relevant because they are correct, my question is if this function I created is doing what is asked....does it create a link list of num structures or is it only storing a single one – Kroganite May 03 '14 at 23:31
  • Neither. Its "storing" `n` structures, and linking *none* of them. And if this is indeed to be a dynamic linked list you'll hit a wall of a whole different beast when it comes to extracting, and *deleting*, a *single* element from said-same using the above allocation scheme. – WhozCraig May 03 '14 at 23:31
  • how should I approach doing what your saying with my code? because I do have to delete later – Kroganite May 03 '14 at 23:34
  • Thanks for updating the question. So one more thing. Is this returned linked list supposed to support single-element insertion and extraction (at some time later, after this code, obviously)? If so, this allocation scheme will not work. if not, it *can* work so long as you realize you can only "free" one node (the lead node) because in so doing you're freeing the entire list. – WhozCraig May 03 '14 at 23:35
  • the instructions say repeated use of malloc....so what should i do to make each element accessable – Kroganite May 03 '14 at 23:36

2 Answers2

0

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;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

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");
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • What you did makes sense, but there is still just one result being displayed....is there a simple function to print whats in the structure we are filling with data so I can make sure the teacher didnt screw up the display function – Kroganite May 03 '14 at 23:46
  • @Kroganite I'll post a how-to-print-your-list as well, but you have to fill in the actual output, as I haven't a clue what you're looking for on that regard. – WhozCraig May 03 '14 at 23:46
  • that would be very helpful – Kroganite May 03 '14 at 23:48