-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
205 lines (171 loc) · 5.21 KB
/
main.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *prev;
Node *next;
Node(){
prev= next = NULL ;
};
// Node(int input){
// data = input ;
// Prev =Next = NULL ;
//
// }
};
class LinkedList
{
private:
Node *Head, *Tail;
static int instanceSize ;
public :
//initiate head and tail in order to know if linkedlist is empty or not
LinkedList(){
Head = Tail = NULL ;
}
//create anew node in the heap instead of stack + initiate its values + returns it
static Node* create(int data){
Node* NodeObjAddress ;
NodeObjAddress = new Node();
NodeObjAddress->data = data ;
return NodeObjAddress ;
};
//
void add(Node *NodeObjAddress) {
//check if linked list is empty
if (Head == NULL && Tail ==NULL)
{
Head = Tail = NodeObjAddress ;
}
else{
//we catch previous node by tail value so we change it in the very end of else block
Tail->next = NodeObjAddress ;
NodeObjAddress-> prev= Tail ;
Tail = NodeObjAddress ;
}
}
//displaying data member value of all node objects that are inside the linked list
void displayData(){
// we create a pointer carries the same address that Head carry & we didn't use head itself to avoid changing it
Node* iteratorPointer = Head ;
//check if the next node of the current node equals null = check if the linked list is ended
while(iteratorPointer != NULL )
{
cout<<endl<<iteratorPointer->data ;
iteratorPointer =iteratorPointer->next;
}
}
//searching for specified data member value of in all node objects that are inside the linked list
//like display but we only return nodes that meets the condition
int calcInstanceSize(){
Node* iteratorPointer = Head ;
int instanceSize = 0 ;
while (iteratorPointer != NULL)
{
instanceSize++ ;
iteratorPointer = iteratorPointer->next ;
}
return instanceSize ;
}
//returns a pointer to array of pointers of all nodes available for instance in the linkedlist
//so you can use it as an array for instance
//notice : you must pass it apointer to array of pointers of node with the same size of the linkedlist
//ex :Node * arr[x.calcInstanceSize()]
Node** actAsArray(Node* arr[]){
Node* iteratorPointer = Head ;
int iteration = 0 ;
while (iteratorPointer != NULL)
{
arr[iteration] = iteratorPointer;
iteration++ ;
iteratorPointer = iteratorPointer->next ;
}
return arr ;
}
// returns a linked list that have all nodes which thier data match the input
LinkedList searchForList(int dataInp){
LinkedList *matched = new LinkedList() ;
Node* iteratorPointer = Head ;
while (iteratorPointer != NULL)
{
if(iteratorPointer->data == dataInp){
matched->add(LinkedList::create(dataInp));
}
iteratorPointer = iteratorPointer->next ;
}
matched->displayData();
return *matched;
}
//returns the first node that its data matches the input
Node* Search(int data)
{
Node *current = Head;
while(current != NULL)
{
if(current->data == data)
return current;
current = current->next;
}
return NULL;
}
// after you search for a node by its data, you can delete it
void Delete (Node *deleted) {
//if there was one node in the tree
if(deleted == Head && deleted ==Tail)
{
Head=Tail =NULL ;
}
else if(deleted ==Tail){
Tail = deleted->prev ;
deleted->prev->next = NULL;
}
else if (deleted == Head){
Head = deleted->next ;
deleted->next->prev = NULL;
}
else
{
deleted->prev->next = deleted->next ;
deleted->next->prev = deleted->prev ;
}
deleted->next = NULL;
deleted->prev = NULL;
}
//reset linkedlist =delete all nodes on the linked list
void deleteAll(){
Node * iteratorNode = Head ;
while(iteratorNode != NULL) {
// iteratorNode->data =
iteratorNode->prev= NULL;
Node* nextNode = iteratorNode->next ;
iteratorNode->next = NULL;
Head = NULL ;
iteratorNode = nextNode;
}
Tail =NULL ;
}
//insert after
//display
};
int main()
{
LinkedList x ;
x.add(LinkedList::create(10));
x.add(LinkedList::create(20));
x.add(LinkedList::create(20));
// x.displayData();
// Node** ptr = x.search(20) ;
Node * arr[x.calcInstanceSize()] ;
Node **arrOfLinkedList = x.actAsArray(arr);
cout<<arrOfLinkedList[0]->data<<endl;
cout <<x.calcInstanceSize()<< endl;
LinkedList newList = x.searchForList(10);
cout <<newList.calcInstanceSize()<< endl;
Node * y =x.Search(20) ;
x.Delete(y);
x.displayData();
//y.displayData();
return 0;
}