Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Linked List

4 views
Skip to first unread message

dmp

unread,
Nov 24, 2009, 10:44:13 PM11/24/09
to
how to create a circular linked list in C?
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Seebs

unread,
Nov 26, 2009, 3:45:10 PM11/26/09
to
On 2009-11-25, dmp <pate...@gmail.com> wrote:
> how to create a circular linked list in C?

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!

Philip Paeps

unread,
Nov 26, 2009, 3:45:23 PM11/26/09
to
dmp <pate...@gmail.com> wrote:
> how to create a circular linked list in C?

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.

Message has been deleted

Ike Naar

unread,
Nov 26, 2009, 3:47:29 PM11/26/09
to
In article <clcm-2009...@plethora.net>, dmp <pate...@gmail.com> wrote:
>how to create a circular linked list in C?

struct node { struct node *next; } circular_list = {&circular_list};

voidpointer

unread,
Nov 28, 2009, 5:03:49 PM11/28/09
to
On 25 nov, 01:44, dmp <pateld...@gmail.com> wrote:
> how to create a circular linked list in C?
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line.  Sorry.

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

0 new messages