k1mm1nseok
k1mm1nseok

Reputation: 7

I have a simple question about linked list in c

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

Answers (3)

Tomer W
Tomer W

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:

uni-directional

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

bi-directional (a.k.a Doubly Linked List)

each node holds a value and two pointers:

  • one to the next node in the list.
  • second to the 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

Wim
Wim

Reputation: 181

This is called doubly linked list you can find an example here http://linuxandc.com/doubly-linked-list/

Upvotes: 0

alexander-jh
alexander-jh

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

Related Questions