Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

doubly linked list in c

sunonesunone Posts: 4Member
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

Comments

  • ash1894ash1894 Posts: 26Member
    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 [b]d[/b] and [b]g[/b] u have to make a new obj for [b]e[/b] then the pNext of d will point to [b]e[/b] and pPrev pointer will also point to [b]e[/b] and adjust the pointers of [b]e[/b] 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
    :

  • 12345678901234567890 Posts: 13Member
    : 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 [b]d[/b] and [b]g[/b] u have to make a new obj for [b]e[/b] then the pNext of d will point to [b]e[/b] and pPrev pointer will also point to [b]e[/b] and adjust the pointers of [b]e[/b] 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.

  • MavrckMavrck Posts: 6Member
    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

Sign In or Register to comment.