forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriply_Linked_List.cpp
More file actions
149 lines (145 loc) · 3.28 KB
/
Copy pathTriply_Linked_List.cpp
File metadata and controls
149 lines (145 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int c = 0;
struct node
{
node *next, *prev, *top;
int data;
}*head = NULL, *tail = NULL, *p = NULL, *r = NULL, *np = NULL, *q = NULL;
void create(int x)
{
np = new node;
np->data = x;
np->next = NULL;
np->prev = NULL;
np->top = NULL;
if (c == 0)
{
tail = np;
head = np;
p = head;
p->next = NULL;
p->prev = NULL;
p->top = NULL;
c++;
}
else
{
p = head;
r = p;
if (np->data < p->data)
{
np->next = p;
p->prev = np;
np->prev = NULL;
head = np;
p = head;
do
{
p = p->next;
}
while (p->next != NULL);
tail = p;
}
else if (np->data > p->data)
{
while (p != NULL && np->data > p->data)
{
r = p;
p = p->next;
if (p == NULL)
{
r->next = np;
np->prev = r;
np->next = NULL;
tail = np;
break;
}
else if (np->data <= p->data)
{
if (np->data < p->data)
{
r->next = np;
np->prev = r;
np->next = p;
p->prev = np;
if (p->next != NULL)
{
do
{
p = p->next;
}
while (p->next !=NULL);
}
tail = p;
break;
}
else if (p->data == np->data)
{
q = p;
while (q->top != NULL)
{
q = q->top;
}
q->top = np;
np->top = NULL;
break;
}
}
}
}
}
}
void traverse_tail()
{
node *t = tail;
//cout<<"\n\nlinear display of nodes currently present in linked list....\n\n";
while (t != NULL)
{
cout<<t->data<<"\t";
q = t;
while (q->top != NULL)
{
q = q->top;
cout<<"top->"<<q->data<<"\t";
}
t = t->prev;
}
cout<<endl;
}
void traverse_head()
{
node *t = head;
while (t != NULL)
{
cout<<t->data<<"\t";
q = t;
while (q->top != NULL)
{
q = q->top;
cout<<"top->"<<q->data<<"\t";
}
t = t->next;
}
cout<<endl;
}
int main()
{
int i = 0, n, x, ch;
cout<<"enter the no of nodes\n";
cin>>n;
while (i < n)
{
cout<<"\nenter value of node\n";
cin>>x;
create(x);
i++;
}
cout<<"\nTraversing Doubly Linked List head first\n";
traverse_head();
cout<<"\nTraversing Doubly Linked List tail first\n";
traverse_tail();
getch();
}