Reputation: 7
I have a simple question in linked list.
As i know if there are head node, node1 and node2, head node points node1, node1 points node2 and node2 points NULL.
But is it possible that node2 points node1, node1 points head node and head node points NULL??
Upvotes: 0
Views: 91
Reputation: 3433
it is bit unclear what you are asking, but I think i can clear things up for you.
there are two major implementation of linked lists:
each node holds a value and pointer to the Next
node
The list wrapper points to the 1stNode
the last node points to NULL
e.g.
[Node1Val | next] --> [Node2Val | next] --> NULL
each node holds a value and two pointers:
next
node in the list.previous
node in the list.e.g.
NULL <-- [prev | Node1Val | next] <--> [prev | Node2Val | next] --> NULL
Note
theoretically, there can be Cycles in a linked list, but it is not very useful to call it a linked list.
Those don't play much in real life applications, and show up only in Job-Interviews and silly questions.e.g.
--> [Node1Val | next] --> [Node2Val | next] __
\___________________________________/ :: tried to draw a looping arrow to the first.
Upvotes: 1
Reputation: 181
This is called doubly linked list you can find an example here http://linuxandc.com/doubly-linked-list/
Upvotes: 0
Reputation: 58
Generally this is called a doubly linked list where every node has a pointer to its predecessor and successor. https://www.geeksforgeeks.org/doubly-linked-list/
The beauty and peril of C is you, the programmer, decides how it should be declared. Very abstractly you could call the tail node head and this convention would hold.
Upvotes: 1