Linked list
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. >.<
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. >.<
G-sus 23 Jan 2008
try a FOR() loop maybe.
e.g.
for(;entry >0; entry--) {
createListElement();
}
e.g.
for(;entry >0; entry--) {
createListElement();
}
Teron 23 Jan 2008
Yeah, thats what I am looking for.
But I cannot write:
This will end up in a crash, so Im looking for a way to do this the idea described the way above. Any help?
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?
CodeCat 23 Jan 2008
Do you really need to build your own list? If not, just use the class in the <list> standard header.
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++?
Using C or C++?
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 :(
They want it to be implemented in C (no C++ ), so no classes :(
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:
linkedlist.cpp:
Edited by CodeCat, 26 January 2008 - 13:44.
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(ListNode* node, ElementType element); ElementType Delete(ListNode* node); ListNode* CreateListFromArray(ElementType* array, int length); void DeleteAllFrom(ListNode* node);
linkedlist.cpp:
#include <stdlib.h> ListNode* InsertAfter(ListNode* node, ElementType element) { ListNode* newNode = malloc(sizeof(ListNode)); if (newNode == NULL) return NULL; newNode->element = element; if (node != NULL) { newNode->prev = node; newNode->next = node->next; node->next = newNode; if (newNode->next != NULL) newNode->next->prev = newNode; } else { newNode->prev = NULL; newNode->next = NULL; } return newNode; } ElementType Delete(ListNode* node) { ElementType element = node->element; if (node->prev != NULL) node->prev->next = node->next; if (node->next != NULL) node->next->prev = node->prev; free(node); return element; } ListNode* CreateListFromArray(ElementType* array, int length) { ListNode* startOfList; ListNode* currentNode; int i; startOfList = InsertAfter(NULL, array[0]); for (i = 1, currentNode = startOfList; i < length; i++) { currentNode = InsertAfter(currentNode, array[i]); } return startOfList; } void DeleteAllFrom(ListNode* node) { ListNode* currentNode; ListNode* nextNode; if (node->prev != NULL) node->prev->next = NULL; currentNode = node; while (currentNode != NULL) { nextNode = currentNode->next; free(currentNode); currentNode = nextNode; } }
Edited by CodeCat, 26 January 2008 - 13:44.
Teron 29 Jan 2008
Thank you very much, Codecat.
Your code works, but I found a short way of doing so
Edited by Teron, 29 January 2008 - 18:30.
Your code works, but I found a short way of doing so
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.
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.