Ik heb hier de code voor een enkelvoudig (single) gelinkte lijst. Weet iemand hoe ik deze code kan aanpassen naar een tweevoudige (double) gelinkte lijst, aan de hand van struct ns*prev, ik kom er niet echt uit...
Code:
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for malloc */
typedef struct ns
{
int data;
struct ns *next; /* pointer to next element in list */
[B]struct ns *prev;[/B]
} node;
node *list_add(node **p, int i)
{
node *n = (node *)malloc(sizeof(node));
if (n == NULL)
return NULL;
n->next = *p; /* the previous element (*p) now becomes the "next" element */
*p = n; /* add new empty element to the front (head) of the list */
n->data = i;
return *p;
}
int main(void)
{
node *n = NULL;
list_add(&n, 0); /* list: 0 */
list_add(&n, 1); /* list: 1 0 */
list_add(&n, 2); /* list: 2 1 0 */
list_add(&n, 3); /* list: 3 2 1 0 */
list_add(&n, 4); /* list: 4 3 2 1 0 */
return 0;
}