Thursday, November 10, 2005

Creating a list by iteration

The previous post is quite a bit confusing because it is by recursion. Well, nothing special. Here is the code in all its glory.



#include "list.h"

LINK s_to_l(char s[])
{
        LINK head = NULL, tail;
        int i;

        if (s[0] != '\0') {
                head = malloc(sizeof(ELEMENT));
                head -> d = s[0];
                tail = head;

                for (i = 1; s[i] != '\0'; ++i) {
                        tail -> next = malloc(sizeof(ELEMENT));
                        tail = tail -> next;
                        tail -> d = s[i];
                }
                tail -> next = NULL;
        }

        return head;
}

No comments: