-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublyll.cpp
143 lines (127 loc) · 2.47 KB
/
doublyll.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
struct Node
{
string order; // order
Node* prev; // A reference to the previous Node
Node* next; // A reference to the next Node
};
class Doubly_Linked_List
{
Node* front; // points to first Node of list
Node* end; // points to first las of list
public:
Doubly_Linked_List()
{
front = NULL;
end = NULL;
}
void add_front(string);
void add_after(Node*, string);
void add_before(Node*, string);
void add_end(string);
void delete_Node(Node*);
void forward_traverse();
void backward_traverse();
};
void Doubly_Linked_List::add_front(string d)
{
// Creating new Node
Node* temp;
temp = new Node();
temp->order = d;
temp->prev = NULL;
temp->next = front;
// List is empty
if (front == NULL)
end = temp;
else
front->prev = temp;
front = temp;
}
void Doubly_Linked_List::add_before(Node* n, string d)
{
Node* temp;
temp = new Node();
temp->order = d;
temp->next = n;
temp->prev = n->prev;
n->prev = temp;
//if Node is to be inserted before first Node
if (n->prev == NULL)
front = temp;
}
void Doubly_Linked_List::add_after(Node* n, string d)
{
Node* temp;
temp = new Node();
temp->order = d;
temp->prev = n;
temp->next = n->next;
n->next = temp;
//if Node is to be inserted after last Node
if (n->next == NULL)
end = temp;
}
void Doubly_Linked_List::add_end(string d)
{
// create new Node
Node* temp;
temp = new Node();
temp->order = d;
temp->prev = end;
temp->next = NULL;
// if list is empty
if (end == NULL)
front = temp;
else
end->next = temp;
end = temp;
}
void Doubly_Linked_List::delete_Node(Node* n)
{
// if Node to be deleted is first Node of list
if (n->prev == NULL)
{
front = n->next; //the next Node will be front of list
front->prev = NULL;
}
// if Node to be deleted is last Node of list
else if (n->next == NULL)
{
end = n->prev; // the previous Node will be last of list
end->next = NULL;
}
else
{
//previous Node's next will point to current Node's next
n->prev->next = n->next;
//next Node's prev will point to current Node's prev
n->next->prev = n->prev;
}
//delete Node
delete(n);
}
void Doubly_Linked_List::forward_traverse()
{
Node* trav;
trav = front;
while (trav != NULL)
{
cout << trav->order << endl;
trav = trav->next;
}
}
void Doubly_Linked_List::backward_traverse()
{
Node* trav;
trav = end;
while (trav != NULL)
{
cout << trav->order << endl;
trav = trav->prev;
}
}