-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.h
130 lines (92 loc) · 1.8 KB
/
List.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
#pragma once
#include <iostream>
using namespace std;
template<class T>
class Node {
public:
T data;
Node* next, * down;
Node() : data(0), next(NULL), down(NULL) {}
Node(T data) : data(data), next(NULL), down(NULL) {}
};
template<class T>
class List {
public:
Node<T>* Head, * last;
List() {
Head = NULL;
last = NULL;
}
List(T data) {
Node<T>* temp = new Node<T>(data);
Head = temp;
last = temp;
}
void InsertNode(T data) {
Node<T>* temp = new Node<T>(data);
if (Head == NULL) {
Head = temp;
last = Head;
}
else {
last->down = temp;
temp->down = NULL;
last = temp;
}
}
bool isEmpty() {
if (Head == NULL) {
return true;
}
return false;
}
Node<T>* operator [](int index) {
Node<T>* temp = Head;
for (int i = 0; i < index; i++)
temp = temp->down;
return temp;
}
void Display() {
if (Head == NULL) {
cout << "List is empty." << endl;
}
else {
Node<T>* tempList = Head;
Node<T>* tempFreinds = tempList->next;
while (tempList != NULL) {
cout << tempList->data << " --> ";
while (tempFreinds != NULL) {
cout << tempFreinds->data;
if (tempFreinds->next != NULL)
cout << " -> ";
tempFreinds = tempFreinds->next;
}
cout << endl;
tempList = tempList->down;
if (tempList != NULL)
tempFreinds = tempList->next;
}
cout << endl;
}
}
void destroyEdges() {
Node<T>* tempNode = Head;
Node<T>* previous = NULL;
while (tempNode != NULL) {
Node<T>* tempEdges = tempNode->next;
while (tempEdges != NULL) {
previous = tempEdges;
tempEdges = tempEdges->next;
delete(previous);
}
tempNode = tempNode->down;
}
}
void destroyVertices(Node<T> * temp) {
if (temp != NULL) {
destroyVertices(temp->down);
delete(temp);
}
Head = NULL;
}
};