From: Jagadeesh Bhaskar P <jbhaskar@hclinsys.com>
To: Alphex Kaanoken <akaanoken@softminecorp.com>
Cc: Linux C Programming <linux-c-programming@vger.kernel.org>
Subject: Re: deletion in singly linked list
Date: Thu, 25 Nov 2004 09:19:37 +0530 [thread overview]
Message-ID: <1101354576.3795.1.camel@myLinux> (raw)
In-Reply-To: <20041124154309.5588936e.akaanoken@softminecorp.com>
/************* start of code ***********/
#include <stdio.h>
typedef struct _node{
int data;
struct _node *next;
}node;
void printList(node* head){
node *temp;
temp = head;
while(temp){
printf("%d\n", temp->data);
temp = temp->next;
}
return;
}
int main(void){
node *a, *b, *c;
a = (node*)malloc(sizeof(node));
b = (node*)malloc(sizeof(node));
c = (node*)malloc(sizeof(node));
a->data = 1;
a->next = b;
b->data = 2;
b->next = c;
c->data = 3;
c->next = NULL;
node *head;
head = a;
printList(head); //prints 1, 2, 3 -- its OK
//deletion of b
printf("Attaching node with data = %d\n", (b->next)->data);
b = b->next;
free(b);
printList(head); //prints 1, 2, 0 -- NOT OK -- still a->next points to the location of b, even if its freed!! And also making b = b->next and free(b) removes the link b->next pointing to c. So finally c->data is not obtained!!
return 0;
}
/********** end of code ************/
It is exactly in the same track as Kaanoken mentioned. But the linked list is broken.
Now how can this be fixed???
--
With regards,
Jagadeesh Bhaskar P
next prev parent reply other threads:[~2004-11-25 3:49 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
2004-11-25 3:49 ` Jagadeesh Bhaskar P [this message]
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=1101354576.3795.1.camel@myLinux \
--to=jbhaskar@hclinsys.com \
--cc=akaanoken@softminecorp.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).