-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist (1).cpp
220 lines (201 loc) · 5.21 KB
/
linkedlist (1).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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>
#include <cstdlib>
using namespace std;
struct node {
int a;
node *next;
} Node;
class linkedList {
public:
node *head;// head pointer
node *tail;// tail pointer
//default constructor
linkedList(){
head = NULL;
tail = NULL;
}
//parametrised constructor
linkedList(const linkedList& List2) {
head = List2.head;
tail = List2.tail;
}
//operator overloading
void operator = (const linkedList &List2){
this->head =List2.head;
this->tail=tail;
}
//inserting number at the front
void insertAtFront (int number) {
node *b = new node();
b->a = number;
b->next = head;
head = b;
}
//inserting number at the back
void insertAtBack (int number) {
node *b = new node();
b->a = number;
b->next = NULL;
if (head == NULL) {
head = b;
tail = b;
b = NULL;
}else {
tail->next = b;
tail = b;
}
}
//searching
void search(){
node *ptr = new node;
int item,i=0,flag;
ptr = head;
if(ptr == NULL){
cout<<"Enter an item.\n";
} else {
cout<<"Enter item which you want to search:\n";
cin>>item;
while (ptr!=NULL){
if(ptr->a == item){
cout<<"Found at node of location "<<(i+1)<<endl;
flag=0;
break;
}else {
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1){
cout<<"Not found!\n";
}
}
}
void output(){
node *temp=new node;
temp = head;
if (head == NULL) {
cout<<"Error"<<endl;
return;
}else {
cout<<"The current linked list elements are:"<<endl;
while(temp!=NULL){
cout<<temp->a<<"\t";
temp=temp->next;
}
}
}
//Constructor that generates a linked list of random integers
linkedList(int listSize) {
int i=0;
while (i < listSize) {
insertAtBack (rand());
i++;
}
output();
}
//removing duplicate numbers
void removeDuplicate() {
node *current = new node;
node *index = new node;
node *temp = new node;
current = head, index = NULL, temp = NULL;
if(head == NULL) {
return;
}
else {
while(current != NULL){
temp = current;
index = current->next;
while(index != NULL) {
if(current->a == index->a) {
temp->next = index->next;
}
else {
temp = index;
}
index = index->next;
}
current = current->next;
}
output();
}
}
//reversing
void reverse_list() {
if (head == NULL) return;
node *temp = new node;
node *prev = new node;
node *current = new node;
temp = NULL;
prev = NULL;
current = head;
while(current != NULL) {
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
head = prev;
}
};
int main() {
int size_of_list, choice, insertedNum;
cout<<"Enter the number of integers to be generated:";
cin>>size_of_list;
linkedList List(size_of_list);
cout<<"Choose any of the following:"<<endl;
cout<<"1.Node at the front."<<endl;
cout<<"2.Node at the end."<<endl;
cout<<"3.Display Linked List"<<endl;
cout<<"4.Search Element"<<endl;
cout<<"5.Reverse Linked List "<<endl;
cout<<"6.Remove duplicates from the list."<<endl;
cout<<"7.Exit "<<endl;
while (1){
cout<<"Enter:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the number to insert at the front:"<<endl;
cin>>insertedNum;
List.insertAtFront (insertedNum);
cout<<endl;
break;
case 2:
cout<<"Enter the number to insert at the end"<<endl;
cin>>insertedNum;
List.insertAtBack (insertedNum);
cout<<endl;
break;
case 3:
List.output();
cout<<endl;
break;
case 4:
List.search();
cout<<endl;
break;
case 5:
List.reverse_list();
cout<<endl;
break;
case 6:
cout<<"Removing duplicates"<<endl;
List.removeDuplicate();
cout<<endl;
break;
case 7:
cout<<"\nEnter a number you want to delete: "<<endl;
cin>>insertedNum;
break;
case 8:
cout<<"End!"<<endl;
exit(1);
break;
default:
cout<<"Invalid input!"<<endl;
}
}
}