-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll3.cpp
164 lines (133 loc) · 2.62 KB
/
ll3.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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) { this->data = data; }
Node() : data(0), next(NULL) {}
};
class LinkedList {
private:
Node *head;
int length;
public:
LinkedList() : head(0), length(0) {}
LinkedList(const LinkedList &original);
~LinkedList() { deleteList(); }
void insertAtFirst(int data);
void deleteFromFront();
void front();
void back();
void center();
void deleteList();
inline void lengthOfList() { cout << length << endl; }
void print();
};
LinkedList::LinkedList(const LinkedList &original) : head(NULL) {
cout << endl << original.length;
this->head = NULL;
Node *ptr = original.head;
Node *newPtr, *lastPtr;
cout << this->length;
//node->head = original.head;
while(ptr != NULL) {
newPtr = new Node(ptr->data);
if(head == 0 )
head = newPtr;
else {
lastPtr->next = newPtr;
lastPtr = newPtr;
ptr = ptr->next;
}
}
}
void LinkedList::deleteList() {
Node *currentptr = this->head;
Node *temp = new Node();
while(currentptr != 0) {
temp = currentptr;
currentptr = currentptr->next;
delete temp;
}
}
void LinkedList::insertAtFirst(int data) {
Node *node = new Node();
length++;
if(head == NULL) {
head = node;
node->data = data;
node->next = NULL;
} else {
Node *temp;
//Node *node = new Node();
temp = head;
head = node;
node->data = data;
node->next = temp;
}
}
void LinkedList::deleteFromFront() {
if(head == NULL)
cout << "The list is already Empty";
else {
Node *temp = head;
head = temp->next;
length--;
}
}
void LinkedList::front() {
Node *temp = head;
cout << temp->data;
}
void LinkedList::back() {
Node *temp, *temp2;
temp = head;
temp2 = head->next;
while(temp2 != NULL) {
temp = temp->next;
temp2 = temp2->next;
}
cout << temp->data;
}
void LinkedList::center() {
Node *ptr = head;
Node *ptr2 = ptr->next;
while(ptr2 != NULL) {
ptr = ptr->next;
ptr2 = ptr2->next->next;
}
cout << ptr->data << endl;
}
void LinkedList::print() {
Node *temp;
temp = head;
cout << "Printing";
while(temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
delete temp;
}
int main() {
LinkedList *list = new LinkedList();
list->insertAtFirst(1);
list->insertAtFirst(2);
list->insertAtFirst(3);
list->insertAtFirst(4);
list->insertAtFirst(5);
list->insertAtFirst(6);
list->insertAtFirst(7);
list->insertAtFirst(8);
list->insertAtFirst(9);
list->deleteFromFront();
LinkedList *l2(list); //= new LinkedList();
//list->front();
//list->back();
//list->center();
list->print();
cout << "\n\n\n\n";
l2->print();
list->lengthOfList();
delete list;
}