├── Chapter 2 - Arrays ├── Programming Project 2.6 │ ├── bin │ │ ├── HighArray.class │ │ └── HighArrayApp.class │ └── src │ │ └── HighArrayApp.java ├── Programming Projects 2.1-2.3 │ ├── bin │ │ ├── HighArray.class │ │ └── HighArrayApp.class │ └── src │ │ └── HighArrayApp.java └── Programming Projects 2.4 - 2.5 │ ├── bin │ ├── OrdArray.class │ └── OrderedApp.class │ └── src │ └── OrderedApp.java ├── Chapter 3 - Simple Sorting ├── Progamming Project 3.1 │ ├── bin │ │ ├── ArrayBub.class │ │ └── BubbleSortApp.class │ └── src │ │ └── BubbleSortApp.java ├── Progamming Project 3.2 │ ├── bin │ │ ├── ArrayIns.class │ │ └── InsertSortApp.class │ └── src │ │ └── InsertSortApp.java ├── Progamming Project 3.3 │ ├── bin │ │ ├── ArrayIns.class │ │ └── InsertSortApp.class │ └── src │ │ └── InsertSortApp.java ├── Progamming Project 3.4 │ ├── bin │ │ ├── ArrayBub.class │ │ └── BubbleSortApp.class │ └── src │ │ └── BubbleSortApp.java ├── Programming Project 3.5 │ ├── bin │ │ ├── ArrayIns.class │ │ └── InsertSortApp.class │ └── src │ │ └── InsertSortApp.java └── Programming Project 3.6 │ ├── bin │ ├── ArrayIns.class │ └── InsertSortApp.class │ └── src │ └── InsertSortApp.java ├── Chapter 4 - Stacks and Queues ├── Programming Project 4.1 │ ├── .classpath │ ├── .project │ ├── bin │ │ ├── Queue.class │ │ └── QueueApp.class │ └── src │ │ └── QueueApp.java ├── Programming Project 4.2 │ ├── .classpath │ ├── .idea │ │ ├── .name │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── .project │ ├── Challenge 4.2.iml │ ├── bin │ │ ├── Deque.class │ │ └── DequeApp.class │ └── src │ │ └── DequeApp.java ├── Programming Project 4.3 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── Programming Project 4.3.iml │ ├── out │ │ └── production │ │ │ └── Programming Project 4.3 │ │ │ ├── Deque.class │ │ │ ├── DequeStackApp.class │ │ │ └── Stack.class │ └── src │ │ └── DequeStackApp.java ├── Programming Project 4.4 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── Programming Project 4.4.iml │ ├── out │ │ └── production │ │ │ └── Programming Project 4.4 │ │ │ ├── PriorityQ.class │ │ │ └── PriorityQApp.class │ └── src │ │ └── PriorityQApp.java └── Programming Project 4.5 │ ├── .classpath │ ├── .idea │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml │ ├── .project │ ├── Programming Project 4.5.iml │ ├── out │ └── production │ │ └── Programming Project 4.5 │ │ ├── CheckoutLineApp.class │ │ ├── Queue.class │ │ └── customer.class │ └── src │ └── CheckoutLineApp.java ├── Chapter 5 - Linked Lists ├── ProgrammingProject5.1 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── ProgrammingProject5.1.iml │ ├── out │ │ └── production │ │ │ └── ProgrammingProject5.1 │ │ │ ├── Link.class │ │ │ ├── LinkedListPriorityQ.class │ │ │ ├── PriorityQ.class │ │ │ └── SortedList.class │ └── src │ │ └── LinkedListPriorityQ.java ├── ProgrammingProject5.2 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── ProgrammingProject5.2.iml │ ├── out │ │ └── production │ │ │ └── ProgrammingProject5.2 │ │ │ ├── Deque.class │ │ │ ├── DoublyLinkedList.class │ │ │ ├── Link.class │ │ │ └── LinkedListDeque.class │ └── src │ │ └── LinkedListDeque.java ├── ProgrammingProject5.3 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── Programming Project 5.3.iml │ ├── out │ │ └── production │ │ │ └── Programming Project 5.3 │ │ │ ├── CircleListApp.class │ │ │ ├── CircularList.class │ │ │ └── Link.class │ └── src │ │ └── CircleListApp.java ├── ProgrammingProject5.4 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── ProgrammingProject54.iml │ ├── out │ │ └── production │ │ │ └── ProgrammingProject54 │ │ │ ├── CircularList.class │ │ │ ├── CircularListStackApp.class │ │ │ ├── Link.class │ │ │ └── Stack.class │ └── src │ │ └── CircularListStackApp.java ├── ProgrammingProject5.5 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── ProgrammingProject5.5.iml │ ├── out │ │ └── production │ │ │ └── ProgrammingProject5.5 │ │ │ ├── CircularList.class │ │ │ ├── JosephusProblemApp.class │ │ │ └── Link.class │ └── src │ │ └── JosephusProblemApp.java └── ProgrammingProject5.6 │ ├── .idea │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml │ ├── ProgrammingProject5.6.iml │ ├── out │ └── production │ │ └── ProgrammingProject5.6 │ │ ├── Link.class │ │ ├── LinkList.class │ │ └── LinkedList2DApp.class │ └── src │ └── LinkedList2DApp.java ├── Chapter 6 - Recursion ├── Programming Project 6.1 │ ├── .idea │ │ ├── .name │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── 6.1.iml │ ├── out │ │ └── production │ │ │ └── 6.1 │ │ │ └── MultiplicatonApp.class │ └── src │ │ └── MultiplicatonApp.java ├── Programming Project 6.2 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── 6.2.iml │ ├── out │ │ └── production │ │ │ └── 6.2 │ │ │ └── TreeApp.class │ └── src │ │ └── TreeApp.java ├── Programming Project 6.3 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── 6.3.iml │ ├── out │ │ └── production │ │ │ └── 6.3 │ │ │ └── PowerApp.class │ └── src │ │ └── PowerApp.java ├── Programming Project 6.4 │ ├── .idea │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── 6.4.iml │ ├── out │ │ └── production │ │ │ └── 6.4 │ │ │ └── KnapsackApp.class │ └── src │ │ └── KnapsackApp.java └── Programming Project 6.5 │ ├── .idea │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml │ ├── ProgrammingProject6.5.iml │ ├── out │ └── production │ │ └── ProgrammingProject6.5 │ │ └── TeamApp.class │ └── src │ └── TeamApp.java ├── Chapter 7 - Advanced Sorting ├── Programming Project 7.1 │ ├── .idea │ │ ├── .name │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── Programming Project 7.1.iml │ ├── out │ │ └── production │ │ │ └── Programming Project 7.1 │ │ │ ├── ArrayPar.class │ │ │ └── PartitionApp.class │ └── src │ │ └── PartitionApp.java ├── Programming Project 7.2 │ ├── .idea │ │ ├── .name │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── Programming Project 7.2.iml │ ├── out │ │ └── production │ │ │ └── Programming Project 7.2 │ │ │ ├── ArrayIns.class │ │ │ └── QuickSort2App.class │ └── src │ │ └── QuickSort2App.java ├── Programming Project 7.3 │ ├── .idea │ │ ├── .name │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── Project 7.3.iml │ ├── out │ │ └── production │ │ │ └── Project 7.3 │ │ │ ├── ArrayPar.class │ │ │ └── MedianApp.class │ └── src │ │ └── MedianApp.java ├── Programming Project 7.4 │ ├── .idea │ │ ├── .name │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── workspace.xml │ ├── Project 7.4.iml │ ├── out │ │ └── production │ │ │ └── Project 7.4 │ │ │ ├── ArrayPar.class │ │ │ └── findNumAtKApp.class │ └── src │ │ └── findNumAtKApp.java └── Programming Project 7.5 │ ├── .idea │ ├── .name │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml │ ├── Project 7.5.iml │ ├── out │ └── production │ │ └── Project 7.5 │ │ ├── Array.class │ │ └── radixSortApp.class │ └── src │ └── radixSortApp.java └── README.md /Chapter 2 - Arrays/Programming Project 2.6/bin/HighArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Project 2.6/bin/HighArray.class -------------------------------------------------------------------------------- /Chapter 2 - Arrays/Programming Project 2.6/bin/HighArrayApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Project 2.6/bin/HighArrayApp.class -------------------------------------------------------------------------------- /Chapter 2 - Arrays/Programming Project 2.6/src/HighArrayApp.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Project 2.6/src/HighArrayApp.java -------------------------------------------------------------------------------- /Chapter 2 - Arrays/Programming Projects 2.1-2.3/bin/HighArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Projects 2.1-2.3/bin/HighArray.class -------------------------------------------------------------------------------- /Chapter 2 - Arrays/Programming Projects 2.1-2.3/bin/HighArrayApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Projects 2.1-2.3/bin/HighArrayApp.class -------------------------------------------------------------------------------- /Chapter 2 - Arrays/Programming Projects 2.1-2.3/src/HighArrayApp.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Projects 2.1-2.3/src/HighArrayApp.java -------------------------------------------------------------------------------- /Chapter 2 - Arrays/Programming Projects 2.4 - 2.5/bin/OrdArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Projects 2.4 - 2.5/bin/OrdArray.class -------------------------------------------------------------------------------- /Chapter 2 - Arrays/Programming Projects 2.4 - 2.5/bin/OrderedApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Projects 2.4 - 2.5/bin/OrderedApp.class -------------------------------------------------------------------------------- /Chapter 2 - Arrays/Programming Projects 2.4 - 2.5/src/OrderedApp.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 2 - Arrays/Programming Projects 2.4 - 2.5/src/OrderedApp.java -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.1/bin/ArrayBub.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Progamming Project 3.1/bin/ArrayBub.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.1/bin/BubbleSortApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Progamming Project 3.1/bin/BubbleSortApp.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.1/src/BubbleSortApp.java: -------------------------------------------------------------------------------- 1 | // modified by Manveer Tamber 2 | // demonstrates bubble sort 3 | //////////////////////////////////////////////////////////////// 4 | class ArrayBub { 5 | private long[] a; // ref to array a 6 | private int nElems; // number of data items 7 | // -------------------------------------------------------------- 8 | 9 | public ArrayBub(int max) // constructor 10 | { 11 | a = new long[max]; // create the array 12 | nElems = 0; // no items yet 13 | } 14 | 15 | // -------------------------------------------------------------- 16 | public void insert(long value) // put element into array 17 | { 18 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | 22 | // -------------------------------------------------------------- 23 | public void display() // displays array contents 24 | { 25 | for (int j = 0; j < nElems; j++) // for each element, 26 | System.out.print(a[j] + " "); // display it 27 | System.out.println(""); 28 | } 29 | 30 | // -------------------------------------------------------------- 31 | public void bubbleSort() { 32 | int out, in; 33 | 34 | for (out = nElems - 1; out > 1; out--) // outer loop (backward) 35 | for (in = 0; in < out; in++) // inner loop (forward) 36 | if (a[in] > a[in + 1]) // out of order? 37 | swap(in, in + 1); // swap them 38 | } // end bubbleSort() 39 | 40 | // modified method for challenge 3.1 by Manveer Tamber 41 | public void biBubbleSort() // bidirectional bubble sort 42 | { 43 | int out = nElems - 1; 44 | int start = 1; 45 | boolean changed = true; 46 | while (changed && out > start) { 47 | changed = false; 48 | for (int in = 0; in < out; in++) // inner loop (forward) 49 | if (a[in] > a[in + 1]) { // out of order? 50 | swap(in, in + 1);// swap them 51 | changed = true; 52 | // System.out.println("Swapped " + a[in] + " with " + 53 | // a[in+1]); 54 | } 55 | out--; 56 | for (int count = out; count > start; count--) { 57 | if (a[count] < a[count - 1]) { 58 | swap(count, count - 1); 59 | changed = true; 60 | // System.out.println("Swapped " + a[count] + " with " + 61 | // a[count-1]); 62 | } 63 | } 64 | start++; 65 | 66 | } 67 | } // end biBubbleSort() 68 | // end 3.1 Challenge 69 | // -------------------------------------------------------------- 70 | private void swap(int one, int two) { 71 | long temp = a[one]; 72 | a[one] = a[two]; 73 | a[two] = temp; 74 | } 75 | // -------------------------------------------------------------- 76 | } // end class ArrayBub 77 | //////////////////////////////////////////////////////////////// 78 | 79 | class BubbleSortApp { 80 | public static void main(String[] args) { 81 | int maxSize = 100; // array size 82 | ArrayBub arr; // reference to array 83 | arr = new ArrayBub(maxSize); // create the array 84 | 85 | arr.insert(77); 86 | arr.insert(99); 87 | arr.insert(44); 88 | arr.insert(55); 89 | arr.insert(22); 90 | arr.insert(88); 91 | arr.insert(11); 92 | arr.insert(00); 93 | arr.insert(66); 94 | arr.insert(33); 95 | 96 | arr.display(); // display items 97 | 98 | arr.biBubbleSort(); // bubble sort them using bidirectional bubble sort 99 | 100 | arr.display(); // display them again 101 | } // end main() 102 | } // end class BubbleSortApp 103 | //////////////////////////////////////////////////////////////// 104 | -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.2/bin/ArrayIns.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Progamming Project 3.2/bin/ArrayIns.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.2/bin/InsertSortApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Progamming Project 3.2/bin/InsertSortApp.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.2/src/InsertSortApp.java: -------------------------------------------------------------------------------- 1 | // modified by Manveer Tamber 2 | // demonstrates insertion sort 3 | //-------------------------------------------------------------- 4 | class ArrayIns { 5 | private long[] a; // ref to array a 6 | private int nElems; // number of data items 7 | // -------------------------------------------------------------- 8 | 9 | public ArrayIns(int max) // constructor 10 | { 11 | a = new long[max]; // create the array 12 | nElems = 0; // no items yet 13 | } 14 | 15 | // -------------------------------------------------------------- 16 | public void insert(long value) // put element into array 17 | { 18 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | 22 | // -------------------------------------------------------------- 23 | public void display() // displays array contents 24 | { 25 | for (int j = 0; j < nElems; j++) // for each element, 26 | System.out.print(a[j] + " "); // display it 27 | System.out.println(""); 28 | } 29 | 30 | // -------------------------------------------------------------- 31 | public void insertionSort() { 32 | int in, out; 33 | 34 | for (out = 1; out < nElems; out++) // out is dividing line 35 | { 36 | long temp = a[out]; // remove marked item 37 | in = out; // start shifts at out 38 | while (in > 0 && a[in - 1] >= temp) // until one is smaller, 39 | { 40 | a[in] = a[in - 1]; // shift item to right 41 | --in; // go left one position 42 | } 43 | a[in] = temp; // insert marked item 44 | } // end for 45 | } // end insertionSort() 46 | 47 | // -------------------------------------------------------------- 48 | // Added for 3.2 Solution 49 | public double median() { 50 | if (nElems % 2 == 0) { 51 | return (a[nElems / 2] + a[nElems / 2 - 1]) / 2.0; 52 | } 53 | return a[nElems / 2]; 54 | } 55 | // end 3.2 Solution 56 | 57 | } // end class ArrayIns 58 | //////////////////////////////////////////////////////////////// 59 | 60 | class InsertSortApp { 61 | public static void main(String[] args) { 62 | int maxSize = 100; // array size 63 | ArrayIns arr; // reference to array 64 | arr = new ArrayIns(maxSize); // create the array 65 | 66 | arr.insert(77); 67 | arr.insert(99); 68 | arr.insert(44); 69 | arr.insert(55); 70 | arr.insert(22); 71 | arr.insert(88); 72 | arr.insert(44); 73 | arr.insert(55); 74 | arr.insert(22); 75 | arr.insert(88); 76 | arr.insert(11); 77 | arr.insert(11); 78 | arr.insert(00); 79 | arr.insert(66); 80 | arr.insert(33); 81 | 82 | arr.display(); // display items 83 | 84 | arr.insertionSort(); 85 | 86 | arr.display(); // display them again 87 | 88 | System.out.println("The Median is " + arr.median()); // output median 89 | 90 | } // end main() 91 | } // end class InsertSortApp 92 | -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.3/bin/ArrayIns.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Progamming Project 3.3/bin/ArrayIns.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.3/bin/InsertSortApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Progamming Project 3.3/bin/InsertSortApp.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.3/src/InsertSortApp.java: -------------------------------------------------------------------------------- 1 | // modified by Manveer Tamber 2 | // demonstrates insertion sort 3 | //-------------------------------------------------------------- 4 | class ArrayIns { 5 | private long[] a; // ref to array a 6 | private int nElems; // number of data items 7 | // -------------------------------------------------------------- 8 | 9 | public ArrayIns(int max) // constructor 10 | { 11 | a = new long[max]; // create the array 12 | nElems = 0; // no items yet 13 | } 14 | 15 | // -------------------------------------------------------------- 16 | public void insert(long value) // put element into array 17 | { 18 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | 22 | // -------------------------------------------------------------- 23 | public void display() // displays array contents 24 | { 25 | for (int j = 0; j < nElems; j++) // for each element, 26 | System.out.print(a[j] + " "); // display it 27 | System.out.println(""); 28 | } 29 | 30 | // -------------------------------------------------------------- 31 | public void insertionSort() { 32 | int in, out; 33 | 34 | for (out = 1; out < nElems; out++) // out is dividing line 35 | { 36 | long temp = a[out]; // remove marked item 37 | in = out; // start shifts at out 38 | while (in > 0 && a[in - 1] >= temp) // until one is smaller, 39 | { 40 | a[in] = a[in - 1]; // shift item to right 41 | --in; // go left one position 42 | } 43 | a[in] = temp; // insert marked item 44 | } // end for 45 | } // end insertionSort() 46 | // -------------------------------------------------------------- 47 | // Added for 3.3 Solution 48 | public void noDups() { // works only for sorted arrays 49 | long current = a[0]; 50 | for (int count = 1; count < nElems; count++) { // set every duplicate number after original to -1 51 | if (a[count] == current) { 52 | a[count] = -1; 53 | } else { 54 | current = a[count]; 55 | } 56 | } 57 | 58 | int toInsert = 0; 59 | for (int count = 0; count < nElems; count++) { // pushes all -1 values to end of array, every value only gets moved once 60 | if (a[count] == -1) { 61 | toInsert = count; 62 | for (int next = count + 1; next < nElems; next++) { 63 | if (a[next] != -1) { 64 | a[toInsert] = a[next]; 65 | a[next] = -1; 66 | // System.out.println("Moved " + a[toInsert]); 67 | break; 68 | } 69 | } 70 | } 71 | } 72 | 73 | for (int count = nElems - 1; count > 0; count--) { // gets rid of -1 values at end of array 74 | if (a[count] == -1) 75 | nElems--; 76 | } 77 | 78 | } 79 | // end 3.3 Solution 80 | 81 | } // end class ArrayIns 82 | //////////////////////////////////////////////////////////////// 83 | 84 | class InsertSortApp { 85 | public static void main(String[] args) { 86 | int maxSize = 100; // array size 87 | ArrayIns arr; // reference to array 88 | arr = new ArrayIns(maxSize); // create the array 89 | 90 | arr.insert(77); 91 | arr.insert(99); 92 | arr.insert(44); 93 | arr.insert(55); 94 | arr.insert(22); 95 | arr.insert(88); 96 | arr.insert(44); 97 | arr.insert(55); 98 | arr.insert(22); 99 | arr.insert(88); 100 | arr.insert(11); 101 | arr.insert(11); 102 | arr.insert(00); 103 | arr.insert(66); 104 | arr.insert(33); 105 | 106 | arr.display(); // display items 107 | 108 | arr.insertionSort(); 109 | 110 | arr.display(); // display them again 111 | 112 | System.out.print("Removed Duplicates: "); 113 | arr.noDups(); // for solution to 3.3 114 | arr.display(); 115 | 116 | } // end main() 117 | } // end class InsertSortApp 118 | -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.4/bin/ArrayBub.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Progamming Project 3.4/bin/ArrayBub.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.4/bin/BubbleSortApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Progamming Project 3.4/bin/BubbleSortApp.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Progamming Project 3.4/src/BubbleSortApp.java: -------------------------------------------------------------------------------- 1 | // modified by Manveer Tamber 2 | // demonstrates bubble sort 3 | //////////////////////////////////////////////////////////////// 4 | class ArrayBub { 5 | private long[] a; // ref to array a 6 | private int nElems; // number of data items 7 | // -------------------------------------------------------------- 8 | 9 | public ArrayBub(int max) // constructor 10 | { 11 | a = new long[max]; // create the array 12 | nElems = 0; // no items yet 13 | } 14 | 15 | // -------------------------------------------------------------- 16 | public void insert(long value) // put element into array 17 | { 18 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | 22 | // -------------------------------------------------------------- 23 | public void display() // displays array contents 24 | { 25 | for (int j = 0; j < nElems; j++) // for each element, 26 | System.out.print(a[j] + " "); // display it 27 | System.out.println(""); 28 | } 29 | 30 | // -------------------------------------------------------------- 31 | public void bubbleSort() { 32 | int out, in; 33 | 34 | for (out = nElems - 1; out > 1; out--) // outer loop (backward) 35 | for (in = 0; in < out; in++) // inner loop (forward) 36 | if (a[in] > a[in + 1]) // out of order? 37 | swap(in, in + 1); // swap them 38 | } // end bubbleSort() 39 | 40 | // Added for 3.4 Challenge 41 | public void oddEvenSort() { 42 | boolean changed = true; 43 | 44 | while (changed) { 45 | changed = false; 46 | // even pass 47 | for (int count = 0; count < nElems-1; count += 2) { 48 | 49 | if (a[count] > a[count + 1]) { 50 | changed = true; 51 | swap(count, count + 1); 52 | } 53 | } 54 | // odd pass 55 | for (int count = 1; count < nElems-1; count += 2) { 56 | 57 | if (a[count] > a[count + 1]) { 58 | changed = true; 59 | swap(count, count + 1); 60 | } 61 | } 62 | } 63 | 64 | } 65 | 66 | // End 3.4 Challenge 67 | // -------------------------------------------------------------- 68 | private void swap(int one, int two) { 69 | long temp = a[one]; 70 | a[one] = a[two]; 71 | a[two] = temp; 72 | } 73 | // -------------------------------------------------------------- 74 | } // end class ArrayBub 75 | //////////////////////////////////////////////////////////////// 76 | 77 | class BubbleSortApp { 78 | public static void main(String[] args) { 79 | int maxSize = 100; // array size 80 | ArrayBub arr; // reference to array 81 | arr = new ArrayBub(maxSize); // create the array 82 | 83 | arr.insert(77); 84 | arr.insert(99); 85 | arr.insert(44); 86 | arr.insert(55); 87 | arr.insert(22); 88 | arr.insert(88); 89 | arr.insert(11); 90 | arr.insert(00); 91 | arr.insert(66); 92 | arr.insert(33); 93 | 94 | arr.display(); // display items 95 | 96 | arr.oddEvenSort(); 97 | 98 | arr.display(); // display them again 99 | } // end main() 100 | } // end class BubbleSortApp 101 | //////////////////////////////////////////////////////////////// 102 | -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Programming Project 3.5/bin/ArrayIns.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Programming Project 3.5/bin/ArrayIns.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Programming Project 3.5/bin/InsertSortApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Programming Project 3.5/bin/InsertSortApp.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Programming Project 3.5/src/InsertSortApp.java: -------------------------------------------------------------------------------- 1 | // modified by Manveer Tamber 2 | // demonstrates insertion sort 3 | //-------------------------------------------------------------- 4 | class ArrayIns { 5 | private long[] a; // ref to array a 6 | private int nElems; // number of data items 7 | // -------------------------------------------------------------- 8 | 9 | public ArrayIns(int max) // constructor 10 | { 11 | a = new long[max]; // create the array 12 | nElems = 0; // no items yet 13 | } 14 | 15 | // -------------------------------------------------------------- 16 | public void insert(long value) // put element into array 17 | { 18 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | 22 | // -------------------------------------------------------------- 23 | public void display() // displays array contents 24 | { 25 | for (int j = 0; j < nElems; j++) // for each element, 26 | System.out.print(a[j] + " "); // display it 27 | System.out.println(""); 28 | } 29 | 30 | // -------------------------------------------------------------- 31 | public void insertionSort() { 32 | int in, out; 33 | 34 | for (out = 1; out < nElems; out++) // out is dividing line 35 | { 36 | long temp = a[out]; // remove marked item 37 | in = out; // start shifts at out 38 | while (in > 0 && a[in - 1] >= temp) // until one is smaller, 39 | { 40 | a[in] = a[in - 1]; // shift item to right 41 | --in; // go left one position 42 | } 43 | a[in] = temp; // insert marked item 44 | } // end for 45 | } // end insertionSort() 46 | // Added for challenge 3.5 47 | 48 | public void countinsertionSort() { 49 | int in, out; 50 | int comparisons = 0; 51 | int copies = 0; 52 | for (out = 1; out < nElems; out++) // out is dividing line 53 | { 54 | long temp = a[out]; // remove marked item 55 | 56 | in = out; // start shifts at out 57 | 58 | while (in > 0) { 59 | comparisons++; 60 | if (a[in - 1] >= temp) { 61 | a[in] = a[in - 1]; // shift item to right 62 | copies++; 63 | in--; 64 | } else { 65 | break; 66 | } 67 | } 68 | 69 | a[in] = temp; // insert marked item 70 | 71 | } // end for 72 | System.out.println("The number of comparisons are " + comparisons); 73 | System.out.println("The number of copies are " + copies); 74 | } 75 | 76 | // End 3.5 Challenge 77 | // -------------------------------------------------------------- 78 | } // end class ArrayIns 79 | //////////////////////////////////////////////////////////////// 80 | 81 | class InsertSortApp { 82 | public static void main(String[] args) { 83 | int maxSize = 100; // array size 84 | ArrayIns arr; // reference to array 85 | arr = new ArrayIns(maxSize); // create the array 86 | 87 | arr.insert(77); // insert 10 items 88 | arr.insert(99); 89 | arr.insert(44); 90 | arr.insert(55); 91 | arr.insert(22); 92 | arr.insert(88); 93 | arr.insert(44); 94 | arr.insert(55); 95 | arr.insert(22); 96 | arr.insert(88); 97 | arr.insert(11); 98 | arr.insert(11); 99 | arr.insert(00); 100 | arr.insert(66); 101 | arr.insert(33); 102 | 103 | arr.display(); // display items 104 | 105 | arr.countinsertionSort(); // insertion-sort, while also counting comparisons and copies for solution to 3.5 106 | 107 | arr.display(); // display them again 108 | 109 | 110 | System.out.println(); 111 | System.out.println("Now, sort almost sorted data"); 112 | ArrayIns arr2 = new ArrayIns(maxSize); // create array for almost sorted data 113 | arr2.insert(1); 114 | arr2.insert(2); 115 | arr2.insert(5); 116 | arr2.insert(3); 117 | arr2.insert(5); 118 | arr2.insert(6); 119 | arr2.insert(8); 120 | arr2.insert(10); 121 | arr2.insert(2); 122 | arr2.insert(5); 123 | arr2.insert(12); 124 | arr2.insert(13); 125 | arr2.insert(10); 126 | arr2.insert(18); 127 | arr2.insert(20); 128 | arr2.insert(13); 129 | arr2.insert(12); 130 | arr2.insert(25); 131 | 132 | arr2.display(); 133 | arr2.countinsertionSort(); 134 | arr2.display(); 135 | 136 | 137 | 138 | } // end main() 139 | } // end class InsertSortApp 140 | -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Programming Project 3.6/bin/ArrayIns.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Programming Project 3.6/bin/ArrayIns.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Programming Project 3.6/bin/InsertSortApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 3 - Simple Sorting/Programming Project 3.6/bin/InsertSortApp.class -------------------------------------------------------------------------------- /Chapter 3 - Simple Sorting/Programming Project 3.6/src/InsertSortApp.java: -------------------------------------------------------------------------------- 1 | // modified by Manveer Tamber 2 | // demonstrates insertion sort 3 | //-------------------------------------------------------------- 4 | class ArrayIns { 5 | private long[] a; // ref to array a 6 | private int nElems; // number of data items 7 | // -------------------------------------------------------------- 8 | 9 | public ArrayIns(int max) // constructor 10 | { 11 | a = new long[max]; // create the array 12 | nElems = 0; // no items yet 13 | } 14 | 15 | // -------------------------------------------------------------- 16 | public void insert(long value) // put element into array 17 | { 18 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | 22 | // -------------------------------------------------------------- 23 | public void display() // displays array contents 24 | { 25 | for (int j = 0; j < nElems; j++) // for each element, 26 | System.out.print(a[j] + " "); // display it 27 | System.out.println(""); 28 | } 29 | 30 | // -------------------------------------------------------------- 31 | public void insertionSort() { 32 | int in, out; 33 | 34 | for (out = 1; out < nElems; out++) // out is dividing line 35 | { 36 | long temp = a[out]; // remove marked item 37 | in = out; // start shifts at out 38 | while (in > 0 && a[in - 1] >= temp) // until one is smaller, 39 | { 40 | a[in] = a[in - 1]; // shift item to right 41 | --in; // go left one position 42 | } 43 | a[in] = temp; // insert marked item 44 | } // end for 45 | } // end insertionSort() 46 | // Added for challenge 3.6 47 | public void noDupsInsertionSort() { 48 | int in, out; 49 | 50 | for (out = 1; out < nElems; out++) // out is dividing line 51 | { 52 | long temp = a[out]; // remove marked item 53 | in = out; // start shifts at out 54 | while (in > 0 && a[in - 1] >= temp) // until one is smaller, 55 | { 56 | if(temp == a[in -1]){ 57 | temp = -1; 58 | } 59 | a[in] = a[in - 1]; // shift item to right 60 | --in; // go left one position 61 | } 62 | a[in] = temp; // insert marked item 63 | } // end for 64 | 65 | // now remove the zeroes 66 | int startpos = 0; 67 | for(int count = 0;count 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.1/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Challenge 4.1 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.1/bin/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.1/bin/Queue.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.1/bin/QueueApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.1/bin/QueueApp.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.1/src/QueueApp.java: -------------------------------------------------------------------------------- 1 | // Modified for Challenge 4.1 by Manveer Tamber 2 | class Queue 3 | { 4 | private int maxSize; 5 | private long[] queArray; 6 | private int front; 7 | private int rear; 8 | private int nItems; 9 | //-------------------------------------------------------------- 10 | public Queue(int s) // constructor 11 | { 12 | maxSize = s; 13 | queArray = new long[maxSize]; 14 | front = 0; 15 | rear = -1; 16 | nItems = 0; 17 | } 18 | //-------------------------------------------------------------- 19 | public void insert(long j) // put item at rear of queue 20 | { 21 | if(rear == maxSize-1) // deal with wraparound 22 | rear = -1; 23 | queArray[++rear] = j; // increment rear and insert 24 | nItems++; // one more item 25 | } 26 | //-------------------------------------------------------------- 27 | public long remove() // take item from front of queue 28 | { 29 | long temp = queArray[front++]; // get value and incr front 30 | if(front == maxSize) // deal with wraparound 31 | front = 0; 32 | nItems--; // one less item 33 | return temp; 34 | } 35 | //-------------------------------------------------------------- 36 | public long peekFront() // peek at front of queue 37 | { 38 | return queArray[front]; 39 | } 40 | //-------------------------------------------------------------- 41 | public boolean isEmpty() // true if queue is empty 42 | { 43 | return (nItems==0); 44 | } 45 | //-------------------------------------------------------------- 46 | public boolean isFull() // true if queue is full 47 | { 48 | return (nItems==maxSize); 49 | } 50 | //-------------------------------------------------------------- 51 | public int size() // number of items in queue 52 | { 53 | return nItems; 54 | } 55 | //-------------------------------------------------------------- 56 | //Added For Challenge 4.1 57 | public void display(){ 58 | int pos = front; 59 | for(int count = 0;count 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/.idea/.name: -------------------------------------------------------------------------------- 1 | Challenge 4.2 -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 35 | 36 | 37 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 67 | 68 | 71 | 72 | 73 | 74 | 77 | 78 | 81 | 82 | 85 | 86 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 109 | 110 | 111 | 112 | 113 | 118 | 119 | 120 | 146 | 147 | 148 | 173 | 174 | 181 | 182 | 183 | 196 | 197 | 198 | 205 | 208 | 210 | 211 | 212 | 213 | 214 | 215 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 254 | 255 | 256 | 267 | 268 | 269 | 279 | 280 | 287 | 288 | 289 | 290 | 308 | 315 | 316 | 317 | 319 | 320 | 321 | 322 | 1495304047183 323 | 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 | 374 | 375 | 376 | 377 | 378 | 379 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Challenge 4.2 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/Challenge 4.2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/bin/Deque.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.2/bin/Deque.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/bin/DequeApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.2/bin/DequeApp.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.2/src/DequeApp.java: -------------------------------------------------------------------------------- 1 | //Written for challenge 4.2 by Manveer Tamber 2 | class Deque { 3 | int maxSize; 4 | int arr[]; 5 | int elem; 6 | int left; 7 | int right; 8 | 9 | public Deque(int m) { 10 | maxSize = m; 11 | arr = new int[maxSize]; 12 | left = 1; 13 | right = 0; 14 | elem = 0; 15 | } 16 | 17 | public boolean insertLeft(int n) { 18 | if (!isFull()) { 19 | arr[left] = n; 20 | left++; 21 | if (left == maxSize) { 22 | left = 0; 23 | } 24 | elem++; 25 | return true; 26 | } 27 | return false; 28 | } 29 | 30 | public boolean insertRight(int n) { 31 | if (!isFull()) { 32 | arr[right] = n; 33 | right--; 34 | elem++; 35 | if (right == -1) { 36 | right = maxSize - 1; 37 | } 38 | return true; 39 | } 40 | return false; 41 | } 42 | 43 | public int removeLeft() { 44 | if (!isEmpty()) { 45 | left--; 46 | elem--; 47 | if (left == -1) { 48 | left = maxSize - 1; 49 | } 50 | return arr[left]; 51 | } 52 | return -1; 53 | } 54 | 55 | public int removeRight() { 56 | if (!isEmpty()) { 57 | right++; 58 | elem--; 59 | if (right == maxSize) { 60 | right = 0; 61 | } 62 | return arr[right]; 63 | } 64 | return -1; 65 | } 66 | 67 | public int peekLeft() { 68 | if (!isEmpty()) { 69 | return arr[left - 1]; 70 | } 71 | return -1; 72 | } 73 | 74 | public int peekRight() { 75 | if (!isEmpty()) { 76 | return arr[right + 1]; 77 | } 78 | return -1; 79 | } 80 | 81 | public boolean isFull() { 82 | return elem == maxSize; 83 | } 84 | 85 | public boolean isEmpty() { 86 | return elem == 0; 87 | } 88 | } 89 | 90 | public class DequeApp { 91 | 92 | public static void main(String[] args) { 93 | Deque thing = new Deque(100); 94 | for (int count = 0; count < 100; count++) { 95 | thing.insertLeft(count); 96 | } 97 | for (int count = 0; count < 10; count++) { 98 | thing.removeRight(); 99 | } 100 | for (int count = 0; count < 10; count++) { 101 | thing.insertLeft(count); 102 | } 103 | while (!thing.isEmpty()) { 104 | System.out.print(thing.removeRight() + " "); 105 | } 106 | 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.3/.idea/misc.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.3/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.3/Programming Project 4.3.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.3/out/production/Programming Project 4.3/Deque.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.3/out/production/Programming Project 4.3/Deque.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.3/out/production/Programming Project 4.3/DequeStackApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.3/out/production/Programming Project 4.3/DequeStackApp.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.3/out/production/Programming Project 4.3/Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.3/out/production/Programming Project 4.3/Stack.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.3/src/DequeStackApp.java: -------------------------------------------------------------------------------- 1 | //Written by Manveer Tamber for Programming Project 4.3 2 | 3 | class Stack{ 4 | 5 | Deque StackDeque; 6 | 7 | public Stack(int maxsize){ 8 | StackDeque = new Deque(maxsize); 9 | } 10 | 11 | public boolean push(int n){ 12 | return StackDeque.insertLeft(n); 13 | } 14 | public int pop(){ 15 | return StackDeque.removeLeft(); 16 | } 17 | public int peek(){ 18 | return StackDeque.peekLeft(); 19 | } 20 | public boolean isEmpty(){ 21 | return StackDeque.isEmpty(); 22 | } 23 | 24 | public boolean isFull(){ 25 | return StackDeque.isFull(); 26 | } 27 | } 28 | 29 | class Deque { 30 | int maxSize; 31 | int arr[]; 32 | int elem; 33 | int left; 34 | int right; 35 | 36 | public Deque(int m) { 37 | maxSize = m; 38 | arr = new int[maxSize]; 39 | left = 1; 40 | right = 0; 41 | elem = 0; 42 | } 43 | 44 | public boolean insertLeft(int n) { 45 | if (!isFull()) { 46 | arr[left] = n; 47 | left++; 48 | if (left == maxSize) { 49 | left = 0; 50 | } 51 | elem++; 52 | return true; 53 | } 54 | return false; 55 | } 56 | 57 | public boolean insertRight(int n) { 58 | if (!isFull()) { 59 | arr[right] = n; 60 | right--; 61 | elem++; 62 | if (right == -1) { 63 | right = maxSize - 1; 64 | } 65 | return true; 66 | } 67 | return false; 68 | } 69 | 70 | public int removeLeft() { 71 | if (!isEmpty()) { 72 | left--; 73 | elem--; 74 | if (left == -1) { 75 | left = maxSize - 1; 76 | } 77 | return arr[left]; 78 | } 79 | return -1; 80 | } 81 | 82 | public int removeRight() { 83 | if (!isEmpty()) { 84 | right++; 85 | elem--; 86 | if (right == maxSize) { 87 | right = 0; 88 | } 89 | return arr[right]; 90 | } 91 | return -1; 92 | } 93 | 94 | public int peekLeft() { 95 | if (!isEmpty()) { 96 | return arr[left - 1]; 97 | } 98 | return -1; 99 | } 100 | 101 | public int peekRight() { 102 | if (!isEmpty()) { 103 | return arr[right + 1]; 104 | } 105 | return -1; 106 | } 107 | 108 | public boolean isFull() { 109 | return elem == maxSize; 110 | } 111 | 112 | public boolean isEmpty() { 113 | return elem == 0; 114 | } 115 | } 116 | 117 | 118 | public class DequeStackApp { 119 | public static void main(String args[]){ 120 | Stack stack = new Stack(100); 121 | 122 | for(int i = 1;i<=100;i++){ 123 | stack.push(i); 124 | } 125 | for(int j = 0;j<100;j++){ 126 | System.out.println(stack.pop()); 127 | } 128 | 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.4/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.4/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.4/Programming Project 4.4.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.4/out/production/Programming Project 4.4/PriorityQ.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.4/out/production/Programming Project 4.4/PriorityQ.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.4/out/production/Programming Project 4.4/PriorityQApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.4/out/production/Programming Project 4.4/PriorityQApp.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.4/src/PriorityQApp.java: -------------------------------------------------------------------------------- 1 | //Modified for Programming Challenge 4.4 by Manveer Tamber 2 | class PriorityQ 3 | { 4 | // array in sorted order, from max at 0 to min at size-1 5 | private int maxSize; 6 | private long[] queArray; 7 | private int nItems; 8 | //------------------------------------------------------------- 9 | public PriorityQ(int s) // constructor 10 | { 11 | maxSize = s; 12 | queArray = new long[maxSize]; 13 | nItems = 0; 14 | } 15 | //------------------------------------------------------------- 16 | public void insert(long item) // insert item 17 | { 18 | if(!isFull()){ 19 | queArray[nItems] = item; 20 | nItems++; 21 | } 22 | } // end insert() 23 | //------------------------------------------------------------- 24 | public long remove() // remove minimum item 25 | { 26 | reverseInsertionSort(); 27 | return queArray[--nItems]; 28 | } 29 | //------------------------------------------------------------- 30 | public long peekMin() // peek at minimum item 31 | { return queArray[nItems-1]; } 32 | //------------------------------------------------------------- 33 | public boolean isEmpty() // true if queue is empty 34 | { return (nItems==0); } 35 | //------------------------------------------------------------- 36 | public boolean isFull() // true if queue is full 37 | { return (nItems == maxSize); } 38 | //------------------------------------------------------------- 39 | 40 | 41 | public void reverseInsertionSort(){ 42 | for(int count = 1;count 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.5/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.5/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.5/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Programming Project 4.5 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.5/Programming Project 4.5.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.5/out/production/Programming Project 4.5/CheckoutLineApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.5/out/production/Programming Project 4.5/CheckoutLineApp.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.5/out/production/Programming Project 4.5/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.5/out/production/Programming Project 4.5/Queue.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.5/out/production/Programming Project 4.5/customer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 4 - Stacks and Queues/Programming Project 4.5/out/production/Programming Project 4.5/customer.class -------------------------------------------------------------------------------- /Chapter 4 - Stacks and Queues/Programming Project 4.5/src/CheckoutLineApp.java: -------------------------------------------------------------------------------- 1 | //Programming Challenge 4.5 by Manveer Tamber 2 | 3 | import java.util.Random; 4 | import java.util.Scanner; 5 | 6 | class customer{ 7 | public int bags; 8 | int id; 9 | 10 | public customer(int bags, int id) { 11 | this.bags = bags; 12 | this.id = id; 13 | } 14 | 15 | public String toString(){ 16 | return "Customer num: " + id + ", " + bags + " bags left to checkout"; 17 | } 18 | } 19 | 20 | class Queue 21 | { 22 | private int maxSize; 23 | private customer[] queArray; 24 | private int front; 25 | private int rear; 26 | private int nItems; 27 | 28 | public int bags = 0; 29 | public int linenumber; 30 | //-------------------------------------------------------------- 31 | public Queue(int s, int n) // constructor 32 | { 33 | maxSize = s; 34 | queArray = new customer[maxSize]; 35 | front = 0; 36 | rear = -1; 37 | nItems = 0; 38 | linenumber = n; 39 | } 40 | //-------------------------------------------------------------- 41 | public void insert(customer j) // put item at rear of queue 42 | { 43 | if(rear == maxSize-1) // deal with wraparound 44 | rear = -1; 45 | queArray[++rear] = j; // increment rear and insert 46 | nItems++; // one more item 47 | } 48 | //-------------------------------------------------------------- 49 | public customer remove() // take item from front of queue 50 | { 51 | customer temp = queArray[front++]; // get value and incr front 52 | if(front == maxSize) // deal with wraparound 53 | front = 0; 54 | nItems--; // one less item 55 | return temp; 56 | } 57 | //-------------------------------------------------------------- 58 | public customer peekFront() // peek at front of queue 59 | { 60 | return queArray[front]; 61 | } 62 | //-------------------------------------------------------------- 63 | public boolean isEmpty() // true if queue is empty 64 | { 65 | return (nItems==0); 66 | } 67 | //-------------------------------------------------------------- 68 | public boolean isFull() // true if queue is full 69 | { 70 | return (nItems==maxSize); 71 | } 72 | //-------------------------------------------------------------- 73 | public int size() // number of items in queue 74 | { 75 | return nItems; 76 | } 77 | //-------------------------------------------------------------- 78 | public void display(){ 79 | System.out.print("Line " + linenumber + " "); 80 | if(!isEmpty()){ 81 | int pos = front; 82 | for(int count = 0;count 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 1.8 11 | 12 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.1/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.1/ProgrammingProject5.1.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.1/out/production/ProgrammingProject5.1/Link.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.1/out/production/ProgrammingProject5.1/Link.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.1/out/production/ProgrammingProject5.1/LinkedListPriorityQ.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.1/out/production/ProgrammingProject5.1/LinkedListPriorityQ.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.1/out/production/ProgrammingProject5.1/PriorityQ.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.1/out/production/ProgrammingProject5.1/PriorityQ.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.1/out/production/ProgrammingProject5.1/SortedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.1/out/production/ProgrammingProject5.1/SortedList.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.1/src/LinkedListPriorityQ.java: -------------------------------------------------------------------------------- 1 | class Link{ 2 | public long data; 3 | public Link next; 4 | 5 | public Link(long n){ 6 | data = n; 7 | } 8 | 9 | public void printData(){ 10 | System.out.print(data + " "); 11 | } 12 | } 13 | 14 | class SortedList{ 15 | public Link first; 16 | 17 | public boolean isEmpty(){ 18 | return first == null; 19 | } 20 | 21 | public void insert(long key) 22 | { 23 | Link newLink = new Link(key); 24 | Link previous = null; 25 | Link current = first; 26 | // until end of list, 27 | while(current != null && key > current.data) 28 | { 29 | previous = current; 30 | current = current.next; 31 | } 32 | if(previous==null) 33 | first = newLink; 34 | else 35 | previous.next = newLink; 36 | newLink.next = current; 37 | } 38 | 39 | public Link remove() 40 | { 41 | Link temp = first; 42 | first = first.next; 43 | return temp; 44 | } 45 | 46 | public void displayList() 47 | { 48 | Link current = first; 49 | while(current != null) 50 | { 51 | current.printData(); 52 | current = current.next; 53 | } 54 | System.out.println(); 55 | } 56 | 57 | } 58 | 59 | class PriorityQ{ 60 | 61 | SortedList linkedList; 62 | 63 | public PriorityQ(){ 64 | linkedList = new SortedList(); 65 | } 66 | public long remove(){ 67 | Link temp = linkedList.remove(); 68 | return temp.data; 69 | } 70 | public long peek(){ 71 | return linkedList.first.data; 72 | } 73 | public boolean isEmpty(){ 74 | return linkedList.isEmpty(); 75 | } 76 | public void insert(long n){ 77 | linkedList.insert(n); 78 | } 79 | 80 | } 81 | 82 | public class LinkedListPriorityQ { 83 | public static void main(String args[]){ 84 | PriorityQ test = new PriorityQ(); 85 | test.insert(1); 86 | test.insert(5); 87 | test.insert(3); 88 | test.insert(4); 89 | test.insert(2); 90 | while(!test.isEmpty()){ 91 | System.out.println(test.remove()); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.2/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 1.8 11 | 12 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.2/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.2/ProgrammingProject5.2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.2/out/production/ProgrammingProject5.2/Deque.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.2/out/production/ProgrammingProject5.2/Deque.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.2/out/production/ProgrammingProject5.2/DoublyLinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.2/out/production/ProgrammingProject5.2/DoublyLinkedList.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.2/out/production/ProgrammingProject5.2/Link.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.2/out/production/ProgrammingProject5.2/Link.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.2/out/production/ProgrammingProject5.2/LinkedListDeque.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.2/out/production/ProgrammingProject5.2/LinkedListDeque.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.2/src/LinkedListDeque.java: -------------------------------------------------------------------------------- 1 | class Link{ 2 | public long data; 3 | 4 | public Link next; 5 | public Link previous; 6 | public Link(long n){ 7 | data = n; 8 | } 9 | 10 | public void printData(){ 11 | System.out.print(data + " "); 12 | } 13 | } 14 | 15 | class DoublyLinkedList 16 | { 17 | public Link first; 18 | public Link last; 19 | 20 | public boolean isEmpty() 21 | { return first==null;} 22 | 23 | public void insertFirst(long dd) // insert at front of list 24 | { 25 | Link newLink = new Link(dd); 26 | 27 | if( isEmpty() ) 28 | last = newLink; 29 | else 30 | first.previous = newLink; 31 | newLink.next = first; 32 | first = newLink; 33 | } 34 | 35 | public void insertLast(long dd) // insert at end of list 36 | { 37 | Link newLink = new Link(dd); 38 | if( isEmpty() ) 39 | first = newLink; 40 | else 41 | { 42 | last.next = newLink; 43 | newLink.previous = last; 44 | } 45 | last = newLink; 46 | } 47 | 48 | public Link deleteFirst() // delete first link 49 | { 50 | Link temp = first; 51 | if(first.next == null) 52 | last = null; 53 | else 54 | first.next.previous = null; 55 | first = first.next; 56 | return temp; 57 | } 58 | 59 | public Link deleteLast() // delete last link 60 | { 61 | Link temp = last; 62 | if(first.next == null) 63 | first = null; 64 | else 65 | last.previous.next = null; 66 | last = last.previous; 67 | return temp; 68 | } 69 | } 70 | 71 | class Deque { 72 | DoublyLinkedList list; 73 | 74 | public Deque(int m) { 75 | list = new DoublyLinkedList(); 76 | } 77 | 78 | public void insertLeft(long n) { 79 | list.insertFirst(n); 80 | } 81 | 82 | public void insertRight(long n) { 83 | list.insertLast(n); 84 | } 85 | 86 | public long removeLeft() { 87 | return list.deleteFirst().data; 88 | } 89 | 90 | public long removeRight() { 91 | return list.deleteLast().data; 92 | } 93 | 94 | public long peekLeft() { 95 | return list.first.data; 96 | } 97 | 98 | public long peekRight() { 99 | return list.last.data; 100 | } 101 | 102 | public boolean isEmpty() { 103 | return list.isEmpty(); 104 | } 105 | } 106 | 107 | public class LinkedListDeque { 108 | 109 | public static void main(String[] args) { 110 | Deque thing = new Deque(100); 111 | for (int count = 0; count < 100; count++) { 112 | thing.insertLeft(count); 113 | } 114 | for (int count = 0; count < 10; count++) { 115 | thing.removeRight(); 116 | } 117 | for (int count = 0; count < 10; count++) { 118 | thing.insertRight(count); 119 | } 120 | while (!thing.isEmpty()) { 121 | System.out.print(thing.removeLeft() + " "); 122 | } 123 | 124 | } 125 | 126 | } -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.3/.idea/misc.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.3/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.3/Programming Project 5.3.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.3/out/production/Programming Project 5.3/CircleListApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.3/out/production/Programming Project 5.3/CircleListApp.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.3/out/production/Programming Project 5.3/CircularList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.3/out/production/Programming Project 5.3/CircularList.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.3/out/production/Programming Project 5.3/Link.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.3/out/production/Programming Project 5.3/Link.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.3/src/CircleListApp.java: -------------------------------------------------------------------------------- 1 | class Link{ 2 | Link next; 3 | int key; 4 | long data; 5 | public Link(long n, int k){ 6 | data = n; 7 | key = k; 8 | } 9 | void display() { 10 | System.out.print(data + " "); 11 | } 12 | } 13 | 14 | class CircularList{ 15 | Link current; 16 | 17 | public boolean isEmpty(){ 18 | return current == null || current.next == current; 19 | } 20 | 21 | public void insert(long n, int k) { 22 | if (isEmpty()) 23 | current = new Link(n,k); 24 | else { 25 | Link toadd = new Link(n,k); 26 | if(current.next == null) { 27 | current.next = toadd; 28 | toadd.next = current; 29 | step(); 30 | } 31 | else{ 32 | toadd.next = current.next; 33 | current.next = toadd; 34 | step(); 35 | } 36 | } 37 | } 38 | 39 | public Link search(int k){ 40 | Link temp = current; 41 | int startKey = current.key; 42 | if(startKey == k){ 43 | return current; 44 | } 45 | else { 46 | while (temp.key != k) { 47 | temp = temp.next; 48 | if(temp == null) { 49 | System.out.println("Could not find item: " + k); 50 | return null; 51 | } 52 | if(temp.key == startKey) { 53 | System.out.println("Could not find item: " + k); 54 | return null; 55 | } 56 | } 57 | return temp; 58 | } 59 | } 60 | 61 | public Link delete(int k){ 62 | Link previous = current; 63 | Link consider = current.next; 64 | if(consider == null){ 65 | if(previous.key == k) { 66 | current = null; 67 | return previous; 68 | } 69 | else{ 70 | System.out.println("Could not find item: " + k); 71 | return null; 72 | } 73 | } 74 | if(consider.key == k){ 75 | if(consider.next == previous) 76 | previous.next = null; 77 | else if(consider.next == null) 78 | current = null; 79 | else 80 | previous.next = consider.next; 81 | return consider; 82 | } 83 | int startKey = consider.key; 84 | previous = consider; 85 | consider = previous.next; 86 | while(consider.key != k){ 87 | previous = consider; 88 | consider = previous.next; 89 | if(consider.key == startKey){ 90 | System.out.println("Could not find item: " + k); 91 | return null; 92 | } 93 | } 94 | if(consider.next == previous) 95 | previous.next = null; 96 | else 97 | previous.next = consider.next; 98 | return consider; 99 | } 100 | 101 | public void step(){ 102 | current = current.next; 103 | } 104 | 105 | public void display(){ 106 | if(!isEmpty()) { 107 | if(current.next == null){ 108 | current.display(); 109 | } 110 | else { 111 | int startKey = current.key; 112 | step(); 113 | current.display(); 114 | while (current.key != startKey) { 115 | step(); 116 | current.display(); 117 | } 118 | } 119 | } 120 | else{ 121 | System.out.println("The List Is Empty"); 122 | } 123 | System.out.println(); 124 | } 125 | } 126 | 127 | public class CircleListApp { 128 | public static void main(String args[]){ 129 | CircularList list = new CircularList(); 130 | for(int count = 0;count < 100;count++){ 131 | list.insert(count,count); 132 | } 133 | list.display(); 134 | System.out.println("Deleting The Items"); 135 | for(int count = 99;count >=0;count--){ 136 | list.delete(count); 137 | } 138 | list.display(); 139 | 140 | 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.4/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.4/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.4/ProgrammingProject54.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.4/out/production/ProgrammingProject54/CircularList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.4/out/production/ProgrammingProject54/CircularList.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.4/out/production/ProgrammingProject54/CircularListStackApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.4/out/production/ProgrammingProject54/CircularListStackApp.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.4/out/production/ProgrammingProject54/Link.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.4/out/production/ProgrammingProject54/Link.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.4/out/production/ProgrammingProject54/Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.4/out/production/ProgrammingProject54/Stack.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.4/src/CircularListStackApp.java: -------------------------------------------------------------------------------- 1 | class Link{ 2 | Link next; 3 | int key; 4 | long data; 5 | public Link(long n, int k){ 6 | data = n; 7 | key = k; 8 | } 9 | void display() { 10 | System.out.print(data + " "); 11 | } 12 | } 13 | 14 | class CircularList{ 15 | Link current; 16 | 17 | public boolean isEmpty(){ 18 | return current == null || current.next == current; 19 | } 20 | 21 | public void insert(long n, int k) { 22 | if (isEmpty()) 23 | current = new Link(n,k); 24 | else { 25 | Link toadd = new Link(n,k); 26 | if(current.next == null) { 27 | current.next = toadd; 28 | toadd.next = current; 29 | step(); 30 | } 31 | else{ 32 | toadd.next = current.next; 33 | current.next = toadd; 34 | step(); 35 | } 36 | } 37 | } 38 | 39 | public Link search(int k){ 40 | Link temp = current; 41 | int startKey = current.key; 42 | if(startKey == k){ 43 | return current; 44 | } 45 | else { 46 | while (temp.key != k) { 47 | temp = temp.next; 48 | if(temp == null) { 49 | System.out.println("Could not find item: " + k); 50 | return null; 51 | } 52 | if(temp.key == startKey) { 53 | System.out.println("Could not find item: " + k); 54 | return null; 55 | } 56 | } 57 | return temp; 58 | } 59 | } 60 | 61 | public Link delete(int k){ 62 | Link previous = current; 63 | Link consider = current.next; 64 | if(consider == null){ 65 | if(previous.key == k) { 66 | current = null; 67 | return previous; 68 | } 69 | else{ 70 | System.out.println("Could not find item: " + k); 71 | return null; 72 | } 73 | } 74 | if(consider.key == k){ 75 | if(consider.next == previous) 76 | previous.next = null; 77 | else if(consider.next == null) 78 | current = null; 79 | else 80 | previous.next = consider.next; 81 | return consider; 82 | } 83 | int startKey = consider.key; 84 | previous = consider; 85 | consider = previous.next; 86 | while(consider.key != k){ 87 | previous = consider; 88 | consider = previous.next; 89 | if(consider.key == startKey){ 90 | System.out.println("Could not find item: " + k); 91 | return null; 92 | } 93 | } 94 | if(consider.next == previous) 95 | previous.next = null; 96 | else 97 | previous.next = consider.next; 98 | return consider; 99 | } 100 | 101 | public void step(){ 102 | current = current.next; 103 | } 104 | 105 | public void display(){ 106 | if(!isEmpty()) { 107 | if(current.next == null){ 108 | current.display(); 109 | } 110 | else { 111 | int startKey = current.key; 112 | step(); 113 | current.display(); 114 | while (current.key != startKey) { 115 | step(); 116 | current.display(); 117 | } 118 | } 119 | } 120 | else{ 121 | System.out.println("The List Is Empty"); 122 | } 123 | System.out.println(); 124 | } 125 | } 126 | 127 | class Stack{ 128 | 129 | CircularList list; 130 | int count = 0; 131 | public Stack(){ 132 | list = new CircularList(); 133 | } 134 | 135 | public boolean isEmpty(){ 136 | return list.isEmpty(); 137 | } 138 | public long pop(){ 139 | if(!isEmpty()) { 140 | count--; 141 | return list.delete(count + 1).data; 142 | } 143 | return -1; 144 | } 145 | public void push(long n){ 146 | count++; 147 | list.insert(n,count); 148 | } 149 | public long peek(){ 150 | return list.current.data; 151 | } 152 | } 153 | 154 | 155 | 156 | 157 | public class CircularListStackApp { 158 | public static void main(String args[]){ 159 | Stack stack = new Stack(); 160 | stack.push(1); 161 | stack.push(2); 162 | stack.push(3); 163 | while(!stack.isEmpty()){ 164 | System.out.println(stack.pop()); 165 | } 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.5/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.5/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.5/ProgrammingProject5.5.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.5/out/production/ProgrammingProject5.5/CircularList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.5/out/production/ProgrammingProject5.5/CircularList.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.5/out/production/ProgrammingProject5.5/JosephusProblemApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.5/out/production/ProgrammingProject5.5/JosephusProblemApp.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.5/out/production/ProgrammingProject5.5/Link.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.5/out/production/ProgrammingProject5.5/Link.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.5/src/JosephusProblemApp.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Link{ 4 | Link next; 5 | int key; 6 | long data; 7 | public Link(long n, int k){ 8 | data = n; 9 | key = k; 10 | } 11 | void display() { 12 | System.out.print(data + " "); 13 | } 14 | } 15 | 16 | class CircularList{ 17 | Link current; 18 | 19 | public boolean isEmpty(){ 20 | return current == null || current.next == current; 21 | } 22 | 23 | public void insert(long n, int k) { 24 | if (isEmpty()) 25 | current = new Link(n,k); 26 | else { 27 | Link toadd = new Link(n,k); 28 | if(current.next == null) { 29 | current.next = toadd; 30 | toadd.next = current; 31 | step(); 32 | } 33 | else{ 34 | toadd.next = current.next; 35 | current.next = toadd; 36 | step(); 37 | } 38 | } 39 | } 40 | 41 | public Link search(int k){ 42 | Link temp = current; 43 | int startKey = current.key; 44 | if(startKey == k){ 45 | return current; 46 | } 47 | else { 48 | while (temp.key != k) { 49 | temp = temp.next; 50 | if(temp == null) { 51 | System.out.println("Could not find item: " + k); 52 | return null; 53 | } 54 | if(temp.key == startKey) { 55 | System.out.println("Could not find item: " + k); 56 | return null; 57 | } 58 | } 59 | return temp; 60 | } 61 | } 62 | 63 | public Link delete(int k){ 64 | Link previous = current; 65 | Link consider = current.next; 66 | if(consider == null){ 67 | if(previous.key == k) { 68 | current = null; 69 | return previous; 70 | } 71 | else{ 72 | System.out.println("Could not find item: " + k); 73 | return null; 74 | } 75 | } 76 | if(consider.key == k){ 77 | if(consider.next == previous) 78 | previous.next = null; 79 | else if(consider.next == null) 80 | current = null; 81 | else 82 | previous.next = consider.next; 83 | return consider; 84 | } 85 | int startKey = consider.key; 86 | previous = consider; 87 | consider = previous.next; 88 | while(consider.key != k){ 89 | previous = consider; 90 | consider = previous.next; 91 | if(consider.key == startKey){ 92 | System.out.println("Could not find item: " + k); 93 | return null; 94 | } 95 | } 96 | if(consider.next == previous) 97 | previous.next = null; 98 | else if(consider.next == null) 99 | current = null; 100 | else 101 | previous.next = consider.next; 102 | return consider; 103 | } 104 | 105 | public void step(){ 106 | current = current.next; 107 | } 108 | 109 | public void display(){ 110 | if(!isEmpty()) { 111 | if(current.next == null){ 112 | current.display(); 113 | } 114 | else { 115 | int startKey = current.key; 116 | step(); 117 | current.display(); 118 | while (current.key != startKey) { 119 | step(); 120 | current.display(); 121 | } 122 | } 123 | } 124 | System.out.println(); 125 | } 126 | 127 | public void goTo(int k){ 128 | while(current.key!=k){ 129 | current = current.next; 130 | } 131 | } 132 | 133 | } 134 | 135 | public class JosephusProblemApp { 136 | public static void main (String args[]){ 137 | Scanner input = new Scanner(System.in); 138 | CircularList circle = new CircularList(); 139 | System.out.println("Enter the number of people"); 140 | int people = input.nextInt(); 141 | System.out.println("Enter the increment"); 142 | int count = input.nextInt(); 143 | System.out.println("Enter the starting person"); 144 | int start = input.nextInt(); 145 | 146 | for(int i = 1;i <=people;i++){ 147 | circle.insert(i,i); 148 | } 149 | circle.goTo(start); 150 | 151 | while(!circle.isEmpty()){ 152 | for(int step = 0;step < count;step++){ 153 | if(circle.current.next!=null) 154 | circle.step(); 155 | } 156 | System.out.println(circle.delete(circle.current.key).data); 157 | if(circle.isEmpty()) 158 | break; 159 | 160 | if(circle.current.next!=null) 161 | circle.step(); 162 | } 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.6/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.6/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.6/ProgrammingProject5.6.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.6/out/production/ProgrammingProject5.6/Link.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.6/out/production/ProgrammingProject5.6/Link.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.6/out/production/ProgrammingProject5.6/LinkList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.6/out/production/ProgrammingProject5.6/LinkList.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.6/out/production/ProgrammingProject5.6/LinkedList2DApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 5 - Linked Lists/ProgrammingProject5.6/out/production/ProgrammingProject5.6/LinkedList2DApp.class -------------------------------------------------------------------------------- /Chapter 5 - Linked Lists/ProgrammingProject5.6/src/LinkedList2DApp.java: -------------------------------------------------------------------------------- 1 | class Link 2 | { 3 | public int iData; // data item (key) 4 | public double dData; // data item 5 | public Link next; // next link in list 6 | public Link below; 7 | // ------------------------------------------------------------- 8 | public Link(int id, double dd) // constructor 9 | { 10 | iData = id; 11 | dData = dd; 12 | } 13 | // ------------------------------------------------------------- 14 | public void displayLink() // display ourself 15 | { 16 | System.out.print("{" + iData + ", " + dData + "} "); 17 | } 18 | } // end class Link 19 | //////////////////////////////////////////////////////////////// 20 | class LinkList 21 | { 22 | private Link first; // ref to first link on list 23 | // ------------------------------------------------------------- 24 | public LinkList() // constructor 25 | { 26 | first = null; // no links on list yet 27 | } 28 | // ------------------------------------------------------------- 29 | public void insertFirst(int id, double dd) { // make new link 30 | Link newLink = new Link(id, dd); 31 | newLink.next = first; // it points to old first link 32 | first = newLink; // now first points to this 33 | } 34 | // ------------------------------------------------------------- 35 | public void displayList() // display the list 36 | { 37 | System.out.println("List (first-->last): "); 38 | Link current = first; // start at beginning of list 39 | int rownum = 1; 40 | while(current!=null) { 41 | while (current != null) // until end of list, 42 | { 43 | current.displayLink(); // print data 44 | current = current.next; // move to next link 45 | } 46 | current = first; 47 | rownum++; 48 | for(int count = 1;count < rownum;count++){ 49 | current = current.below; 50 | } 51 | System.out.println(); 52 | } 53 | System.out.println(); 54 | } 55 | // ------------------------------------------------------------- 56 | public void newRow(int width){ 57 | int rownum = 1; 58 | Link current = first; 59 | if(current == null){ 60 | first = new Link(1,0); 61 | current = first; 62 | for(int count = 2; count <= width;count++){ 63 | current.next = new Link(count,0); 64 | current = current.next; 65 | } 66 | } 67 | else { 68 | int count = 1; 69 | while (current.below != null) { 70 | current = current.below; 71 | rownum++; 72 | } 73 | current.below = new Link(count + rownum * 10, 0); 74 | count++; 75 | while (count <= width) { 76 | current.next.below = new Link(count + rownum * 10, 0); 77 | current.below.next = current.next.below; 78 | current = current.next; 79 | count++; 80 | } 81 | } 82 | } 83 | // ------------------------------------------------------------- 84 | public void setLink(int x, int y, long n){ 85 | Link current = first; 86 | for(int count = 1;count < x;count++){ 87 | if(current == null) 88 | break; 89 | current = current.next; 90 | } 91 | for(int count = 1;count < y;count++){ 92 | if(current == null) 93 | break; 94 | current = current.below; 95 | } 96 | current.dData = n; 97 | 98 | } 99 | } // end class LinkList 100 | 101 | public class LinkedList2DApp { 102 | public static void main(String args[]){ 103 | LinkList list = new LinkList(); 104 | 105 | list.newRow(4); 106 | list.displayList(); 107 | list.newRow(4); 108 | list.displayList(); 109 | list.newRow(4); 110 | list.displayList(); 111 | list.setLink(4,2,52); 112 | list.setLink(2,3,2); 113 | list.displayList(); 114 | 115 | 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.1/.idea/.name: -------------------------------------------------------------------------------- 1 | 6.1 -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.1/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.1/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.1/6.1.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.1/out/production/6.1/MultiplicatonApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 6 - Recursion/Programming Project 6.1/out/production/6.1/MultiplicatonApp.class -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.1/src/MultiplicatonApp.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class MultiplicatonApp { 3 | public static void main(String args[]){ 4 | System.out.println(mult(9,1)); 5 | System.out.println(mult(9,2)); 6 | System.out.println(mult(9,3)); 7 | System.out.println(mult(9,4)); 8 | System.out.println(mult(9,5)); 9 | System.out.println(mult(9,6)); 10 | System.out.println(mult(9,7)); 11 | System.out.println(mult(9,8)); 12 | System.out.println(mult(9,9)); 13 | System.out.println(mult(9,10)); 14 | } 15 | 16 | public static int mult(int x, int y){ 17 | if(y<=1){ 18 | return x; 19 | } 20 | else{ 21 | return x + mult(x,y-1); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.2/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.2/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.2/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 34 | 35 | 36 | 39 | 40 | 41 | 46 | 47 | 48 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 78 | 79 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 98 | 99 | 100 | 101 | 102 | 115 | 116 | 117 | 122 | 123 | 124 | 150 | 151 | 152 | 177 | 178 | 185 | 186 | 187 | 200 | 201 | 202 | 209 | 212 | 214 | 215 | 216 | 217 | 218 | 219 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 258 | 259 | 260 | 271 | 272 | 273 | 283 | 284 | 291 | 292 | 293 | 294 | 312 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 331 | 332 | 333 | 334 | 1499544300728 335 | 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 | 377 | 378 | 379 | 380 | 381 | 382 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.2/6.2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.2/out/production/6.2/TreeApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 6 - Recursion/Programming Project 6.2/out/production/6.2/TreeApp.class -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.2/src/TreeApp.java: -------------------------------------------------------------------------------- 1 | public class TreeApp { 2 | public static void main(String args[]){ 3 | startDrawing(7); 4 | } 5 | 6 | public static void drawBranch(char sym[][],int linenumber,int lines, int start, int end){ 7 | if(start>end || linenumber > lines-1){ 8 | return; 9 | } 10 | int change = (start+end)/2; 11 | sym[linenumber][change] = 'X'; 12 | drawBranch(sym,linenumber+1,lines,start,change); 13 | drawBranch(sym,linenumber+1,lines,change,end); 14 | } 15 | 16 | public static void startDrawing(int lines){ 17 | int size = (int)Math.pow(2,lines-1); 18 | char screen[][] = new char[lines][size]; 19 | for(int count = 0;count < screen.length;count++){ 20 | for(int count1 = 0;count1 < screen[count].length;count1++){ 21 | screen[count][count1] = '-'; 22 | } 23 | } 24 | drawBranch(screen,0,lines,0,screen[0].length); 25 | display(screen); 26 | } 27 | 28 | public static void display(char arr[][]){ 29 | for(int count = 0;count < arr.length;count++){ 30 | for(int count1 = 0;count1 < arr[count].length;count1++){ 31 | System.out.print(arr[count][count1] + " "); 32 | } 33 | System.out.println(); 34 | } 35 | } 36 | 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.3/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.3/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.3/6.3.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.3/out/production/6.3/PowerApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 6 - Recursion/Programming Project 6.3/out/production/6.3/PowerApp.class -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.3/src/PowerApp.java: -------------------------------------------------------------------------------- 1 | public class PowerApp { 2 | public static void main(String args[]){ 3 | System.out.println(power(3,20)); 4 | } 5 | 6 | public static long power(long x, long y){ 7 | if(y == 1){ 8 | return x; 9 | } 10 | else{ 11 | if(y%2 == 0) 12 | return power(x*x,y/2); 13 | else 14 | return x * power(x*x,y/2); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.4/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.4/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.4/6.4.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.4/out/production/6.4/KnapsackApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 6 - Recursion/Programming Project 6.4/out/production/6.4/KnapsackApp.class -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.4/src/KnapsackApp.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class KnapsackApp { 3 | public static void main(String args[]){ 4 | Scanner input = new Scanner(System.in); 5 | System.out.println("Enter the target weight"); 6 | int targetWeight = input.nextInt(); 7 | System.out.println("Enter the number of weights available"); 8 | int numWeights = input.nextInt(); 9 | System.out.println("Enter the number of values of the weights"); 10 | int weightsList[] = new int[numWeights]; 11 | for(int count = 0;count < numWeights;count++){ 12 | weightsList[count] = input.nextInt(); 13 | } 14 | boolean used[] = new boolean[numWeights]; 15 | boolean possible[] = {false}; 16 | reachTarget(targetWeight,weightsList,0, "",possible); 17 | if(possible[0]) 18 | System.out.println(" ... are the possible weights"); 19 | else 20 | System.out.println("There is no possible combination"); 21 | } 22 | public static void reachTarget(int target, int weights[], int index, String used, boolean possible[]){ 23 | if(target <0) { 24 | return; 25 | } 26 | if(target == 0){ 27 | possible[0] = true; 28 | System.out.println(used); 29 | return; 30 | } 31 | else if(index == weights.length){ 32 | return; 33 | } 34 | reachTarget(target,weights,index+1,used,possible); 35 | reachTarget(target-weights[index],weights,index+1,used + " " + weights[index],possible); 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.5/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 1.8 11 | 12 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.5/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.5/ProgrammingProject6.5.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.5/out/production/ProgrammingProject6.5/TeamApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 6 - Recursion/Programming Project 6.5/out/production/ProgrammingProject6.5/TeamApp.class -------------------------------------------------------------------------------- /Chapter 6 - Recursion/Programming Project 6.5/src/TeamApp.java: -------------------------------------------------------------------------------- 1 | public class TeamApp { 2 | public static void main(String args[]){ 3 | String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 4 | printTeams(5,3,alphabet); 5 | } 6 | 7 | 8 | public static void printTeams(int letters, int choose,String alphabet){ 9 | makeTeams(letters,choose,alphabet.substring(0,letters),"",choose); 10 | } 11 | public static void makeTeams(int letters, int choose,String alphabet, String sofar, int ogchoose){ 12 | if(letters == 0|| choose == 0){ 13 | if(sofar.length() == ogchoose) 14 | System.out.println(sofar); 15 | return; 16 | } 17 | makeTeams(letters-1,choose-1,alphabet,sofar+alphabet.charAt(alphabet.length()-letters),ogchoose); 18 | makeTeams(letters-1,choose,alphabet,sofar,ogchoose); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.1/.idea/.name: -------------------------------------------------------------------------------- 1 | Project 7.1 -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.1/.idea/misc.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.1/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.1/Programming Project 7.1.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.1/out/production/Programming Project 7.1/ArrayPar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.1/out/production/Programming Project 7.1/ArrayPar.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.1/out/production/Programming Project 7.1/PartitionApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.1/out/production/Programming Project 7.1/PartitionApp.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.1/src/PartitionApp.java: -------------------------------------------------------------------------------- 1 | // modified for problem 7.1 2 | class ArrayPar 3 | { 4 | private long[] theArray; // ref to array theArray 5 | private int nElems; // number of data items 6 | //-------------------------------------------------------------- 7 | public ArrayPar(int max) // constructor 8 | { 9 | theArray = new long[max]; // create the array 10 | nElems = 0; // no items yet 11 | } 12 | //-------------------------------------------------------------- 13 | public void insert(long value) // put element into array 14 | { 15 | theArray[nElems] = value; // insert it 16 | nElems++; // increment size 17 | } 18 | //-------------------------------------------------------------- 19 | public int size() // return number of items 20 | { return nElems; } 21 | //-------------------------------------------------------------- 22 | public void display() // displays array contents 23 | { 24 | System.out.print("A="); 25 | for(int j=0; j left && // find smaller item 43 | theArray[--rightPtr] > pivot) 44 | ; // (nop) 45 | if(leftPtr >= rightPtr) // if pointers cross, 46 | break; // partition done 47 | else // not crossed, so 48 | swap(leftPtr, rightPtr); // swap elements 49 | 50 | } // end while(true) 51 | swap(right, leftPtr); 52 | return leftPtr; // return partition 53 | } // end partitionIt() 54 | //-------------------------------------------------------------- 55 | public void swap(int dex1, int dex2) // swap two elements 56 | { 57 | long temp; 58 | temp = theArray[dex1]; // A into temp 59 | theArray[dex1] = theArray[dex2]; // B into A 60 | theArray[dex2] = temp; // temp into B 61 | } // end swap() 62 | //-------------------------------------------------------------- 63 | } // end class ArrayPar 64 | //////////////////////////////////////////////////////////////// 65 | class PartitionApp 66 | { 67 | public static void main(String[] args) 68 | { 69 | int maxSize = 10; // array size 70 | ArrayPar arr; // reference to array 71 | arr = new ArrayPar(maxSize); // create the array 72 | 73 | for(int j=0; j 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.2/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.2/Programming Project 7.2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.2/out/production/Programming Project 7.2/ArrayIns.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.2/out/production/Programming Project 7.2/ArrayIns.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.2/out/production/Programming Project 7.2/QuickSort2App.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.2/out/production/Programming Project 7.2/QuickSort2App.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.2/src/QuickSort2App.java: -------------------------------------------------------------------------------- 1 | //Modified for problem 7.2 2 | class ArrayIns 3 | { 4 | private long[] theArray; // ref to array theArray 5 | private int nElems; // number of data items 6 | 7 | int copies; 8 | int comparisons; 9 | //-------------------------------------------------------------- 10 | public ArrayIns(int max) // constructor 11 | { 12 | theArray = new long[max]; // create the array 13 | nElems = 0; // no items yet 14 | } 15 | //-------------------------------------------------------------- 16 | public void insert(long value) // put element into array 17 | { 18 | theArray[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | //-------------------------------------------------------------- 22 | public void display() // displays array contents 23 | { 24 | System.out.print("A="); 25 | for(int j=0; j theArray[center] ) { 58 | swap(left, center); 59 | } 60 | // order left & right 61 | if( theArray[left] > theArray[right] ) { 62 | swap(left, right); 63 | } 64 | // order center & right 65 | if( theArray[center] > theArray[right] ) { 66 | swap(center, right); 67 | } 68 | swap(center, right-1); // put pivot on right 69 | comparisons+=3; // due to the 3 if statements 70 | return theArray[right-1]; // return median value 71 | } // end medianOf3() 72 | //-------------------------------------------------------------- 73 | public void swap(int dex1, int dex2) // swap two elements 74 | { 75 | copies+=3; 76 | long temp = theArray[dex1]; // A into temp 77 | theArray[dex1] = theArray[dex2]; // B into A 78 | theArray[dex2] = temp; // temp into B 79 | } // end swap( 80 | //-------------------------------------------------------------- 81 | public int partitionIt(int left, int right, long pivot) 82 | { 83 | int leftPtr = left; // right of first elem 84 | int rightPtr = right - 1; // left of pivot 85 | 86 | while(true) 87 | { 88 | while( theArray[++leftPtr] < pivot ) comparisons++ // find bigger 89 | ; // (nop) 90 | comparisons++; 91 | while( theArray[--rightPtr] > pivot ) comparisons++ // find smaller 92 | ; // (nop) 93 | comparisons++; 94 | if(leftPtr >= rightPtr) // if pointers cross, 95 | break; 96 | // partition done 97 | else // not crossed, so 98 | swap(leftPtr, rightPtr); // swap elements 99 | } // end while(true) 100 | swap(leftPtr, right-1); // restore pivot 101 | return leftPtr; // return pivot location 102 | } // end partitionIt() 103 | //-------------------------------------------------------------- 104 | public void manualSort(int left, int right) 105 | { 106 | int size = right-left+1; 107 | if(size <= 1) 108 | return; // no sort necessary 109 | if(size == 2) 110 | { // 2-sort left and right 111 | if( theArray[left] > theArray[right] ) { 112 | swap(left, right); 113 | comparisons++; 114 | } 115 | return; 116 | } 117 | else // size is 3 118 | { // 3-sort left, center, & right 119 | if( theArray[left] > theArray[right-1] ) 120 | swap(left, right-1); // left, center 121 | if( theArray[left] > theArray[right] ) 122 | swap(left, right); // left, right 123 | if( theArray[right-1] > theArray[right] ) 124 | swap(right-1, right); // center, right 125 | comparisons+=3; 126 | } 127 | } // end manualSort() 128 | //-------------------------------------------------------------- 129 | } // end class ArrayIns 130 | //////////////////////////////////////////////////////////////// 131 | class QuickSort2App 132 | { 133 | public static void main(String[] args) 134 | { 135 | int maxSize = 16; // array size 136 | ArrayIns arr; // reference to array 137 | arr = new ArrayIns(maxSize); // create the array 138 | 139 | for(int j=0; j 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.3/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.3/Project 7.3.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.3/out/production/Project 7.3/ArrayPar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.3/out/production/Project 7.3/ArrayPar.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.3/out/production/Project 7.3/MedianApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.3/out/production/Project 7.3/MedianApp.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.3/src/MedianApp.java: -------------------------------------------------------------------------------- 1 | //extended project 7.1 2 | import java.util.Random; 3 | class ArrayPar 4 | { 5 | private long[] theArray; // ref to array theArray 6 | private int nElems; // number of data items 7 | //-------------------------------------------------------------- 8 | public ArrayPar(int max) // constructor 9 | { 10 | theArray = new long[max]; // create the array 11 | nElems = 0; // no items yet 12 | } 13 | //-------------------------------------------------------------- 14 | public void insert(long value) // put element into array 15 | { 16 | theArray[nElems] = value; // insert it 17 | nElems++; // increment size 18 | } 19 | //-------------------------------------------------------------- 20 | public int size() // return number of items 21 | { return nElems; } 22 | //-------------------------------------------------------------- 23 | public void display() // displays array contents 24 | { 25 | System.out.print("A="); 26 | for(int j=0; j left && // find smaller item 43 | theArray[--rightPtr] > pivot) 44 | ; // (nop) 45 | if(leftPtr >= rightPtr) // if pointers cross, 46 | break; // partition done 47 | else // not crossed, so 48 | swap(leftPtr, rightPtr); // swap elements 49 | 50 | } // end while(true) 51 | swap(right, leftPtr); 52 | return leftPtr; // return partition 53 | } // end partitionIt() 54 | //-------------------------------------------------------------- 55 | public void swap(int dex1, int dex2) // swap two elements 56 | { 57 | long temp; 58 | temp = theArray[dex1]; // A into temp 59 | theArray[dex1] = theArray[dex2]; // B into A 60 | theArray[dex2] = temp; // temp into B 61 | } // end swap() 62 | 63 | public long findMiddleNumber(){ 64 | int left = 0; 65 | int right = nElems-1; 66 | while(true) { 67 | int currentIndex = partitionIt(left, right); 68 | if(currentIndex == nElems/2){ 69 | return theArray[currentIndex]; 70 | } 71 | else if(currentIndex > nElems/2){ 72 | right = currentIndex - 1; 73 | } 74 | else{ 75 | left = currentIndex + 1; 76 | } 77 | } 78 | } 79 | //-------------------------------------------------------------- 80 | } // end class ArrayPar 81 | //////////////////////////////////////////////////////////////// 82 | 83 | 84 | public class MedianApp { 85 | public static void main(String args[]){ 86 | int arrSize = 25; 87 | ArrayPar arr = new ArrayPar(arrSize); 88 | Random numGenerator = new Random(); 89 | 90 | for(int i = 0;i < arrSize;i++){ 91 | arr.insert(numGenerator.nextInt(100)); 92 | } 93 | 94 | arr.display(); 95 | System.out.println("The middle number is " + arr.findMiddleNumber()); 96 | arr.display(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.4/.idea/.name: -------------------------------------------------------------------------------- 1 | Project 7.4 -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.4/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.4/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.4/Project 7.4.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.4/out/production/Project 7.4/ArrayPar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.4/out/production/Project 7.4/ArrayPar.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.4/out/production/Project 7.4/findNumAtKApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.4/out/production/Project 7.4/findNumAtKApp.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.4/src/findNumAtKApp.java: -------------------------------------------------------------------------------- 1 | //extended project 7.1, 7.3 2 | import java.util.Random; 3 | import java.util.Scanner; 4 | class ArrayPar 5 | { 6 | private long[] theArray; // ref to array theArray 7 | private int nElems; // number of data items 8 | //-------------------------------------------------------------- 9 | public ArrayPar(int max) // constructor 10 | { 11 | theArray = new long[max]; // create the array 12 | nElems = 0; // no items yet 13 | } 14 | //-------------------------------------------------------------- 15 | public void insert(long value) // put element into array 16 | { 17 | theArray[nElems] = value; // insert it 18 | nElems++; // increment size 19 | } 20 | //-------------------------------------------------------------- 21 | public int size() // return number of items 22 | { return nElems; } 23 | //-------------------------------------------------------------- 24 | public void display() // displays array contents 25 | { 26 | System.out.print("A="); 27 | for(int j=0; j left && // find smaller item 44 | theArray[--rightPtr] > pivot) 45 | ; // (nop) 46 | if(leftPtr >= rightPtr) // if pointers cross, 47 | break; // partition done 48 | else // not crossed, so 49 | swap(leftPtr, rightPtr); // swap elements 50 | 51 | } // end while(true) 52 | swap(right, leftPtr); 53 | return leftPtr; // return partition 54 | } // end partitionIt() 55 | //-------------------------------------------------------------- 56 | public void swap(int dex1, int dex2) // swap two elements 57 | { 58 | long temp; 59 | temp = theArray[dex1]; // A into temp 60 | theArray[dex1] = theArray[dex2]; // B into A 61 | theArray[dex2] = temp; // temp into B 62 | } // end swap() 63 | 64 | public long findNumAtIndex(int k){ 65 | int left = 0; 66 | int right = nElems-1; 67 | while(true) { 68 | int currentIndex = partitionIt(left, right); 69 | if(currentIndex == k){ 70 | return theArray[currentIndex]; 71 | } 72 | else if(currentIndex > k){ 73 | right = currentIndex - 1; 74 | } 75 | else{ 76 | left = currentIndex + 1; 77 | } 78 | } 79 | } 80 | //-------------------------------------------------------------- 81 | } // end class ArrayPar 82 | //////////////////////////////////////////////////////////////// 83 | public class findNumAtKApp { 84 | public static void main(String args[]){ 85 | int arrSize = 25; 86 | ArrayPar arr = new ArrayPar(arrSize); 87 | Random numGenerator = new Random(); 88 | 89 | for(int i = 0;i < arrSize;i++){ 90 | arr.insert(numGenerator.nextInt(100)); 91 | } 92 | 93 | arr.display(); 94 | System.out.println("Please input the kth smallest number you would like to know."); 95 | Scanner input = new Scanner(System.in); 96 | int k = input.nextInt(); 97 | if(k > 0 && k < 25) 98 | System.out.println("The number at K is " + arr.findNumAtIndex(k-1)); 99 | else 100 | System.out.println("Please try again with a valid number between 0 and " + (arr.size())); 101 | arr.display(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.5/.idea/.name: -------------------------------------------------------------------------------- 1 | Project 7.5 -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.5/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.5/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.5/Project 7.5.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.5/out/production/Project 7.5/Array.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.5/out/production/Project 7.5/Array.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.5/out/production/Project 7.5/radixSortApp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/manveertamber/Data-Structures-And-Algorithms-In-Java-Exercise-Solutions/6eb75f63889790219ce97f8305ab3f8bf1839ef7/Chapter 7 - Advanced Sorting/Programming Project 7.5/out/production/Project 7.5/radixSortApp.class -------------------------------------------------------------------------------- /Chapter 7 - Advanced Sorting/Programming Project 7.5/src/radixSortApp.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Random; 3 | 4 | class Array{ 5 | long arr[]; 6 | int size; 7 | public Array(int maxSize){ 8 | arr = new long[maxSize]; 9 | size = 0; 10 | } 11 | public void insert(long n){ 12 | arr[size] = n; 13 | size++; 14 | } 15 | public int getSize(){ 16 | return size; 17 | } 18 | public void display(){ 19 | for(int count = 0;count < size;count++){ 20 | System.out.print(arr[count] + " "); 21 | } 22 | System.out.println(); 23 | } 24 | public void radixSort(){ 25 | String nums[] = new String[size]; 26 | for(int count = 0;count < size;count++){ 27 | nums[count] = Long.toString(arr[count]); 28 | } 29 | ArrayList groups[] = new ArrayList[10]; 30 | for(int count = 0; count < 10;count++){ 31 | groups[count] = new ArrayList(); 32 | } 33 | int currentDigit = 0; 34 | boolean changed = true; 35 | while(changed){ 36 | changed = false; 37 | for(String x: nums){ 38 | if(x.length() < currentDigit + 1){ 39 | groups[0].add(x); 40 | } 41 | else{ 42 | groups[Character.getNumericValue(x.charAt(x.length() - 1 - currentDigit))].add(x); 43 | changed = true; 44 | } 45 | } 46 | int currentIndex = 0; 47 | for(int count = 0;count < 10;count++){ 48 | for(String x: groups[count]){ 49 | nums[currentIndex] = x; 50 | currentIndex++; 51 | } 52 | groups[count].clear(); 53 | } 54 | currentDigit++; 55 | } 56 | for(int count = 0;count < size;count++){ 57 | arr[count] = Long.parseLong(nums[count]); 58 | } 59 | } 60 | } 61 | 62 | 63 | public class radixSortApp { 64 | public static void main(String args[]){ 65 | int arraySize = 20; 66 | Array arr = new Array(arraySize); 67 | Random numGenerator = new Random(); 68 | 69 | for(int count = 0;count < arraySize;count++){ 70 | arr.insert(numGenerator.nextInt(10000)); 71 | } 72 | arr.display(); 73 | arr.radixSort(); 74 | arr.display(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures-And-Algorithms-In-Java-Exercise-Solutions 2 | These are my solutions for the "Programming Projects" in the book,"Data Structures and Algorithms in Java" by Robert Lafore 3 | --------------------------------------------------------------------------------