-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked List.cpp
127 lines (121 loc) · 2.3 KB
/
Linked List.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
#include<iostream>
using namespace std;
//Node Class
class node {
public:
int data;
node* next;
node(int val) {
data = val;
next = nullptr;
}
};
//linked list class
class li {
public:
li(){
head = nullptr;
}
li(int val) {
head = new node(val);
}
//will check if the list is empty or not
bool empty() {
if (head == nullptr)
return true;
return false;
}
//will insert the value at the very end
void insertAtEnd(int val) {
if (empty()) {
head = new node(val);
return;
}
node* i = head; // i is the iterator that will help us move to the last node
while (i->next != nullptr)
i = i->next;
i->next = new node(val);
}
//insert value at end
void insertAtStart(int val){
node* temp = new node(val);
temp->next = head;
head = temp;
}
//delete value at end
void deleteEnd() {
if (empty()) {
cout << "List is empty" << endl;
return;
}
node* i;
for (i = head; i->next->next != nullptr; i = i->next);
i->next = nullptr;
}
//delete value at start
void deleteStart() {
if (empty()) {
cout << "List is empty" << endl;
return;
}
head = head->next;
}
//Display
void display() {
if (empty()) {
cout << "List is empty" << endl;
return;
}
for (node* i = head; i != nullptr; i = i->next)
cout << i->data << ",";
cout << endl;
}
//Display Reverse
void reverseDisplay() {
if (empty()) {
cout << "List is empty" << endl;
return;
}
reDisplay(head);
cout << endl;
}
private:
node* head; //head pointer of node datatype
//Recursive functions to display
void reDisplay(node* temp) {
if (temp==nullptr) {
return;
}
reDisplay(temp->next);
cout << temp->data << ",";
}
};
//Function Prototypes
void listDisplay(li);
void reverseDisplay(li obj);
int main() {
li obj;
obj.insertAtEnd(1);
obj.insertAtEnd(2);
obj.insertAtEnd(3);
obj.insertAtEnd(4);
obj.insertAtEnd(5);
obj.insertAtEnd(6);
listDisplay(obj);
obj.deleteEnd();
listDisplay(obj);
obj.deleteStart();
listDisplay(obj);
obj.insertAtStart(55);
listDisplay(obj);
system("pause");
return 0;
}
void listDisplay(li obj) {
cout << "List : ";
obj.display();
}
void reverseDisplay(li obj) {
cout << "Reverse list : ";
obj.reverseDisplay();
}