Skip to content

Commit 0694c7f

Browse files
Doubly Linked List Implementation and Printing it in both ways (normal, reverse)
1 parent e186830 commit 0694c7f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

doubly_linked_list.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Node
5+
{
6+
public:
7+
int val;
8+
Node *prev;
9+
Node *next;
10+
Node(int val)
11+
{
12+
this->val = val;
13+
this->prev = NULL;
14+
this->next = NULL;
15+
}
16+
};
17+
18+
void print_normal(Node *head)
19+
{
20+
Node *tmp = head;
21+
while (tmp != NULL)
22+
{
23+
cout << tmp->val << " ";
24+
tmp = tmp->next;
25+
}
26+
cout << endl;
27+
}
28+
29+
void print_reverse(Node *tail)
30+
{
31+
Node *tmp = tail;
32+
while (tmp != NULL)
33+
{
34+
cout << tmp->val << " ";
35+
tmp = tmp->prev;
36+
}
37+
cout << endl;
38+
}
39+
40+
int main()
41+
{
42+
Node *head = new Node(10);
43+
Node *a = new Node(20);
44+
Node *b = new Node(30);
45+
Node *tail = b;
46+
47+
head->next = a;
48+
a->prev = head;
49+
a->next = b;
50+
b->prev = a;
51+
52+
print_normal(head);
53+
print_reverse(tail);
54+
55+
return 0;
56+
}

doubly_linked_list.exe

51.3 KB
Binary file not shown.

output.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
10 20 30
2+
30 20 10

0 commit comments

Comments
 (0)