-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcalculator
178 lines (178 loc) · 3.33 KB
/
calculator
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
// Code By- Romijul Laskar
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;
class node
{
public:
int data;
node *link;
node(int val) //construct
{
data = val;
link = NULL;
}
};
void display(node *head);
bool search(node *head, int val);
void insert(node *&head, int val)
{
node *n = new node(val);
if (head == NULL)
{
head = n;
return;
}
node *temp = head;
while (temp->link != NULL)
{
temp = temp->link;
}
temp->link = n;
}
void insertBeg(node *&head, int val)
{
node *n = new node(val);
n->link = head;
head = n;
cout << val << " inserted at the beginning.\n";
return;
}
void insert(node *&head)
{
int val, pos;
cout << "Enter position you wants to insert- ";
cin >> pos;
cout << "Enter number - ";
cin >> val;
if(pos==1)
{
insertBeg(head,val);
return;
}
node *temp = head;
node *cur = new node(val);
while (pos != 2)
{
temp = temp->link;
pos--;
}
cur->link = temp->link;
temp->link = cur;
}
void del(node *head)
{
int val;
cout << "\nEnter that number you want to delete- ";
cin >> val;
if (search(head, val))
{
node *temp = head;
if (temp->data == val)
{
temp = temp->link;
display(temp);
exit(0);
}
while (temp->link->data != val)
{
temp = temp->link;
}
temp->link = temp->link->link;
}
else
{
cout << "Key Not Present !!\n";
}
}
void display(node *head)
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data;
if (temp->link != NULL)
{
cout << " -> ";
}
temp = temp->link;
}
cout << endl;
return;
}
bool search(node *head, int val)
{
int pos = 1;
node *temp = head;
while (temp != NULL)
{
if (temp->data == val)
{
cout << "At posintion " << pos << endl;
return true;
}
temp = temp->link;
pos++;
}
return false;
}
void searchKey(node *head)
{
int key;
cout << "Enter Key To be Search - ";
cin >> key;
if (search(head, key))
{
cout << " Key Found" << endl;
}
else
{
cout << "Key Not Found";
}
}
int main()
{
system("CLS");
cout<<"********* LINKED lIST *************\n";
int n, i, c;
node *head = NULL;
cout << "Enter number of node - ";
cin >> n;
// random function
srand(time(0)); // Use current time as seed for random generator
for (i = 0; i < n; i++)
{
insert(head, (rand() % 50));
}
display(head);
here:
cout << "\n1- Insert at the begining\n2- Insert at given position\n3- Search a Key\n4- display\n5- Delete a given key\n0- Exit\n Your Choice -";
cin >> c;
switch (c)
{
case 1:
insertBeg(head, (rand() % 50));
display(head);
break;
case 2:
insert(head);
break;
case 3:
searchKey(head);
break;
case 4:
display(head);
break;
case 5:
del(head);
display(head);
break;
case 0:
return 0;
default:
cout << setw(50) << "Invalid Choice\n";
break;
}
goto here;
}