All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alphex Kaanoken <akaanoken@softminecorp.com>
To: Jagadeesh Bhaskar P <jbhaskar@hclinsys.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: deletion in singly linked list
Date: Wed, 24 Nov 2004 15:43:09 +0300	[thread overview]
Message-ID: <20041124154309.5588936e.akaanoken@softminecorp.com> (raw)
In-Reply-To: <1101198249.3786.3.camel@myLinux>

On Tue, 23 Nov 2004 13:54:10 +0530
Jagadeesh Bhaskar P <jbhaskar@hclinsys.com> 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!!
> 
> -- 
> Jagadeesh Bhaskar P <jbhaskar@hclinsys.com>
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
So, You must have something like following structure
typedef struct _mylist {
int id; /*I'm usually use it - you may don't including it*/
void *data; /*pointer to data*/
struct _mylist *next; /*pointer to the next list member*/
} mylist;

For deletion from this list use something code like that - 
mylist * DelItem(mylist *ptr,int id) /*id of member that you want to delete*/
{
  mylist *prev=NULL;
  mylist *curr;
  curr=ptr; /*save pointer address*/

  while(curr!=NULL && curr->id!=id){ /*search for member with pointed id*/
    prev=curr;
    curr=curr->next;
  }
  if(curr==NULL) /*nothing to delete NOTE!: don't make `curr' unsigned!*/
    return ptr;
  if(curr==ptr) /*just see what the member*/
    ptr=curr->next;
  else
    prev->next=curr->next;
  free(curr->data); /*if you need to free some data that pointer by void* */
  free(curr); /*free member*/
  return ptr; /*return pointer to the start of linked list*/
}

use this function - 
MyList=DelItem(MyList,2);

This function's algorythm working very well and stable. It can delete anything item - in start 
of list, middle if list, or at end of list ;-)

Best wishes.

  parent reply	other threads:[~2004-11-24 12:43 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
2004-11-24 12:43 ` Alphex Kaanoken [this message]
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=20041124154309.5588936e.akaanoken@softminecorp.com \
    --to=akaanoken@softminecorp.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.