-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodes.txt
820 lines (714 loc) · 12.9 KB
/
codes.txt
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#include<iostream>
using namespace std;
class doublyLinkedList {
private:
struct Node {
int item;
Node* next;
Node* prev;
};
Node* first;
Node* last;
int count;
public:
doublyLinkedList() {
first = NULL;
last = NULL;
count = 0;
}
bool isEmpty()
{
return (first == NULL);
}
void destroy()
{
Node *temp;
while (first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
last = NULL;
count = 0;
}
void insertLast(int val) {
Node* newNode = new Node;
newNode->item = val;
if (count == 0) {
first = last = newNode;
newNode->next = newNode->prev = NULL;
}
else {
newNode->next = NULL;
newNode->prev = last;
last->next = newNode;
last = newNode;
}
count++;
}
void insertFirst(int item)
{
Node*newNode = new Node;
newNode->item = item;
if (count == 0) {
first = last = newNode;
newNode->next = newNode->prev = NULL;
}
else {
newNode->next = first;
newNode->prev = NULL;
first->prev = newNode;
first = newNode;
}
count++;
}
void insertAt(int pos, int item)
{
if (pos < 0 || pos > count)
cout << "Out Of Range ...!" << endl;
else
{
Node *newNode = new Node;
newNode->item = item;
if (pos == 0)
insertFirst(item);
else if (pos == count)
insertLast(item);
else
{
Node *current = first;
for (int i = 1; i < pos; i++)
{
current = current->next;
}
newNode->next = current->next;
newNode->prev = current;
current->next->prev = newNode;
current->next = newNode;
count++;
}
}
}
void removeFirst()
{
if (count == 0)
cout << "Empty List" << endl;
else if (count == 1)//first == last
{
delete first;
last = first = NULL;
}
else
{
Node* current = first;
first = first->next;
first->prev = NULL;
delete current;
}
count--;
}
void deleteNthNode(int pos)
{
if (pos < 0 || pos >= count) {
cout << "Out Of Range" << endl;
return;
}
else if (pos == 0)
{
removeFirst();
}
else if (pos == count - 1)
{
removeLast();
}
else {
Node *current = first->next;
for (int i = 1; i < pos; i++)
{
current = current->next;
}
current->prev->next = current->next;
current->next->prev = current->prev;
delete current;
}
count--;
}
void removeLast()
{
if (count == 0)
cout << "Empty List" << endl;
else if (count == 1)
{
delete first;
// delete last;
last = first = NULL;
count--;
}
else
{
Node *current = last;
last = last->prev;
last->next = NULL;
delete current;
count--;
}
}
void remove(int item)
{
if (isEmpty())
{
cout << "Empty List Can't Remove ";
return;
}
Node*current = first->next;
if (first->item == item)//delete the first element, special case
{
/*
first = current->next;
if (first != NULL)
first->prev = NULL;
delete current;
return;
*/
removeFirst();
return;
}
else
{
while (current != NULL)//current->item!=element
{
if (current->item == item)
break;
current = current->next;
}
if (current == NULL) {
cout << "The item is not there\n";
return;
}
else if (current->next == NULL) {
removeLast();
return;
}
else
{
current->prev->next = current->next;
//if (current->next != NULL)
current->next->prev = current->prev;
delete current;
count--;
}
}
}
void display() {
if (isEmpty())
{
cout << "Empty List Can't Display...!";
}
else {
Node* temp = first;
while (temp != nullptr) {
cout << temp->item << " ";
temp = temp->next;
}
}
cout << endl;
}
void reverse_display() {
if (isEmpty())
{
cout << "Empty List Can't Display Reverse...!";
}
else
{
Node* temp = last;
while (temp != NULL) {
cout << temp->item << " ";
temp = temp->prev;
}
}
}
};
int main() {
doublyLinkedList dl;
dl.insertAt(0, 4);
dl.insertAt(1, 6);
dl.insertAt(2, 7);
dl.insertFirst(2);
dl.insertLast(10);
dl.remove(7);
dl.display();
dl.reverse_display();
}
------------------------------------
#include<iostream>
using namespace std;
const int MAX_SIZE = 3;
template<class t>
class stack {
int top;
t item[MAX_SIZE];
public:
stack() :top(-1) {}
bool isEmpty()
{
return top < 0;
}
void push(t Element)
{
if (top >= MAX_SIZE-1)
{
cout << "Stack full on push";
}
else
{
top++;
item[top] = Element;
}
}
void pop()
{
if (isEmpty())
cout << "stack empty on pop";
else
top--;
}
void pop(t&Element)
{
if (isEmpty())
cout << "stack empty on pop";
else {
Element = item[top];
top--;
}
}
void getTop(t&stackTop)
{
if (isEmpty())
cout << "stack empty on getTop";
else {
stackTop = item[top];
cout << stackTop << endl;
}
}
void print() {
cout << "[ ";
for (int i = top; i >= 0; i--)
{
cout << item[i] << " ";
}
cout << "]";
cout << endl;
}
};
int main() {
stack<int>s;
s.push(5);
s.push(15);
s.push(20);
s.print();
}
-------------------------------------
#include <iostream>
#include <cassert>
using namespace std;
template<class t>
class linkedQueue
{
private:
struct Node
{
t item;
Node *next;
};
int length;
Node *frontPtr, *rearPtr;
public:
linkedQueue():frontPtr(NULL), rearPtr(NULL), length(0)
{}
bool isEmpty()
{
return (length == 0);
}
void dequeue()
{
if (isEmpty())
cout << "Empty Queue" << endl;
else if (length == 1)
{
delete frontPtr;
rearPtr = NULL;
length = 0;
}
else
{
Node *current = frontPtr;
frontPtr = frontPtr->next;
delete current;//free(current)
length--;
}
}
void enqueue(t item)
{
Node *newNode = new Node;
newNode->item = item;
newNode->next = NULL;
if (length == 0)
rearPtr = frontPtr = newNode;
else
{
rearPtr->next = newNode;
rearPtr = newNode;
}
length++;
}
t front()
{
assert(!isEmpty());
return frontPtr->item;
}
t rear()
{
assert(!isEmpty());
return rearPtr->item;
}
void clearQueue()
{
Node *current;
while (frontPtr != NULL)
{
current = frontPtr;
frontPtr = frontPtr->next;
delete current;
}
rearPtr = NULL;
length = 0;
}
void display()
{
Node*cur = frontPtr;
cout << "Item in the queue :[ ";
while (cur!=NULL)
{
cout << cur->item<<" ";
cur = cur->next;
}cout << "]" << endl;
}
void search(t item)
{
Node*cur = frontPtr;
bool flag = true;
while (cur != NULL)
{
if (cur->item == item)
{
cout << "the item :" << item << " found" << endl;
flag = false;
break;
}
cur = cur->next;
}
if(flag)
cout << "the item : " << item << " not found" << endl;
}
};
int main()
{
linkedQueue<int>q1;
for (int i = 1; i <= 20; i++)
q1.enqueue(i);
cout << q1.front() << endl;
cout << q1.rear() << endl;
q1.display();
return 0;
}
-----------------------------------------
#include <iostream>
#include <cassert>
using namespace std;
class arrayQueueType
{
int rear;
int front;
int length;
int *arr;
int maxSize;
public:
arrayQueueType(int size) {
if (size <= 0)
maxSize = 100;
else
maxSize = size;
front = 0;
arr = new int[maxSize];
rear = maxSize - 1;
length = 0;
}
int isEmpty()
{
return length == 0;
}
bool isFull()
{
return length == maxSize;
}
void addQueue(int Element)
{
if (isFull())
{
cout << "Queue Full Can't Enqueue ...!";
}
else
{
rear = (rear + 1) % maxSize;
arr[rear] = Element;
length++;
}
}
void deleteQueue()
{
if (isEmpty())
{
cout << "Empty Queue Can't Dequeue ...!";
}
else
{
front = (front + 1) % maxSize;
--length;
}
}
int frontQueue()
{
assert(!isEmpty());
return arr[front];
}
int rearQueue()
{
assert(!isEmpty());
return arr[rear];
}
void printQueue()
{
if (!isEmpty()) {
for (size_t i = front; i != rear; i = (i + 1) % maxSize)
{
cout << arr[i] << " ";
}
cout << arr[rear];
}
else
cout << "Empty Queue";
}
int queueSearch(int element) {
int pos = -1;
if (!isEmpty())
{
for (int i = front; i != rear; i = (i + 1) % maxSize)
if (arr[i] == element)
{
pos = i;
break;
}
if (pos == -1)
{
if (arr[rear] == element)
pos = rear;
}
}
else
cout << "Q is empty ! " << endl;
return pos;
}
~arrayQueueType() {
delete[]arr;
}
};
int main()
{
arrayQueueType q1(5);
q1.addQueue(10);
q1.addQueue(20);
q1.addQueue(30);
q1.addQueue(40);
q1.addQueue(50);
q1.printQueue();
return 0;
}
-----------------------------------------
#include <iostream>
#include <cassert>
using namespace std;
class arrayListType
{
public:
arrayListType(int size = 100);
arrayListType(arrayListType& otherList); //copy constructor
~arrayListType(); // destructor
bool isEmpty();
bool isFull();
int listSize();
int maxListSize();
void print();
bool isItemAtEqual(int loc, int item);
void insertAt(int loc, int item);
void insertEnd(int item);
void removeAt(int loc);
void retrieveAt(int loc, int& item);
void replaceAt(int loc, int item);
void clearList();
int seqSearch(int item);
void insertNoDuplicate(int item);
void remove(int item);
private:
int *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
arrayListType::arrayListType(int size)
{
/* initilize the private members */
if(size <= 0)
{
cout << " Wrong Size " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new int [maxSize];
assert(list != NULL); //terminate if unable to allocate memory space
}
arrayListType::arrayListType(arrayListType& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new int [maxSize]; //create the array
assert(list != NULL); //terminate if unable to allocate memory space
for(int j = 0; j < length; j++) //copy otherList
list [j] = otherList.list[j];
}
arrayListType::~arrayListType()
{
delete [] list;
}
bool arrayListType::isEmpty()
{
return (length == 0);
}
bool arrayListType::isFull()
{
return (length == maxSize);
}
int arrayListType::listSize()
{
return length;
}
int arrayListType::maxListSize()
{
return maxSize;
}
void arrayListType::print()
{
for(int i = 0; i < length; i++)
cout<<list[i]<<" ";
cout<<endl;
}
bool arrayListType::isItemAtEqual(int loc, int item)
{
if(loc < 0 || loc >= length)
return false;
else
return (list[loc] == item);
}
void arrayListType::insertAt(int loc, int item)
{
if(isFull())
cout<<" The List is Full " << endl;
else if(loc < 0 || loc > length)
cout << "Out of Range " << endl;
else
{
for(int i = length; i > loc; i--)
list[i] = list[i - 1]; //shift right
list[loc] = item; //insert the item at the specified position
length++; //increment the length
}
}
void arrayListType::insertEnd(int item)
{
if(isFull())
cout<<" The List is Full " << endl;
else
list[length++] = item;
}
void arrayListType::retrieveAt(int loc, int& item)
{
if(loc < 0 || loc >= length)
cout << "Out of Range " << endl;
else
item = list[loc];
}
void arrayListType::replaceAt(int loc, int item)
{
if(loc < 0 || loc >= length)
cout << "Out of Range " << endl;
else
list[loc] = item;
}
void arrayListType::clearList()
{
length = 0;
}
int arrayListType::seqSearch(int item)
{
for(int loc = 0; loc < length; loc++)
if(list[loc] == item)
return loc;
return -1;
}
void arrayListType::insertNoDuplicate(int item)
{
if(isFull())
cout<<" The List is Full " << endl;
else
{
int flag = seqSearch(item);
if(flag == -1)
list[length++] = item;
else
cout<<"No duplicates are allowed."<<endl;
}
}
void arrayListType::remove(int item)
{
int loc = seqSearch(item);
if(loc == -1)
cout<<"The item to be deleted is not in the list" << endl;
else
removeAt(loc);
}
void arrayListType::removeAt(int loc)
{
if(loc < 0 || loc >= length)
cout<<"The location of the item to be removed is out of range."<<endl;
else
{
for(int i = loc; i < length - 1; i++)
list[i] = list[i+1];
length--;
}
}
int main()
{
arrayListType lst1;
for(int i = 0; i < 20; i++)
lst1.insertAt(i, i * i);
lst1.print();
int x;
lst1.retrieveAt(10, x);
cout<< x << endl;
arrayListType lst2(lst1);
lst2.print();
return 0;
}
----------------------------------------------