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: Thu, 25 Nov 2004 13:11:42 +0300 [thread overview]
Message-ID: <20041125131142.652bdb95.akaanoken@softminecorp.com> (raw)
In-Reply-To: <1101354576.3795.1.camel@myLinux>
On Thu, 25 Nov 2004 09:19:37 +0530
Jagadeesh Bhaskar P <jbhaskar@hclinsys.com> wrote:
ok, you made some mistakes
>
>
> /************* start of code ***********/
> #include <stdio.h>
>
> typedef struct _node{
> int data;
> struct _node *next;
> }node;
>
> void printList(node* head){ /*this is ok, but you make pointer to unsiged*/
> node *temp;
> temp = head;
> while(temp){ /*! make for safetly while(tmep!=NULL)*/
> 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;
/*it's can't be worked! I'm usually write a simple function for this and it's working*/
> 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);
/*it's not true way - use my function*/
>
>
> 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???
>
So, I'm write a functions- for deletion from list ypu will see in my prevous message
for add mamber to the end of list use something like this function:
mylist * AddItem(mylist *ptr,void *data)
{
mylist *lp=ptr;
int id=0;
if(ptr!=NULL){
++id;
while(ptr->next!=NULL)
{
ptr=ptr->next;
++id;
}
ptr->next=(app_list*) malloc(sizeof(app_list);
ptr=ptr->next;
ptr->next=NULL;
ptr->id=id;
ptr->data=data; /*see how you will be work with pointers*/
return lp;
}
else{
ptr=(app_list*) malloc(sizeof(app_list);
ptr->next=NULL;
ptr->id=id;
ptr->data=data;
return ptr;
}
}
it's worked with my singly linked list structure example in prevous message
If you need to insert/add to start of list, just rewrite this function it's simple.
In addition just imagenate you linked list structure in mind and understand that
is very simple
in linked list you have "members" with pointer to the next member,
if you need to remove member in any place you must do following things
- find member to remove
- take a pointer of prev member and change him to the next member that following after removeing member
- free memory that malloced for removing member
it's all
also if you need to make addition to the end of list faster you can create
a pointer to the end of list, in this case you don;t need to go for the end of list when
you want to add the new member.
Best wishes
>
> --
> With regards,
>
> Jagadeesh Bhaskar P
>
> -
> 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
>
--
System Developer of
SoftMine Corp.
Alphex Kaanoken
next prev parent reply other threads:[~2004-11-25 10:11 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
2004-11-25 10:11 ` Alphex Kaanoken [this message]
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=20041125131142.652bdb95.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 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).