Jump to content


Linked list


9 replies to this topic

#1 Teron

    Absinth drinker

  • Member
  • 938 posts

Posted 23 January 2008 - 16:14

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. >.<
"It's not the cards you have, it's how you play them!" - Gambit (X-Men)

#2 G-sus

    batshit insane

  • Member
  • 802 posts
  • Projects: Coding Skynet

Posted 23 January 2008 - 16:50

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

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

createListElement();

}
Posted Image
(Sig by The DR)

True beauty comes from heart and mind.
(but perfection has also big boobs)

#3 Teron

    Absinth drinker

  • Member
  • 938 posts

Posted 23 January 2008 - 21:16

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? :)
"It's not the cards you have, it's how you play them!" - Gambit (X-Men)

#4 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 23 January 2008 - 22:08

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

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#5 Teron

    Absinth drinker

  • Member
  • 938 posts

Posted 24 January 2008 - 00:03

I'd like to build my own list, yes :P
"It's not the cards you have, it's how you play them!" - Gambit (X-Men)

#6 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 25 January 2008 - 15:20

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++?
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#7 Teron

    Absinth drinker

  • Member
  • 938 posts

Posted 26 January 2008 - 01:38

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 :(
"It's not the cards you have, it's how you play them!" - Gambit (X-Men)

#8 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 26 January 2008 - 13:06

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.

CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#9 Teron

    Absinth drinker

  • Member
  • 938 posts

Posted 29 January 2008 - 18:29

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.

"It's not the cards you have, it's how you play them!" - Gambit (X-Men)

#10 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 29 January 2008 - 20:55

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.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users