linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Glynn Clements <glynn@gclements.plus.com>
To: Jagadeesh Bhaskar P <jbhaskar@hclinsys.com>
Cc: Linux C Programming <linux-c-programming@vger.kernel.org>
Subject: Re: deletion in singly linked list
Date: Tue, 23 Nov 2004 10:08:32 +0000	[thread overview]
Message-ID: <16803.3104.750163.815769@cerise.gclements.plus.com> (raw)
In-Reply-To: <1101198249.3786.3.camel@myLinux>


Jagadeesh Bhaskar P wrote:

> I am having the address of a single node of a singly linked list. All I
> know about that node is that it is not the head of the list. Now say, I
> want to delete this node. I can infer its next node, but not its
> predicissor. Is there any way to delete that node, without breaking the
> whole linked list down!!

In general, no.

If you want to be able to delete elements, you normally require an
extra level of indirection, i.e. a pointer to the pointer.

E.g. if the node structure is:

	struct node {
		struct node *next;
		/* other fields */
	};

and you have a list:

	struct node *the_list;

you can traverse the list for reading with:

	struct node *l;

	for (l = the_list; l; l = l->next)
	{
		...
	}

But if you want to be able to delete an element, you would use e.g.:

	struct node **p, *l;

	for (p = &the_list; l = *p; )
	{
		if (want_to_delete(l))
		{
			*p = l->next;
			free(l);
		}
		else
			p = &l->next;
	}

The same applies if you want to insert a new element before the
current element.

-- 
Glynn Clements <glynn@gclements.plus.com>

  parent reply	other threads:[~2004-11-23 10:08 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-23  8:24 deletion in singly linked list Jagadeesh Bhaskar P
2004-11-23  8:42 ` Justinas
2004-11-23  8:55   ` mikael-aronsson
2004-11-23  9:09     ` Jagadeesh Bhaskar P
2004-11-23 16:57   ` Justinas
2004-11-23 10:08 ` Glynn Clements [this message]
2004-11-24 12:43 ` Alphex Kaanoken
2004-11-25  3:49   ` Jagadeesh Bhaskar P
2004-11-25 10:11     ` Alphex Kaanoken
2004-11-25 10:26       ` Jagadeesh Bhaskar P
2004-11-25 11:09         ` Alphex Kaanoken
  -- strict thread matches above, loose matches on Subject: below --
2004-11-23  9:00 Bakki Srinivas
2004-11-23  9:13 ` mikael-aronsson
2004-11-23  9:33   ` Jagadeesh Bhaskar P
2004-11-23  9:29 ` Jagadeesh Bhaskar P
2004-11-23  9:59 ` Glynn Clements
2004-11-23  9:35 Bakki Srinivas
2004-11-23  9:55 ` Jagadeesh Bhaskar P
2004-11-23 10:22 Bakki Srinivas
2004-11-23 10:23 Bakki Srinivas
2004-11-23 10:31 Bakki Srinivas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=16803.3104.750163.815769@cerise.gclements.plus.com \
    --to=glynn@gclements.plus.com \
    --cc=jbhaskar@hclinsys.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).