-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublyLinkedList.h
171 lines (119 loc) · 2.89 KB
/
doublyLinkedList.h
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef DOUBLYLINKEDLIST_H
#define DOUBLYLINKEDLIST_H
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
// node
struct node
{
int data;
node *next;
node *prev;
};
// doubly linked list
class DoublyLinkedList
{
private:
node *head;
node *prev;
public:
DoublyLinkedList()
{
head = NULL;
prev = NULL;
}
node* generateHead(int data)
{
// making a new node
node *tmp = new node;
// adding data
tmp->data = data;
tmp->next = NULL;
tmp->prev = NULL;
// making the new node as head
head = tmp;
return head;
}
};
// function to add to the beginning of the doubly linked list
void addToBeginning_doublyLinkedList(node* &head , int data)
{
// new node
node* newNode = new node();
// tmp node which has contents of head
node* tmp = head;
// new node init
newNode->data = data;
newNode->prev = NULL;
newNode->next = head;
// head prev will point to new node
tmp->prev = newNode;
// new node will become head
head = newNode;
}
// function to display elements
// pass the head of the linked list
// things will be seperated by the string seperator
void displayFromHead_doublyLinkedList(node* head , string seperator = " ")
{
// new node points to head of linked list
node *tmp;
tmp = head;
// if the list as no further node
while (tmp != NULL)
{
cout << tmp->data <<seperator;
// making the node to node pointed by tmp next
tmp = tmp->next;
}
}
// function to display elements
// pass the tail of the linked list
// things will be seperated by the string seperator
void displayFromTail_doublyLinkedList(node* tail , string seperator = " ")
{
// new node points to head of linked list
node *tmp;
tmp = tail;
// if the list as no further node
while (tmp != NULL)
{
cout << tmp->data <<seperator;
// making the node to node pointed by tmp next
tmp = tmp->prev;
}
}
// function to find the size of linked list
// keep one NULL and pass the value of other
int size_doublyLinkedList(node* head = NULL , node* tail = NULL)
{
// new node points to head of linked list
node *tmp;
int size = 0;
// if the tail is provided
if(head == NULL)
{
tmp = tail;
while (tmp != NULL)
{
size++;
// making the node to node pointed by tmp next
tmp = tmp->prev;
}
}
// if the head is provided
if(tail == NULL)
{
tmp = head;
// if the list as no further node
while (tmp != NULL)
{
size++;
// making the node to node pointed by tmp next
tmp = tmp->next;
}
}
return size;
}
#endif