Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #11018 > unrolled thread

Circular Link List program doesn't run

Started byMaxx <grungeddd.maxx@gmail.com>
First post2011-09-27 10:46 -0700
Last post2011-09-28 10:53 -0700
Articles 10 — 6 participants

Back to article view | Back to comp.lang.c


Contents

  Circular Link List program doesn't run Maxx <grungeddd.maxx@gmail.com> - 2011-09-27 10:46 -0700
    Re: Circular Link List program doesn't run Fred <fred.l.kleinschmidt@boeing.com> - 2011-09-27 12:16 -0700
      Re: Circular Link List program doesn't run Ike Naar <ike@sverige.freeshell.org> - 2011-09-28 07:18 +0000
        Re: Circular Link List program doesn't run Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-28 11:03 +0100
        Re: Circular Link List program doesn't run James Kuyper <jameskuyper@verizon.net> - 2011-09-28 07:00 -0400
          Re: Circular Link List program doesn't run Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-10-03 20:10 +0300
        Re: Circular Link List program doesn't run Maxx <grungeddd.maxx@gmail.com> - 2011-09-28 10:54 -0700
          Re: Circular Link List program doesn't run James Kuyper <jameskuyper@verizon.net> - 2011-09-28 15:04 -0400
    Re: Circular Link List program doesn't run Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-27 20:21 +0100
      Re: Circular Link List program doesn't run Maxx <grungeddd.maxx@gmail.com> - 2011-09-28 10:53 -0700

#11018 — Circular Link List program doesn't run

FromMaxx <grungeddd.maxx@gmail.com>
Date2011-09-27 10:46 -0700
SubjectCircular Link List program doesn't run
Message-ID<8ceb0f1f-92f5-4926-a66d-e6a726f09af8@t11g2000yqk.googlegroups.com>
I have this problem on circular link list which says to move nodes
following t on the list to the node following x on the list. I have
written this program and it also compiled with no errors, but its not
running at all. I have tried a lot to solve this program but can't
seem to make any headway.

here is the program:::

#include <stdio.h>
#include <stdlib.h>

struct node
{
	int item;
	struct node *next;
};

typedef struct node *link;

link walk(link l,int v)  /*to traverse a list pointed by l till node v
*/
{

	while(l->next->item!=v)
	{

		l=l->next;
	}
	return l;
}

void shift(link l,int x,int t) /*to shift the contents of the list
following t to positon following x on list */
{
	int buff[30],i;

	l=walk(l,x);

	for(i=0; l->next->item!=t; i++)
	{
		l=l->next;
		buff[i]=l->item;

	}buff[i++]=9999;    /*used to mark the end of the conetents of the
list following x till t*/

	for(;l->next!=l;i++)
	{
		l=l->next;
		buff[i]=l->item;


	}


	l=walk(l,x);

	for(i=0;buff[i]!=9999;i++,l=l->next)
	{
		l->item=buff[i];
	}
	for(;l->next!=l;i++,l=l->next)
	{
		l->item=buff[i];
	}
}

void init(link l,int n)   /*to initialize a list with values till n */
{
	int i=2;
	while(i<=n)
	{
		l=(l->next=malloc(sizeof *l));
		l->item=i;
		l->next=l;
		i++;

	}


}
void traverse(link l)
{
	do
	{
		printf(" %d",l->item);
		l=l->next;
	}while(l->next!=l);
}


int main(int argc, char **argv)
{
	int N=atoi(argv[1]), x=atoi(argv[2]), t=atoi(argv[3]);

	link l=malloc(sizeof *l);
	l->item=1;
	l->next=l;

	init(l,N);
	shift(l,x,t);

	traverse(l);

	return 0;
}


Please help, any solutions or hint would be very grateful..

[toc] | [next] | [standalone]


#11020

FromFred <fred.l.kleinschmidt@boeing.com>
Date2011-09-27 12:16 -0700
Message-ID<741023e0-e416-45ef-891d-e6531808b90b@u6g2000vbo.googlegroups.com>
In reply to#11018
On Sep 27, 10:46 am, Maxx <grungeddd.m...@gmail.com> wrote:
> I have this problem on circular link list which says to move nodes
> following t on the list to the node following x on the list. I have
> written this program and it also compiled with no errors, but its not
> running at all. I have tried a lot to solve this program but can't
> seem to make any headway.
>
> here is the program:::
>
> #include <stdio.h>
> #include <stdlib.h>
>
> struct node
> {
>         int item;
>         struct node *next;
>
> };
>
> typedef struct node *link;
>
> link walk(link l,int v)  /*to traverse a list pointed by l till node v
> */
> {
>
>         while(l->next->item!=v)
>         {
>
>                 l=l->next;
>         }
>         return l;
>
> }
>
> void shift(link l,int x,int t) /*to shift the contents of the list
> following t to positon following x on list */
> {
>         int buff[30],i;
>
>         l=walk(l,x);
>
>         for(i=0; l->next->item!=t; i++)
>         {
>                 l=l->next;
>                 buff[i]=l->item;
>
>         }buff[i++]=9999;    /*used to mark the end of the conetents of the
> list following x till t*/
>
>         for(;l->next!=l;i++)
>         {
>                 l=l->next;
>                 buff[i]=l->item;
>
>         }
>
>         l=walk(l,x);
>
>         for(i=0;buff[i]!=9999;i++,l=l->next)
>         {
>                 l->item=buff[i];
>         }
>         for(;l->next!=l;i++,l=l->next)
>         {
>                 l->item=buff[i];
>         }
>
> }
>
> void init(link l,int n)   /*to initialize a list with values till n */
> {
>         int i=2;
>         while(i<=n)
>         {
>                 l=(l->next=malloc(sizeof *l));
>                 l->item=i;
>                 l->next=l;
>                 i++;
>
>         }
>
> }
>
> void traverse(link l)
> {
>         do
>         {
>                 printf(" %d",l->item);
>                 l=l->next;
>         }while(l->next!=l);
>
> }
>
> int main(int argc, char **argv)
> {
>         int N=atoi(argv[1]), x=atoi(argv[2]), t=atoi(argv[3]);
>
>         link l=malloc(sizeof *l);
>         l->item=1;
>         l->next=l;
>
>         init(l,N);
>         shift(l,x,t);
>
>         traverse(l);
>
>         return 0;
>
> }
>
> Please help, any solutions or hint would be very grateful..


Your init() method is flawed. For each new node, you set l->next=l
so the final node's "next" pointer points to itself, not to the first
node.
--
Fred K

[toc] | [prev] | [next] | [standalone]


#11038

FromIke Naar <ike@sverige.freeshell.org>
Date2011-09-28 07:18 +0000
Message-ID<slrn3vfsj85iia.5l2.ike@sverige.freeshell.org>
In reply to#11020
On 2011-09-27, Fred <fred.l.kleinschmidt@boeing.com> wrote:
> On Sep 27, 10:46?am, Maxx <grungeddd.m...@gmail.com> wrote:
>> [...]
> Your init() method is flawed. For each new node, you set l->next=l
> so the final node's "next" pointer points to itself, not to the first
> node.

It looks as if Maxx marks the end of the list with a node
that links to itself, instead of using the more customary convention
of letting the last node link to NULL.
Although his method is unconventional, it is not wrong in itself,
and I've seen situations where it works well.
One drawback is that the list cannot be empty; that is probably
the reason why Maxx initializes his list with a single element.

[toc] | [prev] | [next] | [standalone]


#11041

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2011-09-28 11:03 +0100
Message-ID<0.873201fbc620071b17ac.20110928110344BST.87k48tkmpr.fsf@bsb.me.uk>
In reply to#11038
Ike Naar <ike@sverige.freeshell.org> writes:

> On 2011-09-27, Fred <fred.l.kleinschmidt@boeing.com> wrote:
>> On Sep 27, 10:46?am, Maxx <grungeddd.m...@gmail.com> wrote:
>>> [...]
>> Your init() method is flawed. For each new node, you set l->next=l
>> so the final node's "next" pointer points to itself, not to the first
>> node.
>
> It looks as if Maxx marks the end of the list with a node
> that links to itself, instead of using the more customary convention
> of letting the last node link to NULL.

You maybe right, but I think it is more likely that it's an attempt to
meet the specification gone wrong rather than a peculiar list design.
The OP started:

| I have this problem on circular link list 

so the link to itself is correct when there is one element.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#11042

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-09-28 07:00 -0400
Message-ID<j5uuoc$e3i$1@dont-email.me>
In reply to#11038
On 09/28/2011 03:18 AM, Ike Naar wrote:
> On 2011-09-27, Fred <fred.l.kleinschmidt@boeing.com> wrote:
>> On Sep 27, 10:46?am, Maxx <grungeddd.m...@gmail.com> wrote:
>>> [...]
>> Your init() method is flawed. For each new node, you set l->next=l
>> so the final node's "next" pointer points to itself, not to the first
>> node.
> 
> It looks as if Maxx marks the end of the list with a node
> that links to itself, instead of using the more customary convention
> of letting the last node link to NULL.

Marking the last node with a link to NULL is the convention in a linear
list, but the subject line says that this is supposed to be a circular
linked list. A one-element circularly linked list should be linked to
itself. However, it should not remain linked to itself after other
elements are added to the list.

> Although his method is unconventional, it is not wrong in itself,
> and I've seen situations where it works well.
> One drawback is that the list cannot be empty; that is probably
> the reason why Maxx initializes his list with a single element.

A circularly linked list can be pointed at by pointing at any element of
the list; having that pointer be null is the way to indicate an empty list.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#11210

FromPhil Carmody <thefatphil_demunged@yahoo.co.uk>
Date2011-10-03 20:10 +0300
Message-ID<87ehyut30y.fsf@bazspaz.fatphil.org>
In reply to#11042
James Kuyper <jameskuyper@verizon.net> writes:
> On 09/28/2011 03:18 AM, Ike Naar wrote:
> > On 2011-09-27, Fred <fred.l.kleinschmidt@boeing.com> wrote:
> >> On Sep 27, 10:46?am, Maxx <grungeddd.m...@gmail.com> wrote:
> >>> [...]
> >> Your init() method is flawed. For each new node, you set l->next=l
> >> so the final node's "next" pointer points to itself, not to the first
> >> node.
> > 
> > It looks as if Maxx marks the end of the list with a node
> > that links to itself, instead of using the more customary convention
> > of letting the last node link to NULL.
> 
> Marking the last node with a link to NULL is the convention in a linear
> list, but the subject line says that this is supposed to be a circular
> linked list. A one-element circularly linked list should be linked to
> itself.

That's one way, but is not the linux implementation, for example. The 
list object (which is just a pointer or a pointer pair for doubly-linked
lists) points to the first node, that node points back to the head object.

> However, it should not remain linked to itself after other
> elements are added to the list.

That's certainly true.
 
> > Although his method is unconventional, it is not wrong in itself,
> > and I've seen situations where it works well.
> > One drawback is that the list cannot be empty; that is probably
> > the reason why Maxx initializes his list with a single element.
> 
> A circularly linked list can be pointed at by pointing at any element of
> the list; having that pointer be null is the way to indicate an empty list.

For singly linked lists, almost always, but it doesn't have to be.
The head object could point to itself, rather than at any nodes.
In particular in linux' implementation, doubly-linked lists are always
that way, with the list object pointing to itself in both directions.

Phi
-- 
"Religion is what keeps the poor from murdering the rich."
-- Napoleon

[toc] | [prev] | [next] | [standalone]


#11057

FromMaxx <grungeddd.maxx@gmail.com>
Date2011-09-28 10:54 -0700
Message-ID<3e422425-a96b-4adc-9dff-234389b83b3b@q13g2000vby.googlegroups.com>
In reply to#11038
On Sep 28, 12:18 am, Ike Naar <i...@sverige.freeshell.org> wrote:
> On 2011-09-27, Fred <fred.l.kleinschm...@boeing.com> wrote:
>
> > On Sep 27, 10:46?am, Maxx <grungeddd.m...@gmail.com> wrote:
> >> [...]
> > Your init() method is flawed. For each new node, you set l->next=l
> > so the final node's "next" pointer points to itself, not to the first
> > node.
>
> It looks as if Maxx marks the end of the list with a node
> that links to itself, instead of using the more customary convention
> of letting the last node link to NULL.
> Although his method is unconventional, it is not wrong in itself,
> and I've seen situations where it works well.
> One drawback is that the list cannot be empty; that is probably
> the reason why Maxx initializes his list with a single element.

Thats the problem i'm facing i want the last node to point to the
first node but its somehow its pointing to itself

[toc] | [prev] | [next] | [standalone]


#11059

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-09-28 15:04 -0400
Message-ID<4E836FC3.8090606@verizon.net>
In reply to#11057
On 09/28/2011 01:54 PM, Maxx wrote:
...
> Thats the problem i'm facing i want the last node to point to the
> first node but its somehow its pointing to itself

Ask  yourself one important question: "where in my code do I have a line
that is supposed to cause the last node to be linked back to the first
node?". The answer to that question should help you resolve that problem.

[toc] | [prev] | [next] | [standalone]


#11021

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2011-09-27 20:21 +0100
Message-ID<0.56ed73a65f2699236f9f.20110927202123BST.871uv1n64s.fsf@bsb.me.uk>
In reply to#11018
Maxx <grungeddd.maxx@gmail.com> writes:

> I have this problem on circular link list which says to move nodes
> following t on the list to the node following x on the list.

Coursework?

> I have
> written this program and it also compiled with no errors, but its not
> running at all. I have tried a lot to solve this program but can't
> seem to make any headway.

You've made some headway but I think you should slow down and build up
from smaller bits.  Start with init and traverse (both of which could be
better named -- make_sequence and print_list maybe?).  See if you can
make these work first.  Logically, init should be able to make a list
with no elements and it certainly should not start at 2.  Try printing
an empty list, then one with a single element and so on.  When you have
these working in a rock-solid way you can move on the more complex
parts.

<snip code>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#11058

FromMaxx <grungeddd.maxx@gmail.com>
Date2011-09-28 10:53 -0700
Message-ID<1ff8db87-8c11-4f5e-927b-05bef921e59a@c1g2000yql.googlegroups.com>
In reply to#11021
On Sep 27, 12:21 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Maxx <grungeddd.m...@gmail.com> writes:
> > I have this problem on circular link list which says to move nodes
> > following t on the list to the node following x on the list.
>
> Coursework?
>

not course work but i was learning link lists by myself and i came
across this problem..

> > I have
> > written this program and it also compiled with no errors, but its not
> > running at all. I have tried a lot to solve this program but can't
> > seem to make any headway.
>
> You've made some headway but I think you should slow down and build up
> from smaller bits.  Start with init and traverse (both of which could be
> better named -- make_sequence and print_list maybe?).  See if you can
> make these work first.  Logically, init should be able to make a list
> with no elements and it certainly should not start at 2.  Try printing
> an empty list, then one with a single element and so on.  When you have
> these working in a rock-solid way you can move on the more complex
> parts.
>
> <snip code>
> --
> Ben.

I started init with 2 cause the link list was declared earlier in
main() and it only contained one item. So i thought to start the next
item with 2 and initialize the list with the desired no of nodes. Yeah
i was also thinking about starting from basics. This head pointer is
giving me trouble and also the fact that my last node is pointing to
itself instead of the first node on the list.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web