0

Creating a list in C implementing SLL.

struct dat
{
char fname[20];
char lname[20];
};

typedef struct node_s
{
 struct dat data;
 struct node_s *next;
}NODE;

NODE *list=NULL;

NODE *list_create(struct dat *data)
{
    NODE *node;
    if(!(node=malloc(sizeof(NODE))))
    {
        return NULL;
    }
    node->data = data;
    node->next = NULL;
    return node;
}

NODE *list_insert_after(NODE *node, struct dat *data)
{
    NODE *newnode;
    newnode = list_create(data);
    newnode->next = node->next;
    node->next = newnode;
    return newnode;
}

I cant find any good examples using SLL. Now i have this append function now i will apply the two SLL functions, list_create and list_insert_after. Is this correct?

void app(struct dat x)
{
    FILE *fp;
    fp=fopen("database.dat","a");

    if(fp==NULL)
    {
        printf("file error");
    }
    if(fp!=NULL)
    {
        printf("enter lname: ");
        gets(x.lname);
        printf("enter fname: ");
        gets(x.fname);

        fprintf(fp,"%s %s\n",x.lname,x.fname);

        if(list == NULL)
        {
         list=list_create((&x));
        }
        else
        {
         next=list_insert_after(list,&x);
        }

    }

    fclose(fp);

}
MANOJ GOPI
  • 1,279
  • 10
  • 31
  • See [Is it a good idea to typedef pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – Jonathan Leffler Feb 01 '15 at 04:25
  • In `list_insert_after()`, it is a good idea to test the return value from `list_create()` before dereferencing it; it could be a null pointer. – Jonathan Leffler Feb 01 '15 at 04:27
  • 1
    See [Why is the `gets()` function dangerous?](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for an explanation of why you should never, ever use the `gets()` function. – Jonathan Leffler Feb 01 '15 at 04:28
  • I understand the concept of SLL. Applying it to C programming and using it for programs is hard to understand and i cant find any good examples applying it to programs like database etc. – imagineracoon Feb 01 '15 at 04:36
  • You can find plenty of good [examples](https://www.google.co.in/search?q=singly+linked+list+in+c+example&ie=utf-8&oe=utf-8&gws_rd=cr&ei=BK7NVLXVJ8z58QXm44LoCQ) of singly linked list. Once you choose what you think best for you, you can use it for any purpose like database etc. – Atul Feb 01 '15 at 04:42
  • there are dozens of examples in stackoverflow on how to implement singly linked lists in C. Therefore, I see this as a duplicate question that should be closed. – user3629249 Feb 01 '15 at 06:45
  • this line: 'newnode->next = node->next;' is generating a list where every entry points to the first entry. probably not what you want. suggest: 'newnode->next = NULL;' – user3629249 Feb 01 '15 at 06:50
  • why are the list entries being defined as struct dat in some places and NODE in others, especially as there is no relationship (not do I see a definition of the struct dat. – user3629249 Feb 01 '15 at 06:53

2 Answers2

0

To complete your example you should decide:

a) what if the role of your file database.dat.

b) how you work with your list.

I suppose you need two pointers to operate with the list: a pointer to the first element (it is list I think) and pointer to the last element (may be it is next). Now call such as list_insert_after(list,&x); always inserts element after the first element, so your list cannot be longer than 2.

Consider the changes in your code as follows:

        if(list == NULL)
        {
         list = list_create((&x));
         next = list; // the last is the first
        }
        else
        {
         next=list_insert_after(next,&x); // insertion after the last
        }

And about the file: if you decide to store not strings in text file, but structures in the binary file, save only struct dat to file, because storing pointers has no sense.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
VolAnd
  • 6,367
  • 3
  • 25
  • 43
0

Even if C is a low level language, it is not forbidden to structure data. The node should be an internal object of the list. How to implement it in C depends on how you want to use it. Some examples :

  • a FIFO stack : 2 functions push and pop, and the list only needs to retain the last added element (you won't add after but before - left as an exercise)
  • a LIFO queue : still only 2 functions add and take, and the list needs to retain first element (its head) because its the next that will be taken, and last one (its tail) because it is where you will add next

    typedef struct list_s {
        NODE *head;
        NODE *tail;
        int len;
    } LIST;
    
    int add(LIST *list, struct dat *data) {
        list->tail = list_insert_after(list->tail, data);
        list->len += 1;
        return list->len;
    }
    
    struct dat * take(LIST *list) {
        if (list->len == 0) return NULL;
        struct dat *data = head->data;
        NODE *n = head;
        head = head->next;
        free(n);
        return data;
    }
    

    of course, you will have to initialize a list, could take its length, ...

  • a true list : internally the same as a LIFO, but you will have to define an iterator (only contains a NODE *), and for example a BOOL hasnext(*ITERATOR) and struct dat * next(*ITERATOR)
  • the remaining is only limited by your requirements

NB: I assumed NODE.data was a struct dat * not a struct dat ...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252