struct x { struct x *next; } x = { &x };
-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
Using pointers.
- Philip
--
Philip Paeps Please don't email any replies
phi...@paeps.cx I follow the newsgroup.
Never offend people with style
when you can offend them with substance.
struct node { struct node *next; } circular_list = {&circular_list};
well, this is a simple example, and have memory leak, this is just an
example to give you some ideas
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
struct List
{
int val;
struct List *next;
}
*head = NULL;
void
addVal (int val)
{
struct List *l = malloc (sizeof (*l));
assert (l);
l->val = val;
/* is it the first value added? */
if (!head)
{
l->next = l;
head = l;
return;
}
/* is it the second? */
l->next = head;
if (head == head->next)
{
head->next = l;
return;
}
/* well there's more than 2 element in list, so we need
* walk to the tail and append l there */
{
struct List *tmp = head;
while (tmp->next != head)
tmp = tmp->next;
tmp->next = l;
}
}
int
main ()
{
struct List *tmp;
int i;
addVal (1);
addVal (2);
for (i = 0, tmp = head; i < 10; i++, tmp = tmp->next)
printf ("%d\n", tmp->val);
return 0;
}
you should get an output like
1
2
1
2
1
2
1
2
1
2