C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
doubly linked list in c Posted by sunone on 12 May 2004 at 12:09 AM
i need help on designing a doubly linked list in c with basic operations like navigating back and forth in the list, append, display, insert, delete elements from the list
Report
Re: doubly linked list in c Posted by ash1894 on 12 May 2004 at 4:32 AM
Hi,
In C you have to construct a structure:

struct linklist{
char ch;
linklist *pNext;
linklist *pPrev;
};

Insertion and deletion from the begin or end is pretty easy. but if u want to insert/delete from the middle it is not that simple but not complex at all. whenever u insert just creat a new object of your structure and then assign its and other's pNext, pPrev pointers.

as:

think u have a link list: a c d g.
Now if u had to insert e which will reside in btn d and g u have to make a new obj for e then the pNext of d will point to e and pPrev pointer will also point to e and adjust the pointers of e as necessary.
Long mail aint it.
--------------------->ASH
: i need help on designing a doubly linked list in c with basic operations like navigating back and forth in the list, append, display, insert, delete elements from the list
:

Report
Re: doubly linked list in c Posted by 1234567890 on 26 May 2004 at 3:04 PM
: Hi,
: In C you have to construct a structure:
:
: struct linklist{
: char ch;
: linklist *pNext;
: linklist *pPrev;
: };
:
: Insertion and deletion from the begin or end is pretty easy. but if u want to insert/delete from the middle it is not that simple but not complex at all. whenever u insert just creat a new object of your structure and then assign its and other's pNext, pPrev pointers.
:
: as:
:
: think u have a link list: a c d g.
: Now if u had to insert e which will reside in btn d and g u have to make a new obj for e then the pNext of d will point to e and pPrev pointer will also point to e and adjust the pointers of e as necessary.
: Long mail aint it.
: --------------------->ASH
: : i need help on designing a doubly linked list in c with basic operations like navigating back and forth in the list, append, display, insert, delete elements from the list
: :
:
:

Fairly simple, but maybe this will be clearer. If you want to insert into the list, and temp points to the element BEFORE the new element, you do this...

struct linklist *temp, *new_el;
/* code to create new element and find position in list goes here */

new_el->pPrev = temp;
new_el->pNext = temp->pNext;
if(temp->next != NULL) temp->pNext->pPrev = new_el;
temp->pNext = new_el;



Just make sure whenever dealing with insertions or deletions from a linked list that all of your pointers still point to something useful when you are done. Drawing your list using boxes for elements and arrows for pointers can really help you to understand which pointers you need to change given an operation. Any more ?'s, just ask.

Joe.

Report
All u freaks, a challenge!!!! Posted by Mavrck on 30 Jan 2006 at 5:53 AM
Hi people...
My first message to the forum...
Are yaar, the question was to delete a node from a singly linked list without using any temp.variables or pointers..
i.e. delete a node, with the help of only the main that is the header pointer which is passed to the function..
Guys i have tried it for 4 days and have got an approach...
by recursion make it point to the node just before the node to be deleted, now fork a child, let the child form the new link..
put a wait for the pid of the child in the parent...
when the child.. finishes.. in the parent delete the header node in the parent after movin it to the next location... now there's lies the problem with the deleting..

Now, since i am using recurssion when i return back to my calling fun. i have my address details from the stack....
But... i have been challenged that it is impossible with one pointer.. and i told him nothing..NOTHING is impossible if u have a console and a keyboard.. so guys..
lets proove the computing power...
Do it...
Mavrck




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.