Doubly Linked List with separate left and right node but same tail

453 views Asked by At

I'm working on double linked list where the head has to possessing 2 "hands" , left and right, and both hands must end up at the same tail. And in the end, it should be able to print the value of

  • Head - Left - Tail
  • Head - Right - Tail

Like Binary Searching but must end up at the same tail.

I'm a little bit confuse about what should I use for create left and right node. I've tried using a lot of middle insertion methods, and after a week I still can't figure out how to get the print out I want.

I tried to use Binary Searching method too, reference HERE, but can't figure out how to make this one tail that all the hands should end up to.

This is how I write my Struct

struct City
{
    char name[20], place[20], food[20];
    struct City *next;
}
*head, *tail, *new, *right,*left,*del_temp, *temp, *temp2;

I used this one too... but still not work out

struct City
{
    char name[20], place[20], food[20];
    struct City *right;
    struct City *left;
}
*head, *tail, *new, *del_temp, *temp, *temp2;

Sorry for lacking code, I made several project with each different insertion methods. I'm not sure which one to put here.

My question is, how should I go from here ? Should I use tree and subtrees for left and right hand ? what the algorithm I should use ?

If I were to continue with double linked list, how do I get the separate output for both left and right hand? Or if I were to continue with binary searching, how do I get the exact one tail in the end ?

Any advice ?

Thanks before

2

There are 2 answers

0
Puddinglord On

You would just need to use a circularly doubly linked list so it would go

                               Head
                         Left        Right
                               Tail

Link each one to the left and right of each other back and forth creating the double link and then what you can do is when you want to right the right side just start from the head, go to the right hand and then print and stop at the tail. Same would go for the left and if you need to start with the tail you can do that too since you have a unique pointer for it.

https://i.stack.imgur.com/SCNzn.png

a badly drawn picture but with arrows!

3
jwd On

This is more of a comment, but is this the type of structure you want?

         .-> N1 <---> N2 <---> N3 <-.
Head <__/                            \ __> Tail
        \.-> N4 <---> N5 <---> N6 <-./

I'm not sure exactly what your requirements are.