0

I'm trying to create an empty linked list, which asks the user for the maximum number of terms that the list can hold. (I didn't add my code for that as its simply a printf). I then have to create a new function which asks the user to insert input into the previously created list.

My question is, how do I make the create_q() function return the empty list?

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

typedef struct node_t {
    int value;
    int priority;
    struct node_t *next;
}node;

typedef struct priority_linked_list {
    struct name *head;
    int current_size;
    int max_size;
}priority_list;

typedef node *Node;
typedef priority_list *List;

void create_q(int max_terms) {

    node *head = NULL;
    node *next = NULL;
    List *current_size = 0;
    List *max_size = max_terms;

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user3160152
  • 51
  • 1
  • 5
  • 12
  • 1
    You'll want a variable in the list struct for the root node and then in create_q you will want to create the root node using malloc() and then create a node* for the current node you are at then loop for max_terms, use malloc() to create a new node, set it as the current node's next and then set the current node to the next node of the current node, voila. You might want to google how to use malloc(), etc before you attempt this though. – tom Dec 29 '14 at 17:16
  • You have a `struct name *head;` member in your priority linked list structure, but you haven't defined `struct name` (and you probably meant `struct node_t head;` or `node *head;`). If `create_q()` is going to return a value, you have to change the return type from `void` — presumably to `priority_list *` or `List`. (See [Is it a good idea to `typedef` pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers)). – Jonathan Leffler Dec 29 '14 at 17:16
  • It looks a little bit like you don't know how linked lists are used. Usually, you create new list nodes while you are inserting data. I am not quite sure what you mean with “empty list” either. – fuz Dec 29 '14 at 17:18
  • @FUZxxl: one design for linked lists simply keeps a pointer to the first node in the list. Another design — the one used here — has an auxilliary structure which contains other information (for example, it maintains a count of the number of elements, and might contain a pointer to tail of the list as well as the head). It's a different way of managing a list; it is not as minimal and spartan, but it can certainly work. – Jonathan Leffler Dec 29 '14 at 17:21
  • @JonathanLeffler That's right, but OP's data structure does not contain a pointer to the place to the next unused list node. I honestly doubt that he wants an O(n) insert function. – fuz Dec 29 '14 at 17:24
  • If you know the max number of items, better use a static array. – mrk Dec 29 '14 at 17:47

3 Answers3

1

In C, linked lists are usually implemented as a series of nodes stored on the heap that point to eachother. The heap is a persistent memory area that runs throughout the life-cycle of the program.

When you create a variable normally in a C function, and the function returns, the variable that you created is no longer accessible. However when you create something on the heap in a function, and the function is returned, the data you allocated on the heap is still there. However, you have no way of accessing it-- unless the function returns a pointer.

So what you would do for create_q() would be to create the linked list on the heap (using a function in stdlib.h called "malloc"), and then you would return a pointer to your first node, letting the main function know where on the heap to find the first node. Then that first node would have a pointer in it, telling the program where on the heap to find the second node, and so forth.

However, you're probably approaching linked lists the wrong way. Unless this is for some sort of homework project, you probably wouldn't want to create an empty linked list. One of the benefits of a linked list is that it's a dynamic structure in which you can easily insert new nodes. You could still have some variable keeping track of the maximum size you want the list to be, but you probably wouldn't want to actually create the nodes until you had to.

Just keep in mind what a linked list is. It's a set of nodes floating on the heap (in C) that each store some data, and contain a pointer to the next node floating on the heap. All you need, to access the linked list, is a pointer to the first node. To add a new node, you simply "walk" through the list till you reach the last node, and then create a new node and have the old-last node point to it.

Nathan
  • 73,987
  • 14
  • 40
  • 69
  • And as some other people mentioned, you can have an axillary structure that keeps track of the number of nodes in the list, or other information you might need. You could either have this in your main function, have a global counter (although this is generally bad form), or make the first head node of the list different than the rest of the nodes and contain some extra information. – Nathan Dec 29 '14 at 17:35
  • Hi, thanks a lot for your response. It helped me understand linked lists. So if i wanted to create an empty linked list in this function, all I'd have to do is make all my nodes equal to NULL? – user3160152 Dec 29 '14 at 18:21
0
Is this what you had in mind?

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

struct node_t
{
    int value;
    int priority;

    struct node_t *next;
};

static int current_size;
static int max_size;
static struct node_t* head = NULL;

struct node_t* create_q(int);

struct node_t* create_q(int max_terms)
{
    int i; // loop counter/index

    current_size = max_terms;
    max_size = max_terms;

    if( NULL == (head = malloc(sizeof(struct node_t)*max_terms)))
    { // then, malloc failed
        perror("malloc failed for struct node_t list");
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // set all fields to '0,0,Null'
    memset( head, 0x00, sizeof(struct node_t)*max_terms);

    // set all next links, except last link
    for(i=0;i<(max_terms-1);i++)
    {
        head[i].next = &head[i+1];
    }

    // set last link
    head[i].next = NULL;

    return( head );
} // end function: create_q
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Quick question: Would it be possible to create multiple linked lists from this function? As for one part of the project i also have to combine 2 previously created linked lists. Also, the if statement wouldn't work if the linked list is empty right? – user3160152 Dec 29 '14 at 18:26
0

I suspect you are looking for something like the following for creating or initializing your priority linked list.

/*****
 * alloc_q - allocate memory for the priority linked list
 */
struct priority_linked_list *alloc_q(void)
{
    struct priority_linked_list *list;

    list = malloc(sizeof(*list));
    return list;
}

/******
 * init_q - initialize the priority linked list
 */
void init_q(struct priority_linked_list *list, int max_terms)
{
    list->head = NULL;
    list->current_size = 0;
    list->max_size = max_terms;
}

/******
 * create_q - allocate AND initialize the priority linked list
 */
struct priority_linked_list *create_q(int max_terms)
{
    struct priority_linked_list *list;

    list = alloc_q();
    if (list == NULL) {
        return NULL;
    }
    init_q(list, max_terms);
    return list;
}

Allocation of nodes and their addition/removal to/from the list would be handled separately.

There may be typos in the above (I have not tested it). However, it should be enough to get you on the path you want.

Hope it helps.

Sparky
  • 13,505
  • 4
  • 26
  • 27