├── .gitignore ├── WorkshopApplets ├── Chap08 │ └── Tree │ │ ├── Tree.class │ │ ├── stack.class │ │ ├── person.class │ │ ├── personGroup.class │ │ └── Tree.html ├── Chap11 │ ├── Hash │ │ ├── Hash.class │ │ ├── person.class │ │ ├── personGroup.class │ │ └── Hash.html │ ├── HashChain │ │ ├── link.class │ │ ├── bucket.class │ │ ├── person.class │ │ ├── HashChain.class │ │ ├── personGroup.class │ │ └── HashChain.html │ └── HashDouble │ │ ├── person.class │ │ ├── HashDouble.class │ │ ├── personGroup.class │ │ └── HashDouble.html ├── Chap12 │ └── Heap │ │ ├── Heap.class │ │ ├── person.class │ │ ├── personGroup.class │ │ └── Heap.html ├── Chap02 │ ├── Array │ │ ├── Array.class │ │ ├── person.class │ │ ├── personGroup.class │ │ └── Array.html │ └── OrderedArray │ │ ├── Ordered.class │ │ ├── person.class │ │ ├── personGroup.class │ │ └── Ordered.html ├── Chap04 │ ├── Queue │ │ ├── Queue.class │ │ ├── person.class │ │ ├── personGroup.class │ │ └── Queue.html │ ├── Stack │ │ ├── Stack.class │ │ ├── person.class │ │ ├── personGroup.class │ │ └── Stack.html │ └── PriorityQ │ │ ├── person.class │ │ ├── PriorityQ.class │ │ ├── personGroup.class │ │ └── PriorityQ.html ├── Chap06 │ ├── Towers │ │ ├── disk.class │ │ ├── stack.class │ │ ├── tower.class │ │ ├── Towers.class │ │ ├── params.class │ │ ├── gameGroup.class │ │ └── Towers.html │ └── MergeSort │ │ ├── stack.class │ │ ├── params.class │ │ ├── person.class │ │ ├── MergeSort.class │ │ ├── personGroup.class │ │ └── MergeSort.html ├── Chap10 │ └── Tree234 │ │ ├── node.class │ │ ├── Tree234.class │ │ ├── person.class │ │ ├── nodeGroup.class │ │ └── Tree234.html ├── Chap13 │ ├── GraphN │ │ ├── Queue.class │ │ ├── stack.class │ │ ├── GraphN.class │ │ ├── vertex.class │ │ ├── vertexGroup.class │ │ └── GraphN.html │ └── GraphD │ │ ├── GraphD.class │ │ ├── vertex.class │ │ ├── vertexGroup.class │ │ └── GraphD.html ├── Chap14 │ ├── GraphDW │ │ ├── disP.class │ │ ├── GraphDW.class │ │ ├── disIs.class │ │ ├── vertex.class │ │ ├── vertexGroup.class │ │ └── GraphDW.html │ └── GraphW │ │ ├── disIs.class │ │ ├── edge.class │ │ ├── GraphW.class │ │ ├── vertex.class │ │ ├── priorityQ.class │ │ ├── vertexGroup.class │ │ └── GraphW.html ├── Chap03 │ ├── Bubble │ │ ├── groupBS.class │ │ ├── personBS.class │ │ ├── BubbleSort.class │ │ └── BubbleSort.html │ ├── Insertion │ │ ├── groupIS.class │ │ ├── InsertSort.class │ │ ├── personIS.class │ │ └── InsertSort.html │ └── Selection │ │ ├── groupSS.class │ │ ├── SelectSort.class │ │ ├── personSS.class │ │ └── SelectSort.html ├── Chap05 │ └── LinkList │ │ ├── link.class │ │ ├── person.class │ │ ├── LinkList.class │ │ ├── personGroup.class │ │ └── LinkList.html ├── Chap09 │ └── RBTree │ │ ├── RBTree.class │ │ ├── person.class │ │ ├── personGroup.class │ │ └── RBTree.html ├── Chap07 │ ├── Partition │ │ ├── person.class │ │ ├── Partition.class │ │ ├── personGroup.class │ │ └── Partition.html │ ├── QuickSort1 │ │ ├── person.class │ │ ├── stack.class │ │ ├── QuickSort1.class │ │ ├── personGroup.class │ │ └── QuickSort1.html │ ├── QuickSort2 │ │ ├── person.class │ │ ├── stack.class │ │ ├── QuickSort2.class │ │ ├── personGroup.class │ │ └── QuickSort2.html │ └── ShellSort │ │ ├── person.class │ │ ├── ShellSort.class │ │ ├── personGroup.class │ │ └── ShellSort.html ├── readme.txt └── AppletList.txt ├── README └── src ├── Chap06 ├── towers │ └── towers.java ├── triangle │ └── triangle.java ├── merge │ └── merge.java ├── anagram │ └── anagram.java ├── stackTriangle2 │ └── stackTriangle2.java ├── binarySearch │ └── binarySearch.java ├── mergeSort │ └── mergeSort.java └── stackTriangle │ └── stackTriangle.java ├── Chap01 └── Bank │ └── bank.java ├── Chap02 ├── Array │ └── array.java ├── LowArray │ └── lowArray.java ├── HighArray │ └── highArray.java ├── OrderedArray │ └── orderedArray.java └── ClassData │ └── classDataArray.java ├── Chap04 ├── Stack │ └── stack.java ├── PriorityQ │ └── priorityQ.java ├── Queue │ └── queue.java ├── Reverse │ └── reverse.java ├── Brackets │ └── brackets.java └── Postfix │ └── postfix.java ├── Chap03 ├── BubbleSort │ └── bubbleSort.java ├── InsertSort │ └── insertSort.java ├── SelectSort │ └── selectSort.java └── ObjectSort │ └── objectSort.java ├── Chap07 ├── shellSort │ └── shellSort.java ├── partition │ └── partition.java ├── quickSort1 │ └── quickSort1.java ├── quickSort3 │ └── quickSort3.java └── quickSort2 │ └── quickSort2.java ├── Chap05 ├── linkList │ └── linkList.java ├── sortedList │ └── sortedList.java ├── listInsertionSort │ └── listInsertionSort.java ├── firstLastList │ └── firstLastList.java ├── linkStack │ └── linkStack.java ├── linkQueue │ └── linkQueue.java └── linkList2 │ └── linkList2.java └── Chap13 ├── dfs └── dfs.java ├── bfs └── bfs.java └── mst └── mst.java /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap08/Tree/Tree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap08/Tree/Tree.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap08/Tree/stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap08/Tree/stack.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/Hash/Hash.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/Hash/Hash.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap12/Heap/Heap.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap12/Heap/Heap.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap02/Array/Array.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap02/Array/Array.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap02/Array/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap02/Array/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/Queue/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/Queue/Queue.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/Queue/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/Queue/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/Stack/Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/Stack/Stack.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/Stack/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/Stack/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/Towers/disk.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/Towers/disk.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/Towers/stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/Towers/stack.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/Towers/tower.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/Towers/tower.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap08/Tree/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap08/Tree/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap10/Tree234/node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap10/Tree234/node.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/Hash/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/Hash/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap12/Heap/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap12/Heap/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphN/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap13/GraphN/Queue.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphN/stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap13/GraphN/stack.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphDW/disP.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphDW/disP.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphW/disIs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphW/disIs.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphW/edge.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphW/edge.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Bubble/groupBS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Bubble/groupBS.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Bubble/personBS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Bubble/personBS.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap05/LinkList/link.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap05/LinkList/link.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap05/LinkList/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap05/LinkList/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/MergeSort/stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/MergeSort/stack.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/Towers/Towers.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/Towers/Towers.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/Towers/params.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/Towers/params.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap09/RBTree/RBTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap09/RBTree/RBTree.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap09/RBTree/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap09/RBTree/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap10/Tree234/Tree234.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap10/Tree234/Tree234.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap10/Tree234/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap10/Tree234/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashChain/link.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/HashChain/link.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphD/GraphD.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap13/GraphD/GraphD.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphD/vertex.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap13/GraphD/vertex.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphN/GraphN.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap13/GraphN/GraphN.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphN/vertex.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap13/GraphN/vertex.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphDW/GraphDW.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphDW/GraphDW.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphDW/disIs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphDW/disIs.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphDW/vertex.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphDW/vertex.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphW/GraphW.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphW/GraphW.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphW/vertex.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphW/vertex.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap02/Array/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap02/Array/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Bubble/BubbleSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Bubble/BubbleSort.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Insertion/groupIS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Insertion/groupIS.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Selection/groupSS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Selection/groupSS.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/PriorityQ/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/PriorityQ/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/Queue/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/Queue/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/Stack/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/Stack/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap05/LinkList/LinkList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap05/LinkList/LinkList.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/MergeSort/params.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/MergeSort/params.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/MergeSort/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/MergeSort/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/Towers/gameGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/Towers/gameGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/Partition/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/Partition/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort1/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/QuickSort1/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort1/stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/QuickSort1/stack.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort2/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/QuickSort2/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort2/stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/QuickSort2/stack.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/ShellSort/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/ShellSort/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap08/Tree/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap08/Tree/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap10/Tree234/nodeGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap10/Tree234/nodeGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/Hash/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/Hash/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashChain/bucket.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/HashChain/bucket.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashChain/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/HashChain/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashDouble/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/HashDouble/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap12/Heap/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap12/Heap/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphW/priorityQ.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphW/priorityQ.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap02/OrderedArray/Ordered.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap02/OrderedArray/Ordered.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap02/OrderedArray/person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap02/OrderedArray/person.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Insertion/InsertSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Insertion/InsertSort.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Insertion/personIS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Insertion/personIS.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Selection/SelectSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Selection/SelectSort.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Selection/personSS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap03/Selection/personSS.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/PriorityQ/PriorityQ.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/PriorityQ/PriorityQ.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap05/LinkList/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap05/LinkList/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/MergeSort/MergeSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/MergeSort/MergeSort.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/Partition/Partition.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/Partition/Partition.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/ShellSort/ShellSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/ShellSort/ShellSort.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap09/RBTree/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap09/RBTree/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashChain/HashChain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/HashChain/HashChain.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphD/vertexGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap13/GraphD/vertexGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphN/vertexGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap13/GraphN/vertexGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphDW/vertexGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphDW/vertexGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphW/vertexGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap14/GraphW/vertexGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/PriorityQ/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap04/PriorityQ/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/MergeSort/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap06/MergeSort/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/Partition/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/Partition/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort1/QuickSort1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/QuickSort1/QuickSort1.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort1/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/QuickSort1/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort2/QuickSort2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/QuickSort2/QuickSort2.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort2/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/QuickSort2/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/ShellSort/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap07/ShellSort/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashChain/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/HashChain/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashDouble/HashDouble.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/HashDouble/HashDouble.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashDouble/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap11/HashDouble/personGroup.class -------------------------------------------------------------------------------- /WorkshopApplets/Chap02/OrderedArray/personGroup.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymanmobarak/Lafore/HEAD/WorkshopApplets/Chap02/OrderedArray/personGroup.class -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Lafore Repository 2 | 3 | This is not too exciting, just a workspace where I can work with the examples in Robert Lafore's "Data Structures and Algorithms in Java" 4 | 5 | -Ayman Mobarak 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /WorkshopApplets/readme.txt: -------------------------------------------------------------------------------- 1 | These are the applets that accompany the book "Data Structures and Algorithms in Java," 2 | Second Edition. Robert Lafore, 2002 3 | 4 | The applets are little demonstration programs that clarify the topics in the book. 5 | For example, to demonstrate sorting algorithms, a bar chart is displayed and, each 6 | time the user pushes a button on the applet, another step of the algorithm is 7 | carried out. The user can see how the bars move, and annotations within the 8 | applet explain what's going on. 9 | 10 | Readers will not need the source code for these applets. They are simply an 11 | interactive visual aid for the book. Therefore only .class and 12 | .html files are available on the Sams web site. 13 | 14 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/Queue/Queue.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Queue 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Queue
Queue 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | New creates new queue 23 |

24 | 25 | Ins inserts item with value N at rear 26 | of queue. 27 |

28 | 29 | Rem removes item from front of queue, 30 | returns value. 31 |

32 | 33 | Peek returns value of item at front 34 | of queue. 35 |

36 | 37 | (Type N into "Enter number" box.) 38 |

39 |

40 | 41 | 42 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/Stack/Stack.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Stack 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Stack
Stack 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | New creates new stack 23 |

24 | 25 | Push inserts item with value N at top 26 | of stack. 27 |

28 | 29 | Pop removes item from top of stack, 30 | returns value. 31 |

32 | 33 | Peek returns value of item at top 34 | of stack. 35 |

36 | 37 | (Type N into "Enter number" box.) 38 |

39 |

40 | 41 | 42 | -------------------------------------------------------------------------------- /src/Chap06/towers/towers.java: -------------------------------------------------------------------------------- 1 | // towers.java 2 | // solves Towers of Hanoi puzzle 3 | // to run this program: C>java TowersApp 4 | //////////////////////////////////////////////////////////////// 5 | class TowersApp 6 | { 7 | static int nDisks = 3; 8 | 9 | public static void main(String[] args) 10 | { 11 | doTowers(nDisks, 'A', 'B', 'C'); 12 | } 13 | //----------------------------------------------------------- 14 | public static void doTowers(int topN, 15 | char src, char inter, char dest) 16 | { 17 | if(topN==1) 18 | System.out.println("Disk 1 from " + src + " to "+ dest); 19 | else 20 | { 21 | doTowers(topN-1, src, dest, inter); // src to inter 22 | 23 | System.out.println("Disk " + topN + // move bottom 24 | " from " + src + " to "+ dest); 25 | doTowers(topN-1, inter, src, dest); // inter to dest 26 | } 27 | } 28 | //------------------------------------------------------------- 29 | } // end class TowersApp 30 | //////////////////////////////////////////////////////////////// 31 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap04/PriorityQ/PriorityQ.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Priority Queue 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Priority Queue
Priority Queue 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | New creates new empty priority queue 23 |

24 | 25 | Ins inserts item with value N. 26 |

27 | 28 | Rem removes item from front of queue, 29 | returns value. 30 |

31 | 32 | Peek returns value of item at front 33 | of queue. 34 |

35 | 36 | (Type N into "Enter number" box.)

37 | 38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/Towers/Towers.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Towers of Hanoi 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Towers of Hanoi
Towers 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | Drag disk to another post. 23 |

24 | 25 | Type number to specify number of disks 26 | for New. 27 |

28 | 29 | New creates a new game with N disks. 30 |

31 | 32 | Step carries out one step of 33 | solution algorithm. 34 |

35 | 36 | Run starts the solution algorithm running. 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphD/GraphD.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Directed, Unweighted Graph 4 | 5 | 6 | 7 | 8 | 9 | 11 | 14 |
Lafore's Directed, 10 | Unweighted Graph
Graph 12 | Operation 13 |
15 | 19 | 20 | 21 |
22 | 23 | Double-click to create new vertex. 24 |

25 | 26 | Drag from vertex to vertex to create 27 | edge. 28 |

29 | 30 | New clears an old graph. 31 |

32 | 33 | Topo carries out topological sort. 34 |

35 | 36 | View toggles between graph and adjacency 37 | matrix. 38 | 39 |

40 | 41 | 42 | -------------------------------------------------------------------------------- /WorkshopApplets/AppletList.txt: -------------------------------------------------------------------------------- 1 | "Data Structures and Algorithms" 2 | Robert Lafore 3 | 7/26/02 4 | 5 | List of Applets 6 | --------------- 7 | Chapter 1 -- Overview 8 | (No applets) 9 | 10 | Chapter 2 -- Arrays 11 | 1) Array 12 | 2) OrderedArray 13 | 14 | Chapter 3 -- Simple Sorting 15 | 3) Bubble 16 | 4) Insertion 17 | 5) Selection 18 | 19 | Chapter 4 -- Stacks and Queues 20 | 6) Stack 21 | 7) Queue 22 | 8) PriorityQ 23 | 24 | Chapter 5 -- Linked Lists 25 | 9) LinkList 26 | 27 | Chapter 6 -- Recursion 28 | 10) Towers 29 | 11) mergeSort 30 | 31 | Chapter 7 -- Advanced Sorting 32 | 12) shellSort 33 | 13) partition 34 | 14) quickSort1 35 | 15) quickSort2 36 | 37 | Chapter 8 -- Binary Trees 38 | 16) Tree 39 | 40 | Chapter 9 -- Red-black Trees 41 | 17) RBTree 42 | 43 | Chapter 10 -- 2-3-4 Trees 44 | 18) Tree234 45 | 46 | Chapter 11 -- Hash Tables 47 | 19) Hash 48 | 20) HashDouble 49 | 21) HashChain 50 | 51 | Chapter 12 -- Heaps 52 | 22) Heap 53 | 54 | Chapter 13 -- Graphs 55 | 23) GraphN 56 | 24) GraphD 57 | 58 | Chapter 14 -- Weighted Graphs 59 | 25) GraphW 60 | 26) GraphDW 61 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap12/Heap/Heap.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Heap 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Heap
Heap 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | Fill creates new heap with N nodes. 23 |

24 | 25 | Chng changes selected node to value N. 26 |

27 | 28 | Click on node to select it. 29 |

30 | 31 | Rem removes node with highest key. 32 |

33 | 34 | Ins inserts new node with value N. 35 |

36 | 37 | (Type N into "Enter number" box.) 38 | 39 |

40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphW/GraphW.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Weighted, Undirected Graph 4 | 5 | 6 | 7 | 8 | 9 | 11 | 14 |
Lafore's Weighted, 10 | Undirected Graph
Graph 12 | Operation 13 |
15 | 19 | 20 | 21 |
22 | 23 | Double-click to create new vertex. 24 |

25 | 26 | Drag from vertex to vertex to create 27 | edge. (Enter weight first.) 28 |

29 | 30 | New clears an old graph. 31 |

32 | 33 | Tree creates minimum spanning tree. 34 |

35 | 36 | View toggles between graph and adjacency 37 | matrix. 38 | 39 |

40 | 41 | 42 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap02/Array/Array.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Unordered Array 4 | 5 | 6 | 7 | 8 | 11 |
Lafore's Unordered Array
Unordered Array 9 | Operation 10 |
12 | 16 | 17 | 18 |
19 | 20 | New creates array 21 | with N cells (60 max) 22 |

23 | 24 | Fill inserts N items into 25 | array. 26 |

27 | 28 | Ins inserts new item with 29 | value N. 30 |

31 | 32 | Find finds item(s) with 33 | value N. 34 |

35 | 36 | Del deletes item(s) with 37 | value N. 38 |

39 | 40 | (Type N into "Enter number" box.) 41 |

42 |

43 | 44 | 45 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/Hash/Hash.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Linear-Probe Hash Table 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Linear-Probe Hash Table
Hash Table 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | New creates hash table 23 | with N cells (60 max) 24 |

25 | 26 | Fill inserts N items into 27 | table. 28 |

29 | 30 | Ins inserts new item with 31 | key N. 32 |

33 | 34 | Del deletes item with 35 | key N. 36 |

37 | 38 | Find finds item with 39 | key N. 40 |

41 | 42 | (Type N into "Enter number" box.) 43 | 44 |

45 | 46 | 47 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap02/OrderedArray/Ordered.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Ordered Array 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Ordered Array
Ordered Array 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | New creates array 23 | with N cells (60 max) 24 |

25 | 26 | Fill inserts N items into 27 | array. 28 |

29 | 30 | Ins inserts new item with 31 | value N. 32 |

33 | 34 | Find finds item with 35 | value N. 36 |

37 | 38 | Del deletes item with 39 | value N. 40 |

41 | 42 | (Type N into "Enter number" box.) 43 |

44 |

45 | 46 | 47 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap05/LinkList/LinkList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Linked List 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Linked List
Linked List 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | New creates linked list 23 | with N items (28 max) 24 |

25 | 26 | Ins inserts new item with 27 | value N. 28 |

29 | 30 | Find finds item with 31 | value N. 32 |

33 | 34 | Del deletes item with 35 | value N. 36 |

37 | 38 | (Type N into "Enter number" box.) 39 |

40 | 41 | Unsorted/Sorted buttons are 42 | used with New. 43 |

44 |

45 | 46 | 47 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap06/MergeSort/MergeSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Merge Sort 4 | 5 | 6 | 7 | 8 | 9 | 12 |
Lafore's Merge Sort
Merge Sort 10 | Operation 11 |
13 | 17 | 18 | 19 | 20 | New creates new data and initializes sort; 21 | toggles between random and inverse order. 22 |

23 | 24 | Size toggles between 12 bars and 100 bars; 25 | also creates new data and initializes sort. 26 |

27 | 28 | Draw redraws bars. 29 |

30 | 31 | Run starts sorting process 32 | running automatically. (Push Step to pause, Run to resume.) 33 |

34 | 35 | Step executes one step of sorting 36 | process. 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/ShellSort/ShellSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Shell Sort 4 | 5 | 6 | 7 | 8 | 9 | 12 |
Lafore's Shell Sort
Shell Sort 10 | Operation 11 |
13 | 17 | 18 | 19 | 20 | New creates new data and initializes sort; 21 | toggles between random and inverse order. 22 |

23 | 24 | Size toggles between 10 bars and 100 bars; 25 | also creates new data and initializes sort. 26 |

27 | 28 | Draw redraws bars. 29 |

30 | 31 | Run starts sorting process 32 | running automatically. (Push Step to pause, Run to resume.) 33 |

34 | 35 | Step executes one step of sorting 36 | process. 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap08/Tree/Tree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Binary Tree 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Binary Tree
Binary Tree 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | Fill creates a new tree 23 | with N nodes. 24 |

25 | 26 | Find searches for a node with 27 | value N. 28 |

29 | 30 | Ins inserts a new node with 31 | value N. 32 |

33 | 34 | Trav traverses the tree 35 | in ascending order. 36 |

37 | 38 | Del deletes the node with value N. 39 |

40 | 41 | (Type N into "Enter number" box.) 42 |

43 |

44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort1/QuickSort1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Quicksort1 4 | 5 | 6 | 7 | 8 | 9 | 12 |
Lafore's Quicksort1
Quicksort1 10 | Operation 11 |
13 | 17 | 18 | 19 | 20 | New creates new data and initializes sort; 21 | toggles between random and inverse order. 22 |

23 | 24 | Size toggles between 10 bars and 100 bars; 25 | also creates new data and initializes sort. 26 |

27 | 28 | Draw redraws bars. 29 |

30 | 31 | Run starts sorting process 32 | running automatically. (Push Step to pause, Run to resume.) 33 |

34 | 35 | Step executes one step of sorting 36 | process. 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/QuickSort2/QuickSort2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Quicksort2 4 | 5 | 6 | 7 | 8 | 9 | 12 |
Lafore's Quicksort2
Quicksort2 10 | Operation 11 |
13 | 17 | 18 | 19 | 20 | New creates new data and initializes sort; 21 | toggles between random and inverse order. 22 |

23 | 24 | Size toggles between 10 bars and 100 bars; 25 | also creates new data and initializes sort. 26 |

27 | 28 | Draw redraws bars. 29 |

30 | 31 | Run starts sorting process 32 | running automatically. (Push Step to pause, Run to resume.) 33 |

34 | 35 | Step executes one step of sorting 36 | process. 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashDouble/HashDouble.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Quad/Double Hash Table 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Quad/Double Hash Table
Hash Table 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | New creates hash table 23 | with N cells (60 max) 24 |

25 | 26 | Fill inserts N items into 27 | table. 28 |

29 | 30 | Ins inserts new item with 31 | value N. 32 |

33 | 34 | Find finds item with 35 | value N. 36 |

37 | 38 | (Type N into "Enter number" box.) 39 |

40 | 41 | Quad/Double selects probe method. 42 | 43 |

44 | 45 | 46 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Insertion/InsertSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Insertion Sort 4 | 5 | 6 | 7 | 8 | 9 | 12 |
Lafore's Insertion Sort
Insertion Sort 10 | Operation 11 |
13 | 17 | 18 | 19 | 20 | New creates new data and initializes sort; 21 | toggles between random and inverse order. 22 |

23 | 24 | Size toggles between 10 bars and 100 bars; 25 | also creates new data and initializes sort. 26 |

27 | 28 | Draw redraws bars. 29 |

30 | 31 | Run starts sorting process 32 | running automatically. (Push Step to pause, Run to resume.) 33 |

34 | 35 | Step executes one step of sorting 36 | process. 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Selection/SelectSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Selection Sort 4 | 5 | 6 | 7 | 8 | 9 | 12 |
Lafore's Selection Sort
Selection Sort 10 | Operation 11 |
13 | 17 | 18 | 19 | 20 | New creates new data and initializes sort; 21 | toggles between random and inverse order. 22 |

23 | 24 | Size toggles between 10 bars and 100 bars; 25 | also creates new data and initializes sort. 26 |

27 | 28 | Draw redraws bars. 29 |

30 | 31 | Run starts sorting process 32 | running automatically. (Push Step to pause, Run to resume.) 33 |

34 | 35 | Step executes one step of sorting 36 | process. 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap03/Bubble/BubbleSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bubble Sort 4 | 5 | 6 | 7 | 8 | 9 | 12 |
Lafore's Bubble Sort
Bubble Sort 10 | Operation 11 |
13 | 17 | 18 | 19 |
20 | 21 | New creates new data and initializes sort; 22 | toggles between random and inverse order. 23 |

24 | 25 | Size toggles between 10 bars and 100 bars; 26 | also creates new data and initializes sort. 27 |

28 | 29 | Draw Redraws bars. 30 |

31 | 32 | Run starts the sorting process 33 | running automatically. (Push Step to pause, Run to resume.) 34 |

35 | 36 | Step executes one step of sorting 37 | process. 38 |

39 |

40 | 41 | 42 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap11/HashChain/HashChain.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Separate-Chaining Hash Table 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Separate-Chaining Hash Table
Hash Table 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | New creates new hash table 23 | containing N linked lists. 24 |

25 | 26 | Fill inserts N items into 27 | table. 28 |

29 | 30 | Ins inserts new item with 31 | value N. 32 |

33 | 34 | Find finds item with 35 | value N. 36 |

37 | 38 | Del deletes item with 39 | value N. 40 |

41 | 42 | (Type number N into text box.) 43 | 44 |

45 | 46 | 47 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap07/Partition/Partition.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Quicksort 4 | 5 | 6 | 7 | 8 | 9 | 12 |
Lafore's Partition
Partition 10 | Operation 11 |
13 | 17 | 18 | 19 | 20 | New creates new data and initializes the 21 | partition process; toggles between random and inverse order. 22 |

23 | 24 | Size toggles between 10 bars and 100 bars; 25 | also creates new data and initializes the partition process. 26 |

27 | 28 | Draw redraws bars. 29 |

30 | 31 | Run starts the partition process 32 | running automatically. (Push Step to pause, Run to resume.) 33 |

34 | 35 | Step executes one step of the partition 36 | process. 37 |

38 |

39 | 40 | 41 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap14/GraphDW/GraphDW.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Directed, Weighted Graph 4 | 5 | 6 | 7 | 8 | 9 | 11 | 14 |
Lafore's Directed, 10 | Weighted Graph
Graph 12 | Operation 13 |
15 | 19 | 20 | 21 |
22 | 23 | Double-click to create new vertex. 24 |

25 | 26 | Type number to specify weight of an edge 27 | (before drag). 28 |

29 | 30 | Drag from vertex to vertex to create 31 | edge. 32 |

33 | 34 | New clears old graph. 35 |

36 | 37 | Path finds all Shortest Paths from a vertex. 38 |

39 | 40 | View toggles between graph and adjacency 41 | matrix.

42 | 43 |

44 | 45 | 46 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap10/Tree234/Tree234.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2-3-4 Tree 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's 2-3-4 Tree
2-3-4 Tree 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | Fill creates a new tree 23 | with N nodes. 24 |

25 | 26 | Ins inserts a new item with 27 | value N. 28 |

29 | 30 | Zoom toggles between close-up of selected 31 | nodes and view of complete tree. 32 |

33 | 34 | Blue triangles indicate a node has 35 | children. 36 |

37 | 38 | Click on a node to see its children, 39 | if they're not already in view. 40 |

41 | 42 | (Type N into "Enter number" box.) 43 | 44 |

45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap13/GraphN/GraphN.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Non-Directed, Non-Weighted Graph 4 | 5 | 6 | 7 | 8 | 9 | 11 | 14 |
Lafore's Non-Directed 10 | Non-Weighted Graph
Graph 12 | Operation 13 |
15 | 19 | 20 | 21 |
22 | 23 | Double-click to create new vertex.

24 | 25 | 26 | Drag from vertex to vertex to create 27 | edge. 28 |

29 | 30 | New clears an old graph. 31 |

32 | 33 | DFS carries out Depth First Search. 34 |

35 | 36 | BFS carries out Breadth First Search. 37 |

38 | 39 | Tree creates minimum spanning tree. 40 |

41 | 42 | View toggles between graph and adjacency 43 | matrix. 44 | 45 |

46 | 47 | 48 | -------------------------------------------------------------------------------- /src/Chap06/triangle/triangle.java: -------------------------------------------------------------------------------- 1 | // triangle.java 2 | // evaluates triangular numbers 3 | // to run this program: C>java TriangleApp 4 | import java.io.*; 5 | //////////////////////////////////////////////////////////////// 6 | class TriangleApp 7 | { 8 | static int theNumber; 9 | 10 | public static void main(String[] args) throws IOException 11 | { 12 | System.out.print("Enter a number: "); 13 | theNumber = getInt(); 14 | int theAnswer = triangle(theNumber); 15 | System.out.println("Triangle="+theAnswer); 16 | } // end main() 17 | //------------------------------------------------------------- 18 | public static int triangle(int n) 19 | { 20 | if(n==1) 21 | return 1; 22 | else 23 | return ( n + triangle(n-1) ); 24 | } 25 | //------------------------------------------------------------- 26 | public static String getString() throws IOException 27 | { 28 | InputStreamReader isr = new InputStreamReader(System.in); 29 | BufferedReader br = new BufferedReader(isr); 30 | String s = br.readLine(); 31 | return s; 32 | } 33 | //------------------------------------------------------------- 34 | public static int getInt() throws IOException 35 | { 36 | String s = getString(); 37 | return Integer.parseInt(s); 38 | } 39 | //-------------------------------------------------------------- 40 | } // end class TriangleApp 41 | //////////////////////////////////////////////////////////////// 42 | -------------------------------------------------------------------------------- /src/Chap01/Bank/bank.java: -------------------------------------------------------------------------------- 1 | // bank.java 2 | // demonstrates basic OOP syntax 3 | // to run this program: C>java BankApp 4 | //////////////////////////////////////////////////////////////// 5 | class BankAccount 6 | { 7 | private double balance; // account balance 8 | 9 | public BankAccount(double openingBalance) // constructor 10 | { 11 | balance = openingBalance; 12 | } 13 | 14 | public void deposit(double amount) // makes deposit 15 | { 16 | balance = balance + amount; 17 | } 18 | 19 | public void withdraw(double amount) // makes withdrawal 20 | { 21 | balance = balance - amount; 22 | } 23 | 24 | public void display() // displays balance 25 | { 26 | System.out.println("balance=" + balance); 27 | } 28 | } // end class BankAccount 29 | //////////////////////////////////////////////////////////////// 30 | class BankApp 31 | { 32 | public static void main(String[] args) 33 | { 34 | BankAccount ba1 = new BankAccount(100.00); // create acct 35 | 36 | System.out.print("Before transactions, "); 37 | ba1.display(); // display balance 38 | 39 | ba1.deposit(74.35); // make deposit 40 | ba1.withdraw(20.00); // make withdrawal 41 | 42 | System.out.print("After transactions, "); 43 | ba1.display(); // display balance 44 | } // end main() 45 | } // end class BankApp 46 | -------------------------------------------------------------------------------- /WorkshopApplets/Chap09/RBTree/RBTree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Red-Black Tree 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 |
Lafore's Red-Black Tree
Red-Black Tree 11 | Operation 12 |
14 | 18 | 19 | 20 |
21 | 22 | Click on node to move arrow to it 23 |

24 | 25 | Start makes a new tree with 26 | one node 27 |

28 | 29 | Ins inserts a new node with 30 | value N 31 |

32 | 33 | Del deletes the node with value N 34 |

35 | 36 | Flip swaps colors between black parent 37 | (arrow) and two red children 38 |

39 | 40 | RoL rotates left around node with arrow 41 |

42 | 43 | RoR rotates right around node with arrow 44 |

45 | 46 | R/B toggles color of node with arrow 47 |

48 | 49 | (Type N into "Number" box.) 50 |

51 |

52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /src/Chap06/merge/merge.java: -------------------------------------------------------------------------------- 1 | // merge.java 2 | // demonstrates merging two arrays into a third 3 | // to run this program: C>java MergeApp 4 | //////////////////////////////////////////////////////////////// 5 | class MergeApp 6 | { 7 | public static void main(String[] args) 8 | { 9 | int[] arrayA = {23, 47, 81, 95}; 10 | int[] arrayB = {7, 14, 39, 55, 62, 74}; 11 | int[] arrayC = new int[10]; 12 | 13 | merge(arrayA, 4, arrayB, 6, arrayC); 14 | display(arrayC, 10); 15 | } // end main() 16 | //----------------------------------------------------------- 17 | // merge A and B into C 18 | public static void merge( int[] arrayA, int sizeA, 19 | int[] arrayB, int sizeB, 20 | int[] arrayC ) 21 | { 22 | int aDex=0, bDex=0, cDex=0; 23 | 24 | while(aDex < sizeA && bDex < sizeB) // neither array empty 25 | if( arrayA[aDex] < arrayB[bDex] ) 26 | arrayC[cDex++] = arrayA[aDex++]; 27 | else 28 | arrayC[cDex++] = arrayB[bDex++]; 29 | 30 | while(aDex < sizeA) // arrayB is empty, 31 | arrayC[cDex++] = arrayA[aDex++]; // but arrayA isn't 32 | 33 | while(bDex < sizeB) // arrayA is empty, 34 | arrayC[cDex++] = arrayB[bDex++]; // but arrayB isn't 35 | } // end merge() 36 | //----------------------------------------------------------- 37 | // display array 38 | public static void display(int[] theArray, int size) 39 | { 40 | for(int j=0; jjava arrayApp 4 | //////////////////////////////////////////////////////////////// 5 | class ArrayApp 6 | { 7 | public static void main(String[] args) 8 | { 9 | long[] arr; // reference to array 10 | arr = new long[100]; // make array 11 | int nElems = 0; // number of items 12 | int j; // loop counter 13 | long searchKey; // key of item to search for 14 | //-------------------------------------------------------------- 15 | arr[0] = 77; // insert 10 items 16 | arr[1] = 99; 17 | arr[2] = 44; 18 | arr[3] = 55; 19 | arr[4] = 22; 20 | arr[5] = 88; 21 | arr[6] = 11; 22 | arr[7] = 00; 23 | arr[8] = 66; 24 | arr[9] = 33; 25 | nElems = 10; // now 10 items in array 26 | //-------------------------------------------------------------- 27 | for(j=0; jjava StackApp 4 | //////////////////////////////////////////////////////////////// 5 | class StackX 6 | { 7 | private int maxSize; // size of stack array 8 | private long[] stackArray; 9 | private int top; // top of stack 10 | //-------------------------------------------------------------- 11 | public StackX(int s) // constructor 12 | { 13 | maxSize = s; // set array size 14 | stackArray = new long[maxSize]; // create array 15 | top = -1; // no items yet 16 | } 17 | //-------------------------------------------------------------- 18 | public void push(long j) // put item on top of stack 19 | { 20 | stackArray[++top] = j; // increment top, insert item 21 | } 22 | //-------------------------------------------------------------- 23 | public long pop() // take item from top of stack 24 | { 25 | return stackArray[top--]; // access item, decrement top 26 | } 27 | //-------------------------------------------------------------- 28 | public long peek() // peek at top of stack 29 | { 30 | return stackArray[top]; 31 | } 32 | //-------------------------------------------------------------- 33 | public boolean isEmpty() // true if stack is empty 34 | { 35 | return (top == -1); 36 | } 37 | //-------------------------------------------------------------- 38 | public boolean isFull() // true if stack is full 39 | { 40 | return (top == maxSize-1); 41 | } 42 | //-------------------------------------------------------------- 43 | } // end class StackX 44 | //////////////////////////////////////////////////////////////// 45 | class StackApp 46 | { 47 | public static void main(String[] args) 48 | { 49 | StackX theStack = new StackX(10); // make new stack 50 | theStack.push(20); // push items onto stack 51 | theStack.push(40); 52 | theStack.push(60); 53 | theStack.push(80); 54 | 55 | while( !theStack.isEmpty() ) // until it's empty, 56 | { // delete item from stack 57 | long value = theStack.pop(); 58 | System.out.print(value); // display it 59 | System.out.print(" "); 60 | } // end while 61 | System.out.println(""); 62 | } // end main() 63 | } // end class StackApp 64 | //////////////////////////////////////////////////////////////// 65 | -------------------------------------------------------------------------------- /src/Chap02/LowArray/lowArray.java: -------------------------------------------------------------------------------- 1 | // lowArray.java 2 | // demonstrates array class with low-level interface 3 | // to run this program: C>java LowArrayApp 4 | //////////////////////////////////////////////////////////////// 5 | class LowArray 6 | { 7 | private long[] a; // ref to array a 8 | //-------------------------------------------------------------- 9 | public LowArray(int size) // constructor 10 | { a = new long[size]; } // create array 11 | //-------------------------------------------------------------- 12 | public void setElem(int index, long value) // set value 13 | { a[index] = value; } 14 | //-------------------------------------------------------------- 15 | public long getElem(int index) // get value 16 | { return a[index]; } 17 | //-------------------------------------------------------------- 18 | } // end class LowArray 19 | //////////////////////////////////////////////////////////////// 20 | class LowArrayApp 21 | { 22 | public static void main(String[] args) 23 | { 24 | LowArray arr; // reference 25 | arr = new LowArray(100); // create LowArray object 26 | int nElems = 0; // number of items in array 27 | int j; // loop variable 28 | 29 | arr.setElem(0, 77); // insert 10 items 30 | arr.setElem(1, 99); 31 | arr.setElem(2, 44); 32 | arr.setElem(3, 55); 33 | arr.setElem(4, 22); 34 | arr.setElem(5, 88); 35 | arr.setElem(6, 11); 36 | arr.setElem(7, 00); 37 | arr.setElem(8, 66); 38 | arr.setElem(9, 33); 39 | nElems = 10; // now 10 items in array 40 | 41 | for(j=0; jjava BubbleSortApp 4 | //////////////////////////////////////////////////////////////// 5 | class ArrayBub 6 | { 7 | private long[] a; // ref to array a 8 | private int nElems; // number of data items 9 | //-------------------------------------------------------------- 10 | public ArrayBub(int max) // constructor 11 | { 12 | a = 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 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | //-------------------------------------------------------------- 22 | public void display() // displays array contents 23 | { 24 | for(int j=0; j1; out--) // outer loop (backward) 34 | for(in=0; in a[in+1] ) // out of order? 36 | swap(in, in+1); // swap them 37 | } // end bubbleSort() 38 | //-------------------------------------------------------------- 39 | private void swap(int one, int two) 40 | { 41 | long temp = a[one]; 42 | a[one] = a[two]; 43 | a[two] = temp; 44 | } 45 | //-------------------------------------------------------------- 46 | } // end class ArrayBub 47 | //////////////////////////////////////////////////////////////// 48 | class BubbleSortApp 49 | { 50 | public static void main(String[] args) 51 | { 52 | int maxSize = 100; // array size 53 | ArrayBub arr; // reference to array 54 | arr = new ArrayBub(maxSize); // create the array 55 | 56 | arr.insert(77); // insert 10 items 57 | arr.insert(99); 58 | arr.insert(44); 59 | arr.insert(55); 60 | arr.insert(22); 61 | arr.insert(88); 62 | arr.insert(11); 63 | arr.insert(00); 64 | arr.insert(66); 65 | arr.insert(33); 66 | 67 | arr.display(); // display items 68 | 69 | arr.bubbleSort(); // bubble sort them 70 | 71 | arr.display(); // display them again 72 | } // end main() 73 | } // end class BubbleSortApp 74 | //////////////////////////////////////////////////////////////// 75 | -------------------------------------------------------------------------------- /src/Chap03/InsertSort/insertSort.java: -------------------------------------------------------------------------------- 1 | // insertSort.java 2 | // demonstrates insertion sort 3 | // to run this program: C>java InsertSortApp 4 | //-------------------------------------------------------------- 5 | class ArrayIns 6 | { 7 | private long[] a; // ref to array a 8 | private int nElems; // number of data items 9 | //-------------------------------------------------------------- 10 | public ArrayIns(int max) // constructor 11 | { 12 | a = 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 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | //-------------------------------------------------------------- 22 | public void display() // displays array contents 23 | { 24 | for(int j=0; j0 && a[in-1] >= temp) // until one is smaller, 38 | { 39 | a[in] = a[in-1]; // shift item to right 40 | --in; // go left one position 41 | } 42 | a[in] = temp; // insert marked item 43 | } // end for 44 | } // end insertionSort() 45 | //-------------------------------------------------------------- 46 | } // end class ArrayIns 47 | //////////////////////////////////////////////////////////////// 48 | class InsertSortApp 49 | { 50 | public static void main(String[] args) 51 | { 52 | int maxSize = 100; // array size 53 | ArrayIns arr; // reference to array 54 | arr = new ArrayIns(maxSize); // create the array 55 | 56 | arr.insert(77); // insert 10 items 57 | arr.insert(99); 58 | arr.insert(44); 59 | arr.insert(55); 60 | arr.insert(22); 61 | arr.insert(88); 62 | arr.insert(11); 63 | arr.insert(00); 64 | arr.insert(66); 65 | arr.insert(33); 66 | 67 | arr.display(); // display items 68 | 69 | arr.insertionSort(); // insertion-sort them 70 | 71 | arr.display(); // display them again 72 | } // end main() 73 | } // end class InsertSortApp 74 | -------------------------------------------------------------------------------- /src/Chap06/anagram/anagram.java: -------------------------------------------------------------------------------- 1 | // anagram.java 2 | // creates anagrams 3 | // to run this program: C>java AnagramApp 4 | import java.io.*; 5 | //////////////////////////////////////////////////////////////// 6 | class AnagramApp 7 | { 8 | static int size; 9 | static int count; 10 | static char[] arrChar = new char[100]; 11 | //----------------------------------------------------------- 12 | public static void main(String[] args) throws IOException 13 | { 14 | System.out.print("Enter a word: "); // get word 15 | String input = getString(); 16 | size = input.length(); // find its size 17 | count = 0; 18 | for(int j=0; jjava PriorityQApp 4 | //////////////////////////////////////////////////////////////// 5 | class PriorityQ 6 | { 7 | // array in sorted order, from max at 0 to min at size-1 8 | private int maxSize; 9 | private long[] queArray; 10 | private int nItems; 11 | //------------------------------------------------------------- 12 | public PriorityQ(int s) // constructor 13 | { 14 | maxSize = s; 15 | queArray = new long[maxSize]; 16 | nItems = 0; 17 | } 18 | //------------------------------------------------------------- 19 | public void insert(long item) // insert item 20 | { 21 | int j; 22 | 23 | if(nItems==0) // if no items, 24 | queArray[nItems++] = item; // insert at 0 25 | else // if items, 26 | { 27 | for(j=nItems-1; j>=0; j--) // start at end, 28 | { 29 | if( item > queArray[j] ) // if new item larger, 30 | queArray[j+1] = queArray[j]; // shift upward 31 | else // if smaller, 32 | break; // done shifting 33 | } // end for 34 | queArray[j+1] = item; // insert it 35 | nItems++; 36 | } // end else (nItems > 0) 37 | } // end insert() 38 | //------------------------------------------------------------- 39 | public long remove() // remove minimum item 40 | { return queArray[--nItems]; } 41 | //------------------------------------------------------------- 42 | public long peekMin() // peek at minimum item 43 | { return queArray[nItems-1]; } 44 | //------------------------------------------------------------- 45 | public boolean isEmpty() // true if queue is empty 46 | { return (nItems==0); } 47 | //------------------------------------------------------------- 48 | public boolean isFull() // true if queue is full 49 | { return (nItems == maxSize); } 50 | //------------------------------------------------------------- 51 | } // end class PriorityQ 52 | //////////////////////////////////////////////////////////////// 53 | class PriorityQApp 54 | { 55 | public static void main(String[] args) 56 | { 57 | PriorityQ thePQ = new PriorityQ(5); 58 | thePQ.insert(30); 59 | thePQ.insert(50); 60 | thePQ.insert(10); 61 | thePQ.insert(40); 62 | thePQ.insert(20); 63 | 64 | while( !thePQ.isEmpty() ) 65 | { 66 | long item = thePQ.remove(); 67 | System.out.print(item + " "); // 10, 20, 30, 40, 50 68 | } // end while 69 | System.out.println(""); 70 | } // end main() 71 | //------------------------------------------------------------- 72 | } // end class PriorityQApp 73 | //////////////////////////////////////////////////////////////// 74 | -------------------------------------------------------------------------------- /src/Chap03/SelectSort/selectSort.java: -------------------------------------------------------------------------------- 1 | // selectSort.java 2 | // demonstrates selection sort 3 | // to run this program: C>java SelectSortApp 4 | //////////////////////////////////////////////////////////////// 5 | class ArraySel 6 | { 7 | private long[] a; // ref to array a 8 | private int nElems; // number of data items 9 | //-------------------------------------------------------------- 10 | public ArraySel(int max) // constructor 11 | { 12 | a = 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 | a[nElems] = value; // insert it 19 | nElems++; // increment size 20 | } 21 | //-------------------------------------------------------------- 22 | public void display() // displays array contents 23 | { 24 | for(int j=0; jjava ShellSortApp 4 | //-------------------------------------------------------------- 5 | class ArraySh 6 | { 7 | private long[] theArray; // ref to array theArray 8 | private int nElems; // number of data items 9 | //-------------------------------------------------------------- 10 | public ArraySh(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; j0) // decreasing h, until h=1 40 | { 41 | // h-sort the file 42 | for(outer=h; outer h-1 && theArray[inner-h] >= temp) 48 | { 49 | theArray[inner] = theArray[inner-h]; 50 | inner -= h; 51 | } 52 | theArray[inner] = temp; 53 | } // end for 54 | h = (h-1) / 3; // decrease h 55 | } // end while(h>0) 56 | } // end shellSort() 57 | //-------------------------------------------------------------- 58 | } // end class ArraySh 59 | //////////////////////////////////////////////////////////////// 60 | class ShellSortApp 61 | { 62 | public static void main(String[] args) 63 | { 64 | int maxSize = 10; // array size 65 | ArraySh arr; 66 | arr = new ArraySh(maxSize); // create the array 67 | 68 | for(int j=0; jjava StackTriangle2App 4 | import java.io.*; // for I/O 5 | //////////////////////////////////////////////////////////////// 6 | class StackX 7 | { 8 | private int maxSize; // size of stack array 9 | private int[] stackArray; 10 | private int top; // top of stack 11 | //-------------------------------------------------------------- 12 | public StackX(int s) // constructor 13 | { 14 | maxSize = s; 15 | stackArray = new int[maxSize]; 16 | top = -1; 17 | } 18 | //-------------------------------------------------------------- 19 | public void push(int p) // put item on top of stack 20 | { stackArray[++top] = p; } 21 | //-------------------------------------------------------------- 22 | public int pop() // take item from top of stack 23 | { return stackArray[top--]; } 24 | //-------------------------------------------------------------- 25 | public int peek() // peek at top of stack 26 | { return stackArray[top]; } 27 | //-------------------------------------------------------------- 28 | public boolean isEmpty() // true if stack is empty 29 | { return (top == -1); } 30 | //-------------------------------------------------------------- 31 | } // end class StackX 32 | //////////////////////////////////////////////////////////////// 33 | class StackTriangle2App 34 | { 35 | static int theNumber; 36 | static int theAnswer; 37 | static StackX theStack; 38 | 39 | public static void main(String[] args) throws IOException 40 | { 41 | System.out.print("Enter a number: "); 42 | System.out.flush(); 43 | theNumber = getInt(); 44 | stackTriangle(); 45 | System.out.println("Triangle="+theAnswer); 46 | } // end main() 47 | //------------------------------------------------------------- 48 | public static void stackTriangle() 49 | { 50 | theStack = new StackX(10000); // make a stack 51 | 52 | theAnswer = 0; // initialize answer 53 | 54 | while(theNumber > 0) // until n is 1, 55 | { 56 | theStack.push(theNumber); // push value 57 | --theNumber; // decrement value 58 | } 59 | while( !theStack.isEmpty() ) // until stack empty, 60 | { 61 | int newN = theStack.pop(); // pop value, 62 | theAnswer += newN; // add to answer 63 | } 64 | } 65 | //------------------------------------------------------------- 66 | public static String getString() throws IOException 67 | { 68 | InputStreamReader isr = new InputStreamReader(System.in); 69 | BufferedReader br = new BufferedReader(isr); 70 | String s = br.readLine(); 71 | return s; 72 | } 73 | //------------------------------------------------------------- 74 | public static int getInt() throws IOException 75 | { 76 | String s = getString(); 77 | return Integer.parseInt(s); 78 | } 79 | //-------------------------------------------------------------- 80 | } // end class StackTriangle2App 81 | //////////////////////////////////////////////////////////////// 82 | -------------------------------------------------------------------------------- /src/Chap04/Queue/queue.java: -------------------------------------------------------------------------------- 1 | // Queue.java 2 | // demonstrates queue 3 | // to run this program: C>java QueueApp 4 | //////////////////////////////////////////////////////////////// 5 | class Queue 6 | { 7 | private int maxSize; 8 | private long[] queArray; 9 | private int front; 10 | private int rear; 11 | private int nItems; 12 | //-------------------------------------------------------------- 13 | public Queue(int s) // constructor 14 | { 15 | maxSize = s; 16 | queArray = new long[maxSize]; 17 | front = 0; 18 | rear = -1; 19 | nItems = 0; 20 | } 21 | //-------------------------------------------------------------- 22 | public void insert(long j) // put item at rear of queue 23 | { 24 | if(rear == maxSize-1) // deal with wraparound 25 | rear = -1; 26 | queArray[++rear] = j; // increment rear and insert 27 | nItems++; // one more item 28 | } 29 | //-------------------------------------------------------------- 30 | public long remove() // take item from front of queue 31 | { 32 | long temp = queArray[front++]; // get value and incr front 33 | if(front == maxSize) // deal with wraparound 34 | front = 0; 35 | nItems--; // one less item 36 | return temp; 37 | } 38 | //-------------------------------------------------------------- 39 | public long peekFront() // peek at front of queue 40 | { 41 | return queArray[front]; 42 | } 43 | //-------------------------------------------------------------- 44 | public boolean isEmpty() // true if queue is empty 45 | { 46 | return (nItems==0); 47 | } 48 | //-------------------------------------------------------------- 49 | public boolean isFull() // true if queue is full 50 | { 51 | return (nItems==maxSize); 52 | } 53 | //-------------------------------------------------------------- 54 | public int size() // number of items in queue 55 | { 56 | return nItems; 57 | } 58 | //-------------------------------------------------------------- 59 | } // end class Queue 60 | //////////////////////////////////////////////////////////////// 61 | class QueueApp 62 | { 63 | public static void main(String[] args) 64 | { 65 | Queue theQueue = new Queue(5); // queue holds 5 items 66 | 67 | theQueue.insert(10); // insert 4 items 68 | theQueue.insert(20); 69 | theQueue.insert(30); 70 | theQueue.insert(40); 71 | 72 | theQueue.remove(); // remove 3 items 73 | theQueue.remove(); // (10, 20, 30) 74 | theQueue.remove(); 75 | 76 | theQueue.insert(50); // insert 4 more items 77 | theQueue.insert(60); // (wraps around) 78 | theQueue.insert(70); 79 | theQueue.insert(80); 80 | 81 | while( !theQueue.isEmpty() ) // remove and display 82 | { // all items 83 | long n = theQueue.remove(); // (40, 50, 60, 70, 80) 84 | System.out.print(n); 85 | System.out.print(" "); 86 | } 87 | System.out.println(""); 88 | } // end main() 89 | } // end class QueueApp 90 | //////////////////////////////////////////////////////////////// 91 | -------------------------------------------------------------------------------- /src/Chap02/HighArray/highArray.java: -------------------------------------------------------------------------------- 1 | // highArray.java 2 | // demonstrates array class with high-level interface 3 | // to run this program: C>java HighArrayApp 4 | //////////////////////////////////////////////////////////////// 5 | class HighArray 6 | { 7 | private long[] a; // ref to array a 8 | private int nElems; // number of data items 9 | //----------------------------------------------------------- 10 | public HighArray(int max) // constructor 11 | { 12 | a = new long[max]; // create the array 13 | nElems = 0; // no items yet 14 | } 15 | //----------------------------------------------------------- 16 | public boolean find(long searchKey) 17 | { // find specified value 18 | int j; 19 | for(j=0; jjava LinkListApp 4 | //////////////////////////////////////////////////////////////// 5 | class Link 6 | { 7 | public int iData; // data item 8 | public double dData; // data item 9 | public Link next; // next link in list 10 | // ------------------------------------------------------------- 11 | public Link(int id, double dd) // constructor 12 | { 13 | iData = id; // initialize data 14 | dData = dd; // ('next' is automatically 15 | } // set to null) 16 | // ------------------------------------------------------------- 17 | public void displayLink() // display ourself 18 | { 19 | System.out.print("{" + iData + ", " + dData + "} "); 20 | } 21 | } // end class Link 22 | //////////////////////////////////////////////////////////////// 23 | class LinkList 24 | { 25 | private Link first; // ref to first link on list 26 | 27 | // ------------------------------------------------------------- 28 | public LinkList() // constructor 29 | { 30 | first = null; // no links on list yet 31 | } 32 | // ------------------------------------------------------------- 33 | public boolean isEmpty() // true if list is empty 34 | { 35 | return (first==null); 36 | } 37 | // ------------------------------------------------------------- 38 | // insert at start of list 39 | public void insertFirst(int id, double dd) 40 | { // make new link 41 | Link newLink = new Link(id, dd); 42 | newLink.next = first; // newLink --> old first 43 | first = newLink; // first --> newLink 44 | } 45 | // ------------------------------------------------------------- 46 | public Link deleteFirst() // delete first item 47 | { // (assumes list not empty) 48 | Link temp = first; // save reference to link 49 | first = first.next; // delete it: first-->old next 50 | return temp; // return deleted link 51 | } 52 | // ------------------------------------------------------------- 53 | public void displayList() 54 | { 55 | System.out.print("List (first-->last): "); 56 | Link current = first; // start at beginning of list 57 | while(current != null) // until end of list, 58 | { 59 | current.displayLink(); // print data 60 | current = current.next; // move to next link 61 | } 62 | System.out.println(""); 63 | } 64 | // ------------------------------------------------------------- 65 | } // end class LinkList 66 | //////////////////////////////////////////////////////////////// 67 | class LinkListApp 68 | { 69 | public static void main(String[] args) 70 | { 71 | LinkList theList = new LinkList(); // make new list 72 | 73 | theList.insertFirst(22, 2.99); // insert four items 74 | theList.insertFirst(44, 4.99); 75 | theList.insertFirst(66, 6.99); 76 | theList.insertFirst(88, 8.99); 77 | 78 | theList.displayList(); // display list 79 | 80 | while( !theList.isEmpty() ) // until it's empty, 81 | { 82 | Link aLink = theList.deleteFirst(); // delete link 83 | System.out.print("Deleted "); // display it 84 | aLink.displayLink(); 85 | System.out.println(""); 86 | } 87 | theList.displayList(); // display list 88 | } // end main() 89 | } // end class LinkListApp 90 | //////////////////////////////////////////////////////////////// 91 | -------------------------------------------------------------------------------- /src/Chap05/sortedList/sortedList.java: -------------------------------------------------------------------------------- 1 | // sortedList.java 2 | // demonstrates sorted list 3 | // to run this program: C>java SortedListApp 4 | //////////////////////////////////////////////////////////////// 5 | class Link 6 | { 7 | public long dData; // data item 8 | public Link next; // next link in list 9 | // ------------------------------------------------------------- 10 | public Link(long dd) // constructor 11 | { dData = dd; } 12 | // ------------------------------------------------------------- 13 | public void displayLink() // display this link 14 | { System.out.print(dData + " "); } 15 | } // end class Link 16 | //////////////////////////////////////////////////////////////// 17 | class SortedList 18 | { 19 | private Link first; // ref to first item 20 | // ------------------------------------------------------------- 21 | public SortedList() // constructor 22 | { first = null; } 23 | // ------------------------------------------------------------- 24 | public boolean isEmpty() // true if no links 25 | { return (first==null); } 26 | // ------------------------------------------------------------- 27 | public void insert(long key) // insert, in order 28 | { 29 | Link newLink = new Link(key); // make new link 30 | Link previous = null; // start at first 31 | Link current = first; 32 | // until end of list, 33 | while(current != null && key > current.dData) 34 | { // or key > current, 35 | previous = current; 36 | current = current.next; // go to next item 37 | } 38 | if(previous==null) // at beginning of list 39 | first = newLink; // first --> newLink 40 | else // not at beginning 41 | previous.next = newLink; // old prev --> newLink 42 | newLink.next = current; // newLink --> old currnt 43 | } // end insert() 44 | // ------------------------------------------------------------- 45 | public Link remove() // return & delete first link 46 | { // (assumes non-empty list) 47 | Link temp = first; // save first 48 | first = first.next; // delete first 49 | return temp; // return value 50 | } 51 | // ------------------------------------------------------------- 52 | public void displayList() 53 | { 54 | System.out.print("List (first-->last): "); 55 | Link current = first; // start at beginning of list 56 | while(current != null) // until end of list, 57 | { 58 | current.displayLink(); // print data 59 | current = current.next; // move to next link 60 | } 61 | System.out.println(""); 62 | } 63 | } // end class SortedList 64 | //////////////////////////////////////////////////////////////// 65 | class SortedListApp 66 | { 67 | public static void main(String[] args) 68 | { // create new list 69 | SortedList theSortedList = new SortedList(); 70 | theSortedList.insert(20); // insert 2 items 71 | theSortedList.insert(40); 72 | 73 | theSortedList.displayList(); // display list 74 | 75 | theSortedList.insert(10); // insert 3 more items 76 | theSortedList.insert(30); 77 | theSortedList.insert(50); 78 | 79 | theSortedList.displayList(); // display list 80 | 81 | theSortedList.remove(); // remove an item 82 | 83 | theSortedList.displayList(); // display list 84 | } // end main() 85 | } // end class SortedListApp 86 | //////////////////////////////////////////////////////////////// 87 | -------------------------------------------------------------------------------- /src/Chap04/Reverse/reverse.java: -------------------------------------------------------------------------------- 1 | // reverse.java 2 | // stack used to reverse a string 3 | // to run this program: C>java ReverseApp 4 | import java.io.*; // for I/O 5 | //////////////////////////////////////////////////////////////// 6 | class StackX 7 | { 8 | private int maxSize; 9 | private char[] stackArray; 10 | private int top; 11 | //-------------------------------------------------------------- 12 | public StackX(int max) // constructor 13 | { 14 | maxSize = max; 15 | stackArray = new char[maxSize]; 16 | top = -1; 17 | } 18 | //-------------------------------------------------------------- 19 | public void push(char j) // put item on top of stack 20 | { 21 | stackArray[++top] = j; 22 | } 23 | //-------------------------------------------------------------- 24 | public char pop() // take item from top of stack 25 | { 26 | return stackArray[top--]; 27 | } 28 | //-------------------------------------------------------------- 29 | public char peek() // peek at top of stack 30 | { 31 | return stackArray[top]; 32 | } 33 | //-------------------------------------------------------------- 34 | public boolean isEmpty() // true if stack is empty 35 | { 36 | return (top == -1); 37 | } 38 | //-------------------------------------------------------------- 39 | } // end class StackX 40 | //////////////////////////////////////////////////////////////// 41 | class Reverser 42 | { 43 | private String input; // input string 44 | private String output; // output string 45 | //-------------------------------------------------------------- 46 | public Reverser(String in) // constructor 47 | { input = in; } 48 | //-------------------------------------------------------------- 49 | public String doRev() // reverse the string 50 | { 51 | int stackSize = input.length(); // get max stack size 52 | StackX theStack = new StackX(stackSize); // make stack 53 | 54 | for(int j=0; jjava BinarySearchApp 4 | //////////////////////////////////////////////////////////////// 5 | class ordArray 6 | { 7 | private long[] a; // ref to array a 8 | private int nElems; // number of data items 9 | //----------------------------------------------------------- 10 | public ordArray(int max) // constructor 11 | { 12 | a = new long[max]; // create array 13 | nElems = 0; 14 | } 15 | //----------------------------------------------------------- 16 | public int size() 17 | { return nElems; } 18 | //----------------------------------------------------------- 19 | public int find(long searchKey) 20 | { 21 | return recFind(searchKey, 0, nElems-1); 22 | } 23 | //----------------------------------------------------------- 24 | private int recFind(long searchKey, int lowerBound, 25 | int upperBound) 26 | { 27 | int curIn; 28 | 29 | curIn = (lowerBound + upperBound ) / 2; 30 | if(a[curIn]==searchKey) 31 | return curIn; // found it 32 | else if(lowerBound > upperBound) 33 | return nElems; // can't find it 34 | else // divide range 35 | { 36 | if(a[curIn] < searchKey) // it's in upper half 37 | return recFind(searchKey, curIn+1, upperBound); 38 | else // it's in lower half 39 | return recFind(searchKey, lowerBound, curIn-1); 40 | } // end else divide range 41 | } // end recFind() 42 | //----------------------------------------------------------- 43 | public void insert(long value) // put element into array 44 | { 45 | int j; 46 | for(j=0; j value) // (linear search) 48 | break; 49 | for(int k=nElems; k>j; k--) // move bigger ones up 50 | a[k] = a[k-1]; 51 | a[j] = value; // insert it 52 | nElems++; // increment size 53 | } // end insert() 54 | //----------------------------------------------------------- 55 | public void display() // displays array contents 56 | { 57 | for(int j=0; jjava PartitionApp 4 | //////////////////////////////////////////////////////////////// 5 | class ArrayPar 6 | { 7 | private long[] theArray; // ref to array theArray 8 | private int nElems; // number of data items 9 | //-------------------------------------------------------------- 10 | public ArrayPar(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 int size() // return number of items 23 | { return nElems; } 24 | //-------------------------------------------------------------- 25 | public void display() // displays array contents 26 | { 27 | System.out.print("A="); 28 | 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 | } // end while(true) 51 | return leftPtr; // return partition 52 | } // end partitionIt() 53 | //-------------------------------------------------------------- 54 | public void swap(int dex1, int dex2) // swap two elements 55 | { 56 | long temp; 57 | temp = theArray[dex1]; // A into temp 58 | theArray[dex1] = theArray[dex2]; // B into A 59 | theArray[dex2] = temp; // temp into B 60 | } // end swap() 61 | //-------------------------------------------------------------- 62 | } // end class ArrayPar 63 | //////////////////////////////////////////////////////////////// 64 | class PartitionApp 65 | { 66 | public static void main(String[] args) 67 | { 68 | int maxSize = 16; // array size 69 | ArrayPar arr; // reference to array 70 | arr = new ArrayPar(maxSize); // create the array 71 | 72 | for(int j=0; jjava ListInsertionSortApp 4 | //////////////////////////////////////////////////////////////// 5 | class Link 6 | { 7 | public long dData; // data item 8 | public Link next; // next link in list 9 | // ------------------------------------------------------------- 10 | public Link(long dd) // constructor 11 | { dData = dd; } 12 | // ------------------------------------------------------------- 13 | } // end class Link 14 | //////////////////////////////////////////////////////////////// 15 | class SortedList 16 | { 17 | private Link first; // ref to first item on list 18 | // ------------------------------------------------------------- 19 | public SortedList() // constructor (no args) 20 | { first = null; } // initialize list 21 | // ------------------------------------------------------------- 22 | public SortedList(Link[] linkArr) // constructor (array 23 | { // as argument) 24 | first = null; // initialize list 25 | for(int j=0; j current.dData) 35 | { // or key > current, 36 | previous = current; 37 | current = current.next; // go to next item 38 | } 39 | if(previous==null) // at beginning of list 40 | first = k; // first --> k 41 | else // not at beginning 42 | previous.next = k; // old prev --> k 43 | k.next = current; // k --> old currnt 44 | } // end insert() 45 | // ------------------------------------------------------------- 46 | public Link remove() // return & delete first link 47 | { // (assumes non-empty list) 48 | Link temp = first; // save first 49 | first = first.next; // delete first 50 | return temp; // return value 51 | } 52 | // ------------------------------------------------------------- 53 | } // end class SortedList 54 | //////////////////////////////////////////////////////////////// 55 | class ListInsertionSortApp 56 | { 57 | public static void main(String[] args) 58 | { 59 | int size = 10; 60 | // create array of links 61 | Link[] linkArray = new Link[size]; 62 | 63 | for(int j=0; jjava FirstLastApp 4 | //////////////////////////////////////////////////////////////// 5 | class Link 6 | { 7 | public long dData; // data item 8 | public Link next; // next link in list 9 | // ------------------------------------------------------------- 10 | public Link(long d) // constructor 11 | { dData = d; } 12 | // ------------------------------------------------------------- 13 | public void displayLink() // display this link 14 | { System.out.print(dData + " "); } 15 | // ------------------------------------------------------------- 16 | } // end class Link 17 | //////////////////////////////////////////////////////////////// 18 | class FirstLastList 19 | { 20 | private Link first; // ref to first link 21 | private Link last; // ref to last link 22 | // ------------------------------------------------------------- 23 | public FirstLastList() // constructor 24 | { 25 | first = null; // no links on list yet 26 | last = null; 27 | } 28 | // ------------------------------------------------------------- 29 | public boolean isEmpty() // true if no links 30 | { return first==null; } 31 | // ------------------------------------------------------------- 32 | public void insertFirst(long dd) // insert at front of list 33 | { 34 | Link newLink = new Link(dd); // make new link 35 | 36 | if( isEmpty() ) // if empty list, 37 | last = newLink; // newLink <-- last 38 | newLink.next = first; // newLink --> old first 39 | first = newLink; // first --> newLink 40 | } 41 | // ------------------------------------------------------------- 42 | public void insertLast(long dd) // insert at end of list 43 | { 44 | Link newLink = new Link(dd); // make new link 45 | if( isEmpty() ) // if empty list, 46 | first = newLink; // first --> newLink 47 | else 48 | last.next = newLink; // old last --> newLink 49 | last = newLink; // newLink <-- last 50 | } 51 | // ------------------------------------------------------------- 52 | public long deleteFirst() // delete first link 53 | { // (assumes non-empty list) 54 | long temp = first.dData; 55 | if(first.next == null) // if only one item 56 | last = null; // null <-- last 57 | first = first.next; // first --> old next 58 | return temp; 59 | } 60 | // ------------------------------------------------------------- 61 | public void displayList() 62 | { 63 | System.out.print("List (first-->last): "); 64 | Link current = first; // start at beginning 65 | while(current != null) // until end of list, 66 | { 67 | current.displayLink(); // print data 68 | current = current.next; // move to next link 69 | } 70 | System.out.println(""); 71 | } 72 | // ------------------------------------------------------------- 73 | } // end class FirstLastList 74 | //////////////////////////////////////////////////////////////// 75 | class FirstLastApp 76 | { 77 | public static void main(String[] args) 78 | { // make a new list 79 | FirstLastList theList = new FirstLastList(); 80 | 81 | theList.insertFirst(22); // insert at front 82 | theList.insertFirst(44); 83 | theList.insertFirst(66); 84 | 85 | theList.insertLast(11); // insert at rear 86 | theList.insertLast(33); 87 | theList.insertLast(55); 88 | 89 | theList.displayList(); // display the list 90 | 91 | theList.deleteFirst(); // delete first two items 92 | theList.deleteFirst(); 93 | 94 | theList.displayList(); // display again 95 | } // end main() 96 | } // end class FirstLastApp 97 | //////////////////////////////////////////////////////////////// 98 | -------------------------------------------------------------------------------- /src/Chap02/OrderedArray/orderedArray.java: -------------------------------------------------------------------------------- 1 | // orderedArray.java 2 | // demonstrates ordered array class 3 | // to run this program: C>java OrderedApp 4 | //////////////////////////////////////////////////////////////// 5 | class OrdArray 6 | { 7 | private long[] a; // ref to array a 8 | private int nElems; // number of data items 9 | //----------------------------------------------------------- 10 | public OrdArray(int max) // constructor 11 | { 12 | a = new long[max]; // create array 13 | nElems = 0; 14 | } 15 | //----------------------------------------------------------- 16 | public int size() 17 | { return nElems; } 18 | //----------------------------------------------------------- 19 | public int find(long searchKey) 20 | { 21 | int lowerBound = 0; 22 | int upperBound = nElems-1; 23 | int curIn; 24 | 25 | while(true) 26 | { 27 | curIn = (lowerBound + upperBound ) / 2; 28 | if(a[curIn]==searchKey) 29 | return curIn; // found it 30 | else if(lowerBound > upperBound) 31 | return nElems; // can't find it 32 | else // divide range 33 | { 34 | if(a[curIn] < searchKey) 35 | lowerBound = curIn + 1; // it's in upper half 36 | else 37 | upperBound = curIn - 1; // it's in lower half 38 | } // end else divide range 39 | } // end while 40 | } // end find() 41 | //----------------------------------------------------------- 42 | public void insert(long value) // put element into array 43 | { 44 | int j; 45 | for(j=0; j value) // (linear search) 47 | break; 48 | for(int k=nElems; k>j; k--) // move bigger ones up 49 | a[k] = a[k-1]; 50 | a[j] = value; // insert it 51 | nElems++; // increment size 52 | } // end insert() 53 | //----------------------------------------------------------- 54 | public boolean delete(long value) 55 | { 56 | int j = find(value); 57 | if(j==nElems) // can't find it 58 | return false; 59 | else // found it 60 | { 61 | for(int k=j; kjava ObjectSortApp 4 | //////////////////////////////////////////////////////////////// 5 | class Person 6 | { 7 | private String lastName; 8 | private String firstName; 9 | private int age; 10 | //----------------------------------------------------------- 11 | public Person(String last, String first, int a) 12 | { // constructor 13 | lastName = last; 14 | firstName = first; 15 | age = a; 16 | } 17 | //----------------------------------------------------------- 18 | public void displayPerson() 19 | { 20 | System.out.print(" Last name: " + lastName); 21 | System.out.print(", First name: " + firstName); 22 | System.out.println(", Age: " + age); 23 | } 24 | //----------------------------------------------------------- 25 | public String getLast() // get last name 26 | { return lastName; } 27 | } // end class Person 28 | //////////////////////////////////////////////////////////////// 29 | class ArrayInOb 30 | { 31 | private Person[] a; // ref to array a 32 | private int nElems; // number of data items 33 | //-------------------------------------------------------------- 34 | public ArrayInOb(int max) // constructor 35 | { 36 | a = new Person[max]; // create the array 37 | nElems = 0; // no items yet 38 | } 39 | //-------------------------------------------------------------- 40 | // put person into array 41 | public void insert(String last, String first, int age) 42 | { 43 | a[nElems] = new Person(last, first, age); 44 | nElems++; // increment size 45 | } 46 | //-------------------------------------------------------------- 47 | public void display() // displays array contents 48 | { 49 | for(int j=0; j0 && // until smaller one found, 63 | a[in-1].getLast().compareTo(temp.getLast())>0) 64 | { 65 | a[in] = a[in-1]; // shift item to the right 66 | --in; // go left one position 67 | } 68 | a[in] = temp; // insert marked item 69 | } // end for 70 | } // end insertionSort() 71 | //-------------------------------------------------------------- 72 | } // end class ArrayInOb 73 | //////////////////////////////////////////////////////////////// 74 | class ObjectSortApp 75 | { 76 | public static void main(String[] args) 77 | { 78 | int maxSize = 100; // array size 79 | ArrayInOb arr; // reference to array 80 | arr = new ArrayInOb(maxSize); // create the array 81 | 82 | arr.insert("Evans", "Patty", 24); 83 | arr.insert("Smith", "Doc", 59); 84 | arr.insert("Smith", "Lorraine", 37); 85 | arr.insert("Smith", "Paul", 37); 86 | arr.insert("Yee", "Tom", 43); 87 | arr.insert("Hashimoto", "Sato", 21); 88 | arr.insert("Stimson", "Henry", 29); 89 | arr.insert("Velasquez", "Jose", 72); 90 | arr.insert("Vang", "Minh", 22); 91 | arr.insert("Creswell", "Lucinda", 18); 92 | 93 | System.out.println("Before sorting:"); 94 | arr.display(); // display items 95 | 96 | arr.insertionSort(); // insertion-sort them 97 | 98 | System.out.println("After sorting:"); 99 | arr.display(); // display them again 100 | } // end main() 101 | } // end class ObjectSortApp 102 | //////////////////////////////////////////////////////////////// 103 | -------------------------------------------------------------------------------- /src/Chap07/quickSort1/quickSort1.java: -------------------------------------------------------------------------------- 1 | // quickSort1.java 2 | // demonstrates simple version of quick sort 3 | // to run this program: C>java QuickSort1App 4 | //////////////////////////////////////////////////////////////// 5 | class ArrayIns 6 | { 7 | private long[] theArray; // ref to array theArray 8 | private int nElems; // number of data items 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 0 && theArray[--rightPtr] > pivot) 59 | ; // (nop) 60 | 61 | if(leftPtr >= rightPtr) // if pointers cross, 62 | break; // partition done 63 | else // not crossed, so 64 | swap(leftPtr, rightPtr); // swap elements 65 | } // end while(true) 66 | swap(leftPtr, right); // restore pivot 67 | return leftPtr; // return pivot location 68 | } // end partitionIt() 69 | //-------------------------------------------------------------- 70 | public void swap(int dex1, int dex2) // swap two elements 71 | { 72 | long temp = theArray[dex1]; // A into temp 73 | theArray[dex1] = theArray[dex2]; // B into A 74 | theArray[dex2] = temp; // temp into B 75 | } // end swap( 76 | //-------------------------------------------------------------- 77 | } // end class ArrayIns 78 | //////////////////////////////////////////////////////////////// 79 | class QuickSort1App 80 | { 81 | public static void main(String[] args) 82 | { 83 | int maxSize = 16; // array size 84 | ArrayIns arr; 85 | arr = new ArrayIns(maxSize); // create array 86 | 87 | for(int j=0; jjava MergeSortApp 4 | //////////////////////////////////////////////////////////////// 5 | class DArray 6 | { 7 | private long[] theArray; // ref to array theArray 8 | private int nElems; // number of data items 9 | //----------------------------------------------------------- 10 | public DArray(int max) // constructor 11 | { 12 | theArray = new long[max]; // create array 13 | nElems = 0; 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 | for(int j=0; jjava LinkStackApp 4 | //////////////////////////////////////////////////////////////// 5 | class Link 6 | { 7 | public long dData; // data item 8 | public Link next; // next link in list 9 | // ------------------------------------------------------------- 10 | public Link(long dd) // constructor 11 | { dData = dd; } 12 | // ------------------------------------------------------------- 13 | public void displayLink() // display ourself 14 | { System.out.print(dData + " "); } 15 | } // end class Link 16 | //////////////////////////////////////////////////////////////// 17 | class LinkList 18 | { 19 | private Link first; // ref to first item on list 20 | // ------------------------------------------------------------- 21 | public LinkList() // constructor 22 | { first = null; } // no items on list yet 23 | // ------------------------------------------------------------- 24 | public boolean isEmpty() // true if list is empty 25 | { return (first==null); } 26 | // ------------------------------------------------------------- 27 | public void insertFirst(long dd) // insert at start of list 28 | { // make new link 29 | Link newLink = new Link(dd); 30 | newLink.next = first; // newLink --> old first 31 | first = newLink; // first --> newLink 32 | } 33 | // ------------------------------------------------------------- 34 | public long deleteFirst() // delete first item 35 | { // (assumes list not empty) 36 | Link temp = first; // save reference to link 37 | first = first.next; // delete it: first-->old next 38 | return temp.dData; // return deleted link 39 | } 40 | // ------------------------------------------------------------- 41 | public void displayList() 42 | { 43 | Link current = first; // start at beginning of list 44 | while(current != null) // until end of list, 45 | { 46 | current.displayLink(); // print data 47 | current = current.next; // move to next link 48 | } 49 | System.out.println(""); 50 | } 51 | // ------------------------------------------------------------- 52 | } // end class LinkList 53 | //////////////////////////////////////////////////////////////// 54 | class LinkStack 55 | { 56 | private LinkList theList; 57 | //-------------------------------------------------------------- 58 | public LinkStack() // constructor 59 | { 60 | theList = new LinkList(); 61 | } 62 | //-------------------------------------------------------------- 63 | public void push(long j) // put item on top of stack 64 | { 65 | theList.insertFirst(j); 66 | } 67 | //-------------------------------------------------------------- 68 | public long pop() // take item from top of stack 69 | { 70 | return theList.deleteFirst(); 71 | } 72 | //-------------------------------------------------------------- 73 | public boolean isEmpty() // true if stack is empty 74 | { 75 | return ( theList.isEmpty() ); 76 | } 77 | //-------------------------------------------------------------- 78 | public void displayStack() 79 | { 80 | System.out.print("Stack (top-->bottom): "); 81 | theList.displayList(); 82 | } 83 | //-------------------------------------------------------------- 84 | } // end class LinkStack 85 | //////////////////////////////////////////////////////////////// 86 | class LinkStackApp 87 | { 88 | public static void main(String[] args) 89 | { 90 | LinkStack theStack = new LinkStack(); // make stack 91 | 92 | theStack.push(20); // push items 93 | theStack.push(40); 94 | 95 | theStack.displayStack(); // display stack 96 | 97 | theStack.push(60); // push items 98 | theStack.push(80); 99 | 100 | theStack.displayStack(); // display stack 101 | 102 | theStack.pop(); // pop items 103 | theStack.pop(); 104 | 105 | theStack.displayStack(); // display stack 106 | } // end main() 107 | } // end class LinkStackApp 108 | //////////////////////////////////////////////////////////////// 109 | -------------------------------------------------------------------------------- /src/Chap04/Brackets/brackets.java: -------------------------------------------------------------------------------- 1 | // brackets.java 2 | // stacks used to check matching brackets 3 | // to run this program: C>java bracketsApp 4 | import java.io.*; // for I/O 5 | //////////////////////////////////////////////////////////////// 6 | class StackX 7 | { 8 | private int maxSize; 9 | private char[] stackArray; 10 | private int top; 11 | //-------------------------------------------------------------- 12 | public StackX(int s) // constructor 13 | { 14 | maxSize = s; 15 | stackArray = new char[maxSize]; 16 | top = -1; 17 | } 18 | //-------------------------------------------------------------- 19 | public void push(char j) // put item on top of stack 20 | { 21 | stackArray[++top] = j; 22 | } 23 | //-------------------------------------------------------------- 24 | public char pop() // take item from top of stack 25 | { 26 | return stackArray[top--]; 27 | } 28 | //-------------------------------------------------------------- 29 | public char peek() // peek at top of stack 30 | { 31 | return stackArray[top]; 32 | } 33 | //-------------------------------------------------------------- 34 | public boolean isEmpty() // true if stack is empty 35 | { 36 | return (top == -1); 37 | } 38 | //-------------------------------------------------------------- 39 | } // end class StackX 40 | //////////////////////////////////////////////////////////////// 41 | class BracketChecker 42 | { 43 | private String input; // input string 44 | //-------------------------------------------------------------- 45 | public BracketChecker(String in) // constructor 46 | { input = in; } 47 | //-------------------------------------------------------------- 48 | public void check() 49 | { 50 | int stackSize = input.length(); // get max stack size 51 | StackX theStack = new StackX(stackSize); // make stack 52 | 53 | for(int j=0; jjava LinkQueueApp 4 | //////////////////////////////////////////////////////////////// 5 | class Link 6 | { 7 | public long dData; // data item 8 | public Link next; // next link in list 9 | // ------------------------------------------------------------- 10 | public Link(long d) // constructor 11 | { dData = d; } 12 | // ------------------------------------------------------------- 13 | public void displayLink() // display this link 14 | { System.out.print(dData + " "); } 15 | // ------------------------------------------------------------- 16 | } // end class Link 17 | //////////////////////////////////////////////////////////////// 18 | class FirstLastList 19 | { 20 | private Link first; // ref to first item 21 | private Link last; // ref to last item 22 | // ------------------------------------------------------------- 23 | public FirstLastList() // constructor 24 | { 25 | first = null; // no items on list yet 26 | last = null; 27 | } 28 | // ------------------------------------------------------------- 29 | public boolean isEmpty() // true if no links 30 | { return first==null; } 31 | // ------------------------------------------------------------- 32 | public void insertLast(long dd) // insert at end of list 33 | { 34 | Link newLink = new Link(dd); // make new link 35 | if( isEmpty() ) // if empty list, 36 | first = newLink; // first --> newLink 37 | else 38 | last.next = newLink; // old last --> newLink 39 | last = newLink; // newLink <-- last 40 | } 41 | // ------------------------------------------------------------- 42 | public long deleteFirst() // delete first link 43 | { // (assumes non-empty list) 44 | long temp = first.dData; 45 | if(first.next == null) // if only one item 46 | last = null; // null <-- last 47 | first = first.next; // first --> old next 48 | return temp; 49 | } 50 | // ------------------------------------------------------------- 51 | public void displayList() 52 | { 53 | Link current = first; // start at beginning 54 | while(current != null) // until end of list, 55 | { 56 | current.displayLink(); // print data 57 | current = current.next; // move to next link 58 | } 59 | System.out.println(""); 60 | } 61 | // ------------------------------------------------------------- 62 | } // end class FirstLastList 63 | //////////////////////////////////////////////////////////////// 64 | class LinkQueue 65 | { 66 | private FirstLastList theList; 67 | //-------------------------------------------------------------- 68 | public LinkQueue() // constructor 69 | { theList = new FirstLastList(); } // make a 2-ended list 70 | //-------------------------------------------------------------- 71 | public boolean isEmpty() // true if queue is empty 72 | { return theList.isEmpty(); } 73 | //-------------------------------------------------------------- 74 | public void insert(long j) // insert, rear of queue 75 | { theList.insertLast(j); } 76 | //-------------------------------------------------------------- 77 | public long remove() // remove, front of queue 78 | { return theList.deleteFirst(); } 79 | //-------------------------------------------------------------- 80 | public void displayQueue() 81 | { 82 | System.out.print("Queue (front-->rear): "); 83 | theList.displayList(); 84 | } 85 | //-------------------------------------------------------------- 86 | } // end class LinkQueue 87 | //////////////////////////////////////////////////////////////// 88 | class LinkQueueApp 89 | { 90 | public static void main(String[] args) 91 | { 92 | LinkQueue theQueue = new LinkQueue(); 93 | theQueue.insert(20); // insert items 94 | theQueue.insert(40); 95 | 96 | theQueue.displayQueue(); // display queue 97 | 98 | theQueue.insert(60); // insert items 99 | theQueue.insert(80); 100 | 101 | theQueue.displayQueue(); // display queue 102 | 103 | theQueue.remove(); // remove items 104 | theQueue.remove(); 105 | 106 | theQueue.displayQueue(); // display queue 107 | } // end main() 108 | } // end class LinkQueueApp 109 | //////////////////////////////////////////////////////////////// 110 | -------------------------------------------------------------------------------- /src/Chap05/linkList2/linkList2.java: -------------------------------------------------------------------------------- 1 | // linkList2.java 2 | // demonstrates linked list 3 | // to run this program: C>java LinkList2App 4 | //////////////////////////////////////////////////////////////// 5 | class Link 6 | { 7 | public int iData; // data item (key) 8 | public double dData; // data item 9 | public Link next; // next link in list 10 | // ------------------------------------------------------------- 11 | public Link(int id, double dd) // constructor 12 | { 13 | iData = id; 14 | dData = dd; 15 | } 16 | // ------------------------------------------------------------- 17 | public void displayLink() // display ourself 18 | { 19 | System.out.print("{" + iData + ", " + dData + "} "); 20 | } 21 | } // end class Link 22 | //////////////////////////////////////////////////////////////// 23 | class LinkList 24 | { 25 | private Link first; // ref to first link on list 26 | 27 | // ------------------------------------------------------------- 28 | public LinkList() // constructor 29 | { 30 | first = null; // no links on list yet 31 | } 32 | // ------------------------------------------------------------- 33 | public void insertFirst(int id, double dd) 34 | { // make new link 35 | Link newLink = new Link(id, dd); 36 | newLink.next = first; // it points to old first link 37 | first = newLink; // now first points to this 38 | } 39 | // ------------------------------------------------------------- 40 | public Link find(int key) // find link with given key 41 | { // (assumes non-empty list) 42 | Link current = first; // start at 'first' 43 | while(current.iData != key) // while no match, 44 | { 45 | if(current.next == null) // if end of list, 46 | return null; // didn't find it 47 | else // not end of list, 48 | current = current.next; // go to next link 49 | } 50 | return current; // found it 51 | } 52 | // ------------------------------------------------------------- 53 | public Link delete(int key) // delete link with given key 54 | { // (assumes non-empty list) 55 | Link current = first; // search for link 56 | Link previous = first; 57 | while(current.iData != key) 58 | { 59 | if(current.next == null) 60 | return null; // didn't find it 61 | else 62 | { 63 | previous = current; // go to next link 64 | current = current.next; 65 | } 66 | } // found it 67 | if(current == first) // if first link, 68 | first = first.next; // change first 69 | else // otherwise, 70 | previous.next = current.next; // bypass it 71 | return current; 72 | } 73 | // ------------------------------------------------------------- 74 | public void displayList() // display the list 75 | { 76 | System.out.print("List (first-->last): "); 77 | Link current = first; // start at beginning of list 78 | while(current != null) // until end of list, 79 | { 80 | current.displayLink(); // print data 81 | current = current.next; // move to next link 82 | } 83 | System.out.println(""); 84 | } 85 | // ------------------------------------------------------------- 86 | } // end class LinkList 87 | //////////////////////////////////////////////////////////////// 88 | class LinkList2App 89 | { 90 | public static void main(String[] args) 91 | { 92 | LinkList theList = new LinkList(); // make list 93 | 94 | theList.insertFirst(22, 2.99); // insert 4 items 95 | theList.insertFirst(44, 4.99); 96 | theList.insertFirst(66, 6.99); 97 | theList.insertFirst(88, 8.99); 98 | 99 | theList.displayList(); // display list 100 | 101 | Link f = theList.find(44); // find item 102 | if( f != null) 103 | System.out.println("Found link with key " + f.iData); 104 | else 105 | System.out.println("Can't find link"); 106 | 107 | Link d = theList.delete(66); // delete item 108 | if( d != null ) 109 | System.out.println("Deleted link with key " + d.iData); 110 | else 111 | System.out.println("Can't delete link"); 112 | 113 | theList.displayList(); // display list 114 | } // end main() 115 | } // end class LinkList2App 116 | //////////////////////////////////////////////////////////////// 117 | -------------------------------------------------------------------------------- /src/Chap06/stackTriangle/stackTriangle.java: -------------------------------------------------------------------------------- 1 | // stackTriangle.java 2 | // evaluates triangular numbers, stack replaces recursion 3 | // to run this program: C>java StackTriangleApp 4 | import java.io.*; // for I/O 5 | //////////////////////////////////////////////////////////////// 6 | class Params // parameters to save on stack 7 | { 8 | public int n; 9 | public int returnAddress; 10 | 11 | public Params(int nn, int ra) 12 | { 13 | n=nn; 14 | returnAddress=ra; 15 | } 16 | } // end class Params 17 | //////////////////////////////////////////////////////////////// 18 | class StackX 19 | { 20 | private int maxSize; // size of StackX array 21 | private Params[] stackArray; 22 | private int top; // top of stack 23 | //-------------------------------------------------------------- 24 | public StackX(int s) // constructor 25 | { 26 | maxSize = s; // set array size 27 | stackArray = new Params[maxSize]; // create array 28 | top = -1; // no items yet 29 | } 30 | //-------------------------------------------------------------- 31 | public void push(Params p) // put item on top of stack 32 | { 33 | stackArray[++top] = p; // increment top, insert item 34 | } 35 | //-------------------------------------------------------------- 36 | public Params pop() // take item from top of stack 37 | { 38 | return stackArray[top--]; // access item, decrement top 39 | } 40 | //-------------------------------------------------------------- 41 | public Params peek() // peek at top of stack 42 | { 43 | return stackArray[top]; 44 | } 45 | //-------------------------------------------------------------- 46 | } // end class StackX 47 | //////////////////////////////////////////////////////////////// 48 | class StackTriangleApp 49 | { 50 | static int theNumber; 51 | static int theAnswer; 52 | static StackX theStack; 53 | static int codePart; 54 | static Params theseParams; 55 | //------------------------------------------------------------- 56 | public static void main(String[] args) throws IOException 57 | { 58 | System.out.print("Enter a number: "); 59 | theNumber = getInt(); 60 | recTriangle(); 61 | System.out.println("Triangle="+theAnswer); 62 | } // end main() 63 | //------------------------------------------------------------- 64 | public static void recTriangle() 65 | { 66 | theStack = new StackX(10000); 67 | codePart = 1; 68 | while( step() == false) // call step() until it's true 69 | ; // null statement 70 | } 71 | //------------------------------------------------------------- 72 | public static boolean step() 73 | { 74 | switch(codePart) 75 | { 76 | case 1: // initial call 77 | theseParams = new Params(theNumber, 6); 78 | theStack.push(theseParams); 79 | codePart = 2; 80 | break; 81 | case 2: // method entry 82 | theseParams = theStack.peek(); 83 | if(theseParams.n == 1) // test 84 | { 85 | theAnswer = 1; 86 | codePart = 5; // exit 87 | } 88 | else 89 | codePart = 3; // recursive call 90 | break; 91 | case 3: // method call 92 | Params newParams = new Params(theseParams.n - 1, 4); 93 | theStack.push(newParams); 94 | codePart = 2; // go enter method 95 | break; 96 | case 4: // calculation 97 | theseParams = theStack.peek(); 98 | theAnswer = theAnswer + theseParams.n; 99 | codePart = 5; 100 | break; 101 | case 5: // method exit 102 | theseParams = theStack.peek(); 103 | codePart = theseParams.returnAddress; // (4 or 6) 104 | theStack.pop(); 105 | break; 106 | case 6: // return point 107 | return true; 108 | } // end switch 109 | return false; 110 | } // end triangle 111 | //------------------------------------------------------------- 112 | public static String getString() throws IOException 113 | { 114 | InputStreamReader isr = new InputStreamReader(System.in); 115 | BufferedReader br = new BufferedReader(isr); 116 | String s = br.readLine(); 117 | return s; 118 | } 119 | //------------------------------------------------------------- 120 | public static int getInt() throws IOException 121 | { 122 | String s = getString(); 123 | return Integer.parseInt(s); 124 | } 125 | //-------------------------------------------------------------- 126 | } // end class StackTriangleApp 127 | //////////////////////////////////////////////////////////////// 128 | -------------------------------------------------------------------------------- /src/Chap02/ClassData/classDataArray.java: -------------------------------------------------------------------------------- 1 | // classDataArray.java 2 | // data items as class objects 3 | // to run this program: C>java ClassDataApp 4 | //////////////////////////////////////////////////////////////// 5 | class Person 6 | { 7 | private String lastName; 8 | private String firstName; 9 | private int age; 10 | //-------------------------------------------------------------- 11 | public Person(String last, String first, int a) 12 | { // constructor 13 | lastName = last; 14 | firstName = first; 15 | age = a; 16 | } 17 | //-------------------------------------------------------------- 18 | public void displayPerson() 19 | { 20 | System.out.print(" Last name: " + lastName); 21 | System.out.print(", First name: " + firstName); 22 | System.out.println(", Age: " + age); 23 | } 24 | //-------------------------------------------------------------- 25 | public String getLast() // get last name 26 | { return lastName; } 27 | } // end class Person 28 | //////////////////////////////////////////////////////////////// 29 | class ClassDataArray 30 | { 31 | private Person[] a; // reference to array 32 | private int nElems; // number of data items 33 | 34 | public ClassDataArray(int max) // constructor 35 | { 36 | a = new Person[max]; // create the array 37 | nElems = 0; // no items yet 38 | } 39 | //-------------------------------------------------------------- 40 | public Person find(String searchName) 41 | { // find specified value 42 | int j; 43 | for(j=0; jjava PostfixApp 4 | import java.io.*; // for I/O 5 | //////////////////////////////////////////////////////////////// 6 | class StackX 7 | { 8 | private int maxSize; 9 | private int[] stackArray; 10 | private int top; 11 | //-------------------------------------------------------------- 12 | public StackX(int size) // constructor 13 | { 14 | maxSize = size; 15 | stackArray = new int[maxSize]; 16 | top = -1; 17 | } 18 | //-------------------------------------------------------------- 19 | public void push(int j) // put item on top of stack 20 | { stackArray[++top] = j; } 21 | //-------------------------------------------------------------- 22 | public int pop() // take item from top of stack 23 | { return stackArray[top--]; } 24 | //-------------------------------------------------------------- 25 | public int peek() // peek at top of stack 26 | { return stackArray[top]; } 27 | //-------------------------------------------------------------- 28 | public boolean isEmpty() // true if stack is empty 29 | { return (top == -1); } 30 | //-------------------------------------------------------------- 31 | public boolean isFull() // true if stack is full 32 | { return (top == maxSize-1); } 33 | //-------------------------------------------------------------- 34 | public int size() // return size 35 | { return top+1; } 36 | //-------------------------------------------------------------- 37 | public int peekN(int n) // peek at index n 38 | { return stackArray[n]; } 39 | //-------------------------------------------------------------- 40 | public void displayStack(String s) 41 | { 42 | System.out.print(s); 43 | System.out.print("Stack (bottom-->top): "); 44 | for(int j=0; j= '0' && ch <= '9') // if it's a number 74 | theStack.push( (int)(ch-'0') ); // push it 75 | else // it's an operator 76 | { 77 | num2 = theStack.pop(); // pop operands 78 | num1 = theStack.pop(); 79 | switch(ch) // do arithmetic 80 | { 81 | case '+': 82 | interAns = num1 + num2; 83 | break; 84 | case '-': 85 | interAns = num1 - num2; 86 | break; 87 | case '*': 88 | interAns = num1 * num2; 89 | break; 90 | case '/': 91 | interAns = num1 / num2; 92 | break; 93 | default: 94 | interAns = 0; 95 | } // end switch 96 | theStack.push(interAns); // push result 97 | } // end else 98 | } // end for 99 | interAns = theStack.pop(); // get answer 100 | return interAns; 101 | } // end doParse() 102 | } // end class ParsePost 103 | //////////////////////////////////////////////////////////////// 104 | class PostfixApp 105 | { 106 | public static void main(String[] args) throws IOException 107 | { 108 | String input; 109 | int output; 110 | 111 | while(true) 112 | { 113 | System.out.print("Enter postfix: "); 114 | System.out.flush(); 115 | input = getString(); // read a string from kbd 116 | if( input.equals("") ) // quit if [Enter] 117 | break; 118 | // make a parser 119 | ParsePost aParser = new ParsePost(input); 120 | output = aParser.doParse(); // do the evaluation 121 | System.out.println("Evaluates to " + output); 122 | } // end while 123 | } // end main() 124 | //-------------------------------------------------------------- 125 | public static String getString() throws IOException 126 | { 127 | InputStreamReader isr = new InputStreamReader(System.in); 128 | BufferedReader br = new BufferedReader(isr); 129 | String s = br.readLine(); 130 | return s; 131 | } 132 | //-------------------------------------------------------------- 133 | } // end class PostfixApp 134 | //////////////////////////////////////////////////////////////// 135 | -------------------------------------------------------------------------------- /src/Chap13/dfs/dfs.java: -------------------------------------------------------------------------------- 1 | // dfs.java 2 | // demonstrates depth-first search 3 | // to run this program: C>java DFSApp 4 | //////////////////////////////////////////////////////////////// 5 | class StackX 6 | { 7 | private final int SIZE = 20; 8 | private int[] st; 9 | private int top; 10 | // ------------------------------------------------------------ 11 | public StackX() // constructor 12 | { 13 | st = new int[SIZE]; // make array 14 | top = -1; 15 | } 16 | // ------------------------------------------------------------ 17 | public void push(int j) // put item on stack 18 | { st[++top] = j; } 19 | // ------------------------------------------------------------ 20 | public int pop() // take item off stack 21 | { return st[top--]; } 22 | // ------------------------------------------------------------ 23 | public int peek() // peek at top of stack 24 | { return st[top]; } 25 | // ------------------------------------------------------------ 26 | public boolean isEmpty() // true if nothing on stack 27 | { return (top == -1); } 28 | // ------------------------------------------------------------ 29 | } // end class StackX 30 | //////////////////////////////////////////////////////////////// 31 | class Vertex 32 | { 33 | public char label; // label (e.g. 'A') 34 | public boolean wasVisited; 35 | // ------------------------------------------------------------ 36 | public Vertex(char lab) // constructor 37 | { 38 | label = lab; 39 | wasVisited = false; 40 | } 41 | // ------------------------------------------------------------ 42 | } // end class Vertex 43 | //////////////////////////////////////////////////////////////// 44 | class Graph 45 | { 46 | private final int MAX_VERTS = 20; 47 | private Vertex vertexList[]; // list of vertices 48 | private int adjMat[][]; // adjacency matrix 49 | private int nVerts; // current number of vertices 50 | private StackX theStack; 51 | // ------------------------------------------------------------ 52 | public Graph() // constructor 53 | { 54 | vertexList = new Vertex[MAX_VERTS]; 55 | // adjacency matrix 56 | adjMat = new int[MAX_VERTS][MAX_VERTS]; 57 | nVerts = 0; 58 | for(int y=0; yjava BFSApp 4 | //////////////////////////////////////////////////////////////// 5 | class Queue 6 | { 7 | private final int SIZE = 20; 8 | private int[] queArray; 9 | private int front; 10 | private int rear; 11 | // ------------------------------------------------------------- 12 | public Queue() // constructor 13 | { 14 | queArray = new int[SIZE]; 15 | front = 0; 16 | rear = -1; 17 | } 18 | // ------------------------------------------------------------- 19 | public void insert(int j) // put item at rear of queue 20 | { 21 | if(rear == SIZE-1) 22 | rear = -1; 23 | queArray[++rear] = j; 24 | } 25 | // ------------------------------------------------------------- 26 | public int remove() // take item from front of queue 27 | { 28 | int temp = queArray[front++]; 29 | if(front == SIZE) 30 | front = 0; 31 | return temp; 32 | } 33 | // ------------------------------------------------------------- 34 | public boolean isEmpty() // true if queue is empty 35 | { 36 | return ( rear+1==front || (front+SIZE-1==rear) ); 37 | } 38 | // ------------------------------------------------------------- 39 | } // end class Queue 40 | //////////////////////////////////////////////////////////////// 41 | class Vertex 42 | { 43 | public char label; // label (e.g. 'A') 44 | public boolean wasVisited; 45 | // ------------------------------------------------------------- 46 | public Vertex(char lab) // constructor 47 | { 48 | label = lab; 49 | wasVisited = false; 50 | } 51 | // ------------------------------------------------------------- 52 | } // end class Vertex 53 | //////////////////////////////////////////////////////////////// 54 | class Graph 55 | { 56 | private final int MAX_VERTS = 20; 57 | private Vertex vertexList[]; // list of vertices 58 | private int adjMat[][]; // adjacency matrix 59 | private int nVerts; // current number of vertices 60 | private Queue theQueue; 61 | // ------------------------------------------------------------ 62 | public Graph() // constructor 63 | { 64 | vertexList = new Vertex[MAX_VERTS]; 65 | // adjacency matrix 66 | adjMat = new int[MAX_VERTS][MAX_VERTS]; 67 | nVerts = 0; 68 | for(int j=0; jjava QuickSort3App 4 | //////////////////////////////////////////////////////////////// 5 | class ArrayIns 6 | { 7 | private long[] theArray; // ref to array theArray 8 | private int nElems; // number of data items 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] ) 55 | swap(left, center); 56 | // order left & right 57 | if( theArray[left] > theArray[right] ) 58 | swap(left, right); 59 | // order center & right 60 | if( theArray[center] > theArray[right] ) 61 | swap(center, right); 62 | 63 | swap(center, right-1); // put pivot on right 64 | return theArray[right-1]; // return median value 65 | } // end medianOf3() 66 | //-------------------------------------------------------------- 67 | public void swap(int dex1, int dex2) // swap two elements 68 | { 69 | long temp = theArray[dex1]; // A into temp 70 | theArray[dex1] = theArray[dex2]; // B into A 71 | theArray[dex2] = temp; // temp into B 72 | } // end swap( 73 | //-------------------------------------------------------------- 74 | public int partitionIt(int left, int right, long pivot) 75 | { 76 | int leftPtr = left; // right of first elem 77 | int rightPtr = right - 1; // left of pivot 78 | while(true) 79 | { 80 | while( theArray[++leftPtr] < pivot ) // find bigger 81 | ; // (nop) 82 | while( theArray[--rightPtr] > pivot ) // find smaller 83 | ; // (nop) 84 | if(leftPtr >= rightPtr) // if pointers cross, 85 | break; // partition done 86 | else // not crossed, so 87 | swap(leftPtr, rightPtr); // swap elements 88 | } // end while(true) 89 | swap(leftPtr, right-1); // restore pivot 90 | return leftPtr; // return pivot location 91 | } // end partitionIt() 92 | //-------------------------------------------------------------- 93 | // insertion sort 94 | public void insertionSort(int left, int right) 95 | { 96 | int in, out; 97 | // sorted on left of out 98 | for(out=left+1; out<=right; out++) 99 | { 100 | long temp = theArray[out]; // remove marked item 101 | in = out; // start shifts at out 102 | // until one is smaller, 103 | while(in>left && theArray[in-1] >= temp) 104 | { 105 | theArray[in] = theArray[in-1]; // shift item to right 106 | --in; // go left one position 107 | } 108 | theArray[in] = temp; // insert marked item 109 | } // end for 110 | } // end insertionSort() 111 | //-------------------------------------------------------------- 112 | } // end class ArrayIns 113 | //////////////////////////////////////////////////////////////// 114 | class QuickSort3App 115 | { 116 | public static void main(String[] args) 117 | { 118 | int maxSize = 16; // array size 119 | ArrayIns arr; // reference to array 120 | arr = new ArrayIns(maxSize); // create the array 121 | 122 | for(int j=0; jjava QuickSort2App 4 | //////////////////////////////////////////////////////////////// 5 | class ArrayIns 6 | { 7 | private long[] theArray; // ref to array theArray 8 | private int nElems; // number of data items 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] ) 54 | swap(left, center); 55 | // order left & right 56 | if( theArray[left] > theArray[right] ) 57 | swap(left, right); 58 | // order center & right 59 | if( theArray[center] > theArray[right] ) 60 | swap(center, right); 61 | 62 | swap(center, right-1); // put pivot on right 63 | return theArray[right-1]; // return median value 64 | } // end medianOf3() 65 | //-------------------------------------------------------------- 66 | public void swap(int dex1, int dex2) // swap two elements 67 | { 68 | long temp = theArray[dex1]; // A into temp 69 | theArray[dex1] = theArray[dex2]; // B into A 70 | theArray[dex2] = temp; // temp into B 71 | } // end swap( 72 | //-------------------------------------------------------------- 73 | public int partitionIt(int left, int right, long pivot) 74 | { 75 | int leftPtr = left; // right of first elem 76 | int rightPtr = right - 1; // left of pivot 77 | 78 | while(true) 79 | { 80 | while( theArray[++leftPtr] < pivot ) // find bigger 81 | ; // (nop) 82 | while( theArray[--rightPtr] > pivot ) // find smaller 83 | ; // (nop) 84 | if(leftPtr >= rightPtr) // if pointers cross, 85 | break; // partition done 86 | else // not crossed, so 87 | swap(leftPtr, rightPtr); // swap elements 88 | } // end while(true) 89 | swap(leftPtr, right-1); // restore pivot 90 | return leftPtr; // return pivot location 91 | } // end partitionIt() 92 | //-------------------------------------------------------------- 93 | public void manualSort(int left, int right) 94 | { 95 | int size = right-left+1; 96 | if(size <= 1) 97 | return; // no sort necessary 98 | if(size == 2) 99 | { // 2-sort left and right 100 | if( theArray[left] > theArray[right] ) 101 | swap(left, right); 102 | return; 103 | } 104 | else // size is 3 105 | { // 3-sort left, center, & right 106 | if( theArray[left] > theArray[right-1] ) 107 | swap(left, right-1); // left, center 108 | if( theArray[left] > theArray[right] ) 109 | swap(left, right); // left, right 110 | if( theArray[right-1] > theArray[right] ) 111 | swap(right-1, right); // center, right 112 | } 113 | } // end manualSort() 114 | //-------------------------------------------------------------- 115 | } // end class ArrayIns 116 | //////////////////////////////////////////////////////////////// 117 | class QuickSort2App 118 | { 119 | public static void main(String[] args) 120 | { 121 | int maxSize = 16; // array size 122 | ArrayIns arr; // reference to array 123 | arr = new ArrayIns(maxSize); // create the array 124 | 125 | for(int j=0; jjava MSTApp 4 | //////////////////////////////////////////////////////////////// 5 | class StackX 6 | { 7 | private final int SIZE = 20; 8 | private int[] st; 9 | private int top; 10 | // ------------------------------------------------------------- 11 | public StackX() // constructor 12 | { 13 | st = new int[SIZE]; // make array 14 | top = -1; 15 | } 16 | // ------------------------------------------------------------- 17 | public void push(int j) // put item on stack 18 | { st[++top] = j; } 19 | // ------------------------------------------------------------- 20 | public int pop() // take item off stack 21 | { return st[top--]; } 22 | // ------------------------------------------------------------- 23 | public int peek() // peek at top of stack 24 | { return st[top]; } 25 | // ------------------------------------------------------------- 26 | public boolean isEmpty() // true if nothing on stack 27 | { return (top == -1); } 28 | // ------------------------------------------------------------- 29 | } // end class StackX 30 | //////////////////////////////////////////////////////////////// 31 | class Vertex 32 | { 33 | public char label; // label (e.g. 'A') 34 | public boolean wasVisited; 35 | // ------------------------------------------------------------- 36 | public Vertex(char lab) // constructor 37 | { 38 | label = lab; 39 | wasVisited = false; 40 | } 41 | // ------------------------------------------------------------- 42 | } // end class Vertex 43 | //////////////////////////////////////////////////////////////// 44 | class Graph 45 | { 46 | private final int MAX_VERTS = 20; 47 | private Vertex vertexList[]; // list of vertices 48 | private int adjMat[][]; // adjacency matrix 49 | private int nVerts; // current number of vertices 50 | private StackX theStack; 51 | // ------------------------------------------------------------- 52 | public Graph() // constructor 53 | { 54 | vertexList = new Vertex[MAX_VERTS]; 55 | // adjacency matrix 56 | adjMat = new int[MAX_VERTS][MAX_VERTS]; 57 | nVerts = 0; 58 | for(int j=0; j