Skip to content

Commit 708f6a1

Browse files
committed
Adds display double circular ll and get middle element of circular double linked list solutions
1 parent 20f9af3 commit 708f6a1

2 files changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://practice.geeksforgeeks.org/problems/display-circular-doubly-linked-list/1/?track=DSA-Foundation-Final-DLL&batchId=193
2+
3+
void displayList(Node *head)
4+
{
5+
//Head to Tail
6+
Node *current = head;
7+
while(current->next != head)
8+
{
9+
cout << current->data << " ";
10+
current = current->next;
11+
}
12+
cout<<current->data<<endl;
13+
//Tail to Head
14+
while(current != head)
15+
{
16+
cout << current->data << " ";
17+
current = current->prev;
18+
}
19+
cout <<current->data;
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://practice.geeksforgeeks.org/problems/find-middle-of-circular-doubly-linked-list/1/?track=DSA-Foundation-Final-DLL&batchId=193
2+
3+
// Count number of nodes present in the circular double linked list
4+
int count_number_of_nodes(Node *head)
5+
{
6+
Node *current_node = head;
7+
int counter = 0;
8+
while(current_node->next != head)
9+
{
10+
counter++;
11+
current_node = current_node ->next;
12+
}
13+
return counter;
14+
}
15+
16+
int findMiddle(Node * head)
17+
{
18+
Node *current = head;
19+
int number_nodes = count_number_of_nodes(current);
20+
// Get position of the middle element of the linked list
21+
int position = (number_nodes/2)+1;
22+
23+
current = head;
24+
// Iterate until we reach the middle element
25+
while(position > 1)
26+
{
27+
position--;
28+
current = current->next;
29+
}
30+
// Return value of the middle element
31+
return current->data;
32+
}

0 commit comments

Comments
 (0)