←  Code Block

Fallout Studios Forums

»

Linked list

Teron's Photo Teron 23 Jan 2008

Hi,
is there any way to create a linked list with the help of a loop? (c/c++)
I have a dynamic set of list elements which depend on the users entry. Say, he/she enters 5 -> for(...i<5...) createList();

Or do I really have to do this with if-methodes? Like:

if(entry>=5) {
createListElement();
connectThem();
createListElement();
connectThem();
createListElement();
connectThem();
createListElement();
connectThem();
createListElement();
}

Cannot imagine that, so there must be a way. >.<
Quote

G-sus's Photo G-sus 23 Jan 2008

try a FOR() loop maybe.
e.g.

for(;entry >0; entry--) {

createListElement();

}
Quote

Teron's Photo Teron 23 Jan 2008

Yeah, thats what I am looking for.
But I cannot write:

int i;

typedef struct listElement {
                           struct listElement* next;
                           struct listElement* prev;
                           int data;
};

listElement* Head = malloc(sizeof(struct listElement));
listElement* lastElement;                                  //pointer at last listElement

for(i = 0; i<sizeOfList; i++) {
        listElement* Element = malloc(sizeof(listElement));
        if( i == 0) {
                head->next = Element;
                lastElement = Element;                    
         } else {
                Element->previous = lastElement;
                lastElement->next = Element;
          }
}

This will end up in a crash, so Im looking for a way to do this the idea described the way above. Any help? :)
Quote

CodeCat's Photo CodeCat 23 Jan 2008

Do you really need to build your own list? If not, just use the class in the <list> standard header.
Quote

Teron's Photo Teron 24 Jan 2008

I'd like to build my own list, yes :P
Quote

CodeCat's Photo CodeCat 25 Jan 2008

So as I understand it, you have an array of elements of a certain length, and need a function to create a (singly or doubly?) linked list from them.

Using C or C++?
Quote

Teron's Photo Teron 26 Jan 2008

I want to create a (double) linked list with the help of a loop, because adding 40 elements to a list manually is just lame :) (too much code with just different indices)
They want it to be implemented in C (no C++ :)), so no classes :(
Quote

CodeCat's Photo CodeCat 26 Jan 2008

If I were you I'd create a struct and a few functions to manipulate them. I'll post something up here later if you like.

EDIT: Here's some code to get you started. This code is entirely untested so you might get a few compile errors. But the general idea is there. CreateListFromArray does what you want. DeleteAllFrom is used to quickly free an entire list, since it frees all nodes from and including the node you pass to it. So pass it the start of a list to delete the entire list.

linkedlist.h:
typedef ElementType int;

typedef struct
{
	ElementType element;
	ListNode* prev;
	ListNode* next;
} ListNode;

ListNode* InsertAfter&#40;ListNode* node, ElementType element&#41;;
ElementType Delete&#40;ListNode* node&#41;;
ListNode* CreateListFromArray&#40;ElementType* array, int length&#41;;
void DeleteAllFrom&#40;ListNode* node&#41;;


linkedlist.cpp:
#include <stdlib.h>

ListNode* InsertAfter&#40;ListNode* node, ElementType element&#41;
{
	ListNode* newNode = malloc&#40;sizeof&#40;ListNode&#41;&#41;;
	
	if &#40;newNode == NULL&#41;
		return NULL;
	
	newNode->element = element;
	
	if &#40;node != NULL&#41;
	{
		newNode->prev = node;
		newNode->next = node->next;
		node->next = newNode;
		
		if &#40;newNode->next != NULL&#41;
			newNode->next->prev = newNode;
	}
	else
	{
		newNode->prev = NULL;
		newNode->next = NULL;
	}
	
	return newNode;
}

ElementType Delete&#40;ListNode* node&#41;
{
	ElementType element = node->element;
	
	if &#40;node->prev != NULL&#41;
		node->prev->next = node->next;
	
	if &#40;node->next != NULL&#41;
		node->next->prev = node->prev;
	
	free&#40;node&#41;;
	
	return element;
}

ListNode* CreateListFromArray&#40;ElementType* array, int length&#41;
{
	ListNode* startOfList;
	ListNode* currentNode;
	int i;
	
	startOfList = InsertAfter&#40;NULL, array&#91;0&#93;&#41;;
	
	for &#40;i = 1, currentNode = startOfList; i < length; i++&#41;
	{
		currentNode = InsertAfter&#40;currentNode, array&#91;i&#93;&#41;;
	}
	
	return startOfList;
}

void DeleteAllFrom&#40;ListNode* node&#41;
{
	ListNode* currentNode;
	ListNode* nextNode;
	
	if &#40;node->prev != NULL&#41;
		node->prev->next = NULL;
	
	currentNode = node;
	
	while &#40;currentNode != NULL&#41;
	{
		nextNode = currentNode->next;
		free&#40;currentNode&#41;;
		currentNode = nextNode;
	}
}

Edited by CodeCat, 26 January 2008 - 13:44.
Quote

Teron's Photo Teron 29 Jan 2008

Thank you very much, Codecat. :P

Your code works, but I found a short way of doing so :D
struct list_element* head;
struct list_element* elem;

head = elem = malloc(sizeof(struct list_element));

head->next = NULL;

for (i = 0; i < length; ++i)
{
elem->next = malloc(sizeof(struct list_element));
elem = elem->next;
elem->next = NULL;
}

Edited by Teron, 29 January 2008 - 18:30.
Quote

CodeCat's Photo CodeCat 29 Jan 2008

Short code doesn't equal better code. Your code serves only one purpose, mine is reusable. That's one of the skills they should be teaching you, too.
Quote