├── README.md ├── .gitignore └── src └── java ├── MotorcycleOilChangeApp.java ├── CardStackApp.java ├── SnowbirdLiftApp.java ├── TrainLinkedListApp.java └── ContactManagerApp.java /README.md: -------------------------------------------------------------------------------- 1 | # ps-data-structure-helpers 2 | Simple Java Helper Classes to help test and work with home grown data structures built in the Java Data Structures Pluralsight course. 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | .DS_Store 14 | src/.DS_Store -------------------------------------------------------------------------------- /src/java/MotorcycleOilChangeApp.java: -------------------------------------------------------------------------------- 1 | 2 | public class MotorcycleOilChangeApp { 3 | //we will use a 12 piece socket set for this job 4 | private BasicHashTable toolset = new BasicHashTable<>(12); 5 | 6 | public static void main(String [] args) { 7 | MotorcycleOilChangeApp app = new MotorcycleOilChangeApp(); 8 | app.changeOil(); 9 | } 10 | 11 | public void changeOil() { 12 | buildToolset(); 13 | 14 | //remove skid plate to get at the oil pan 15 | removeSkidPlate(); 16 | 17 | //drain the oil by removing the drain plug and refill 18 | drainAndReplaceOil(); 19 | 20 | //replace the skid plate 21 | replaceSkidPlate(); 22 | 23 | emptyToolset(); 24 | } 25 | 26 | /** 27 | * This loads the toolbox with all the needed tools 28 | */ 29 | private void buildToolset() { 30 | toolset.put("8mm", "8mm Socket"); 31 | toolset.put("10mm", "10mm Socket"); 32 | toolset.put("12mm", "12mm Socket"); 33 | toolset.put("14mm", "14mm Socket"); 34 | toolset.put("17mm", "17mm Socket"); 35 | toolset.put("1/4sae", "1/4sae Socket"); 36 | toolset.put("3/8sae", "3/8sae Socket"); 37 | toolset.put("1/2sae", "1/2sae Socket"); 38 | toolset.put("5/8sae", "5/8sae Socket"); 39 | toolset.put("3/4sae", "3/4sae Socket"); 40 | toolset.put("1/4d", "1/4 socket wrench"); 41 | toolset.put("2de", "2 inch Drive extension"); 42 | System.out.println("Toolset size: " + toolset.size()); 43 | } 44 | 45 | private void emptyToolset() { 46 | toolset.delete("8mm"); 47 | toolset.delete("10mm"); 48 | toolset.delete("12mm"); 49 | toolset.delete("14mm"); 50 | toolset.delete("17mm"); 51 | toolset.delete("1/4sae"); 52 | toolset.delete("3/8sae"); 53 | toolset.delete("1/2sae"); 54 | toolset.delete("5/8sae"); 55 | toolset.delete("3/4sae"); 56 | toolset.delete("1/4d"); 57 | toolset.delete("2de"); 58 | System.out.println("Toolset size: " + toolset.size()); 59 | } 60 | 61 | private void removeSkidPlate() { 62 | //get the 10mm socket and wrench 63 | String socket = toolset.get("10mm"); 64 | String wrench = toolset.get("1/4d"); 65 | System.out.println("Removed 4 skid plate nuts with the " + socket + " and " + wrench); 66 | } 67 | 68 | private void replaceSkidPlate() { 69 | //get the 10mm socket and wrench 70 | String socket = toolset.get("10mm"); 71 | String wrench = toolset.get("1/4d"); 72 | System.out.println("Replaced 4 skid plate nuts with the " + socket + " and " + wrench); 73 | } 74 | 75 | private void drainAndReplaceOil() { 76 | //this motorcycle's drain plug nut is 17mm. That's a strange size, check to see if the toolset has it 77 | System.out.println("Toolset has 17mm socket: " + toolset.hasValue("17mm Socket") + " - " + toolset.hasKey("17mm")); 78 | 79 | String socket = toolset.get("17mm"); 80 | String wrench = toolset.get("1/4d"); 81 | System.out.println("Removed oil drain plug with " + socket + " and " + wrench); 82 | System.out.println("Drained 1.7 quarts of oil"); 83 | System.out.println("Replaced oil drain plug with " + socket + " and " + wrench); 84 | System.out.println("Added 1.7 quarts of oil"); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/java/CardStackApp.java: -------------------------------------------------------------------------------- 1 | 2 | public class CardStackApp { 3 | BasicStack stack = new BasicStack(); 4 | 5 | public static void main(String[] args) { 6 | CardStackApp app = new CardStackApp(); 7 | app.stackCards(); 8 | app.unstackCards(); 9 | 10 | //restack cards 11 | app.stackCards(); 12 | 13 | //how many cards are on the deck 14 | app.desckSize(); 15 | 16 | //do we have queen of hearts in the deck 17 | app.containsCard("Queen of Hearts"); 18 | 19 | //do we have a joker 20 | app.containsCard("Joker"); 21 | 22 | //go to the king of diamonds 23 | app.goToCard("King of Diamonds"); 24 | 25 | //now how many cards are on the deck 26 | app.desckSize(); 27 | } 28 | 29 | public void stackCards() { 30 | //stack the spade suit 31 | stack.push("Ace of Spades"); 32 | stack.push("2 of Spades"); 33 | stack.push("3 of Spades"); 34 | stack.push("4 of Spades"); 35 | stack.push("5 of Spades"); 36 | stack.push("6 of Spades"); 37 | stack.push("7 of Spades"); 38 | stack.push("8 of Spades"); 39 | stack.push("9 of Spades"); 40 | stack.push("10 of Spades"); 41 | stack.push("Jack of Spades"); 42 | stack.push("Queen of Spades"); 43 | stack.push("King of Spades"); 44 | 45 | //stack the diamond suit 46 | stack.push("Ace of Diamonds"); 47 | stack.push("2 of Diamonds"); 48 | stack.push("3 of Diamonds"); 49 | stack.push("4 of Diamonds"); 50 | stack.push("5 of Diamonds"); 51 | stack.push("6 of Diamonds"); 52 | stack.push("7 of Diamonds"); 53 | stack.push("8 of Diamonds"); 54 | stack.push("9 of Diamonds"); 55 | stack.push("10 of Diamonds"); 56 | stack.push("Jack of Diamonds"); 57 | stack.push("Queen of Diamonds"); 58 | stack.push("King of Diamonds"); 59 | 60 | //stack the club suit 61 | stack.push("Ace of Clubs"); 62 | stack.push("2 of Clubs"); 63 | stack.push("3 of Clubs"); 64 | stack.push("4 of Clubs"); 65 | stack.push("5 of Clubs"); 66 | stack.push("6 of Clubs"); 67 | stack.push("7 of Clubs"); 68 | stack.push("8 of Clubs"); 69 | stack.push("9 of Clubs"); 70 | stack.push("10 of Clubs"); 71 | stack.push("Jack of Clubs"); 72 | stack.push("Queen of Clubs"); 73 | stack.push("King of Clubs"); 74 | 75 | //stack the heart suit 76 | stack.push("Ace of Hearts"); 77 | stack.push("2 of Hearts"); 78 | stack.push("3 of Hearts"); 79 | stack.push("4 of Hearts"); 80 | stack.push("5 of Hearts"); 81 | stack.push("6 of Hearts"); 82 | stack.push("7 of Hearts"); 83 | stack.push("8 of Hearts"); 84 | stack.push("9 of Hearts"); 85 | stack.push("10 of Hearts"); 86 | stack.push("Jack of Hearts"); 87 | stack.push("Queen of Hearts"); 88 | stack.push("King of Hearts"); 89 | } 90 | 91 | public void unstackCards() { 92 | //now pull the cards off the stack and print them 93 | while(stack.size() > 0) { 94 | System.out.println(stack.pop()); 95 | } 96 | } 97 | 98 | public void containsCard(String card) { 99 | System.out.println(stack.contains(card)); 100 | } 101 | 102 | public void goToCard(String card) { 103 | System.out.println(stack.access(card)); 104 | } 105 | 106 | public void desckSize() { 107 | System.out.println(stack.size()); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/java/SnowbirdLiftApp.java: -------------------------------------------------------------------------------- 1 | 2 | public class SnowbirdLiftApp { 3 | BasicQueue gad2Lift = new BasicQueue(); 4 | 5 | public static void main(String[] args) { 6 | SnowbirdLiftApp app = new SnowbirdLiftApp(); 7 | app.runLift(); 8 | } 9 | 10 | public void runLift() { 11 | loadLift(); 12 | 13 | //print out the lift size 14 | System.out.println(gad2Lift.size()); 15 | 16 | //does the lift contain skiers Mary and Anna 17 | System.out.println("Lift has Mary and Anna on it: " + gad2Lift.contains(new Gad2Chair("Mary", "Anna"))); 18 | 19 | //who's at the 2nd position - remember 0 based index 20 | System.out.println("2nd chair has: " + gad2Lift.access(1).listChairRiders()); 21 | 22 | unloadLift(); 23 | 24 | //if the lift is unloaded again, an error should occur since no one is left on it 25 | try { 26 | unloadLift(); 27 | } catch (IllegalStateException t) { 28 | System.out.println("Can't unload any more skiers because the lift is empty: " + t.getMessage()); 29 | } 30 | } 31 | 32 | private void loadLift() { 33 | //skiers in line to get on lift 34 | Gad2Chair chair1 = new Gad2Chair("John", "Dave"); 35 | Gad2Chair chair2 = new Gad2Chair("Samantha", "Kelly"); 36 | Gad2Chair chair3 = new Gad2Chair("Mary", "Anna"); 37 | Gad2Chair chair4 = new Gad2Chair("Robert", "Chad"); 38 | Gad2Chair chair5 = new Gad2Chair("Sarah", "Bill"); 39 | 40 | //load them onto the lift 41 | gad2Lift.enQueue(chair1); 42 | gad2Lift.enQueue(chair2); 43 | gad2Lift.enQueue(chair3); 44 | gad2Lift.enQueue(chair4); 45 | gad2Lift.enQueue(chair5); 46 | } 47 | 48 | private void unloadLift() { 49 | Gad2Chair chair = gad2Lift.deQueue(); 50 | 51 | //should be John and Dave 52 | System.out.println(chair.listChairRiders()); 53 | 54 | chair = gad2Lift.deQueue(); 55 | 56 | //should be Samantha and Kelly 57 | System.out.println(chair.listChairRiders()); 58 | 59 | chair = gad2Lift.deQueue(); 60 | 61 | //should be Mary and Anna 62 | System.out.println(chair.listChairRiders()); 63 | 64 | chair = gad2Lift.deQueue(); 65 | 66 | //should be Robert and Chad 67 | System.out.println(chair.listChairRiders()); 68 | 69 | chair = gad2Lift.deQueue(); 70 | 71 | //should be Sarah and Bill 72 | System.out.println(chair.listChairRiders()); 73 | } 74 | 75 | class Gad2Chair { 76 | private String seat1Name; 77 | private String seat2Name; 78 | 79 | public Gad2Chair(String skierInFirstSeat, String skierInSecondSeat) { 80 | this.seat1Name = skierInFirstSeat; 81 | this.seat2Name = skierInSecondSeat; 82 | } 83 | 84 | @Override 85 | public int hashCode() { 86 | final int prime = 31; 87 | int result = 1; 88 | result = prime * result + getOuterType().hashCode(); 89 | result = prime * result + ((seat1Name == null) ? 0 : seat1Name.hashCode()); 90 | result = prime * result + ((seat2Name == null) ? 0 : seat2Name.hashCode()); 91 | return result; 92 | } 93 | 94 | @Override 95 | public boolean equals(Object obj) { 96 | if (this == obj) 97 | return true; 98 | if (obj == null) 99 | return false; 100 | if (getClass() != obj.getClass()) 101 | return false; 102 | Gad2Chair other = (Gad2Chair) obj; 103 | if (!getOuterType().equals(other.getOuterType())) 104 | return false; 105 | if (seat1Name == null) { 106 | if (other.seat1Name != null) 107 | return false; 108 | } else if (!seat1Name.equals(other.seat1Name)) 109 | return false; 110 | if (seat2Name == null) { 111 | if (other.seat2Name != null) 112 | return false; 113 | } else if (!seat2Name.equals(other.seat2Name)) 114 | return false; 115 | return true; 116 | } 117 | 118 | public String listChairRiders() { 119 | return this.seat1Name + ", " + this.seat2Name; 120 | } 121 | 122 | private SnowbirdLiftApp getOuterType() { 123 | return SnowbirdLiftApp.this; 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/java/TrainLinkedListApp.java: -------------------------------------------------------------------------------- 1 | 2 | public class TrainLinkedListApp { 3 | BasicLinkedList train = new BasicLinkedList(); 4 | 5 | public static void main(String[] args) { 6 | TrainLinkedListApp app = new TrainLinkedListApp(); 7 | app.buildInitialTrain(); 8 | 9 | //print out the train size 10 | System.out.println(app.trainSize()); 11 | 12 | //first stop, we remove a car and add a couple more 13 | app.firstStop(); 14 | 15 | //print out the train size 16 | System.out.println("After First Stop train size: " + app.trainSize()); 17 | 18 | //second stop, we need to remove all the tankers 19 | app.secondStop(); 20 | 21 | //print out the train size 22 | System.out.println("After Second Stop train size: " + app.trainSize()); 23 | 24 | //at the last stop we remove all of the train cars and we're done 25 | app.lastStop(); 26 | 27 | //print out the train size 28 | System.out.println("After Last Stop train size: " + app.trainSize()); 29 | } 30 | 31 | private int trainSize() { 32 | return train.size(); 33 | } 34 | 35 | private void buildInitialTrain() { 36 | //build the train for it's initial trip 37 | TrainCar car1 = new TrainCar(CarType.BOXCAR, "Amazon Packages"); 38 | TrainCar car2 = new TrainCar(CarType.FLATBED, "Farm Machinery"); 39 | TrainCar car3 = new TrainCar(CarType.BOXCAR, "Paper"); 40 | TrainCar car4 = new TrainCar(CarType.BOXCAR, "Grease"); 41 | TrainCar car5 = new TrainCar(CarType.TANKER, "Crude Oil #1"); 42 | TrainCar car6 = new TrainCar(CarType.TANKER, "Crude Oil #2"); 43 | TrainCar car7 = new TrainCar(CarType.TANKER, "Crude Oil #3"); 44 | TrainCar car8 = new TrainCar(CarType.FLATBED, "Empty Shipping Container"); 45 | TrainCar car9 = new TrainCar(CarType.HOPPER, "Wheat Grain"); 46 | TrainCar car10 = new TrainCar(CarType.HOPPER, "Coal"); 47 | 48 | //connect the cars 49 | train.add(car1); 50 | train.add(car2); 51 | train.add(car3); 52 | train.add(car4); 53 | train.add(car5); 54 | train.add(car6); 55 | train.add(car7); 56 | train.add(car8); 57 | train.add(car9); 58 | train.add(car10); 59 | 60 | //test out the find and get 61 | //see if we can find the position of the paper box car and then get it 62 | int position = train.find(car3); 63 | TrainCar paperCar = train.get(position); 64 | System.out.println("Train is built correctly. found and retrieved the paper car at position: " + paperCar + " - " + position); 65 | 66 | //print out the train cars 67 | System.out.println(train); 68 | } 69 | 70 | private void firstStop() { 71 | //at this stop we need to pull off the first box car and insert a new BoxCar after the farm machinery 72 | TrainCar boxcar = train.remove(); 73 | 74 | System.out.println("First Stop: Removed - " + boxcar); 75 | 76 | TrainCar newBoxcar = new TrainCar(CarType.BOXCAR, "Farm Fence Posts and Barbwire"); 77 | train.insert(newBoxcar, 1); 78 | 79 | //print out the train cars 80 | System.out.println(train); 81 | } 82 | 83 | private void secondStop() { 84 | //at this stop we need to pull off all of the tanker cars. They should start at position 5 and there's 3 of them 85 | TrainCar car = train.removeAt(5); 86 | System.out.println("Second Stop: Removed - " + car); 87 | 88 | car = train.removeAt(5); 89 | System.out.println("Second Stop: Removed - " + car); 90 | 91 | car = train.removeAt(5); 92 | System.out.println("Second Stop: Removed - " + car); 93 | 94 | //print out the train cars 95 | System.out.println(train); 96 | } 97 | 98 | private void lastStop() { 99 | //at this stop we simply pull the remaining cars off of the train until we have no more train. 100 | 101 | try{ 102 | while(true) { 103 | TrainCar car = train.remove(); 104 | System.out.println("Last Stop: Removed - " + car); 105 | } 106 | } catch (IllegalStateException ise) { 107 | //when we get an ise that means we don't have any more cars to remove and the train is now empty 108 | } 109 | 110 | //print out the train cars 111 | System.out.println(train); 112 | } 113 | 114 | class TrainCar { 115 | private CarType type; 116 | private String contents; 117 | 118 | public TrainCar(CarType carType, String carContents) { 119 | this.type = carType; 120 | this.contents = carContents; 121 | } 122 | 123 | public String toString() { 124 | return type.toString() + " - " + contents; 125 | } 126 | } 127 | 128 | enum CarType { 129 | BOXCAR, TANKER, FLATBED, HOPPER 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/java/ContactManagerApp.java: -------------------------------------------------------------------------------- 1 | 2 | public class ContactManagerApp { 3 | BasicBinaryTree contacts = new BasicBinaryTree(); 4 | 5 | public static void main(String[] args) { 6 | ContactManagerApp app = new ContactManagerApp(); 7 | app.loadContacts(); 8 | 9 | //See if we have any of the following contacts 10 | app.searchContacts(); 11 | 12 | //delete some contacts 13 | app.cleanupContacts(); 14 | } 15 | 16 | public void loadContacts() { 17 | contacts.add(new Contact("Abe", "Lincoln", "123-555-5555")); 18 | contacts.add(new Contact("Sheree", "Whisman", "123-123-4567")); 19 | contacts.add(new Contact("Doreatha", "Crumbley", "123-123-4568")); 20 | contacts.add(new Contact("Mitchel", "Wear", "123-123-4567")); 21 | contacts.add(new Contact("Lisabeth", "Espiritu", "123-123-4569")); 22 | contacts.add(new Contact("Cora", "Bonhomme", "123-123-4567")); 23 | contacts.add(new Contact("Brigette", "Aikins", "123-123-3567")); 24 | contacts.add(new Contact("Julieann", "Kellett", "123-123-4367")); 25 | contacts.add(new Contact("Floy", "Collin", "123-123-4537")); 26 | contacts.add(new Contact("Abel", "Rocco", "123-123-4563")); 27 | contacts.add(new Contact("Karen", "Presler", "123-223-4567")); 28 | contacts.add(new Contact("Maryjane", "Archambault", "123-123-1234")); 29 | contacts.add(new Contact("Afton", "Tadlock", "123-123-2223")); 30 | contacts.add(new Contact("Ines", "Ludlow", "123-123-2224")); 31 | contacts.add(new Contact("Cristen", "Skillern", "123-123-3334")); 32 | contacts.add(new Contact("Porsche", "Gadsden", "123-123-4444")); 33 | contacts.add(new Contact("Sharee", "Glazer", "123-123-3335")); 34 | contacts.add(new Contact("Ashly", "Absher", "123-123-4443")); 35 | contacts.add(new Contact("Rebekah", "Eide", "123-123-3336")); 36 | contacts.add(new Contact("Dian", "Goheen", "123-123-4555")); 37 | contacts.add(new Contact("Velda", "Mitchem", "123-123-5554")); 38 | contacts.add(new Contact("Fay", "Moro", "123-123-4556")); 39 | contacts.add(new Contact("Claudette", "Damewood", "123-123-6664")); 40 | contacts.add(new Contact("Pei", "Rogan", "123-123-6545")); 41 | contacts.add(new Contact("Sandie", "Mcmillion", "123-123-5557")); 42 | contacts.add(new Contact("Gemma", "Uy", "123-123-7776")); 43 | contacts.add(new Contact("Annetta", "Coale", "123-123-8899")); 44 | contacts.add(new Contact("Ona", "Hynes", "123-123-9876")); 45 | contacts.add(new Contact("Daryl", "Clukey", "123-123-4721")); 46 | contacts.add(new Contact("Meghann", "Wischmeier", "123-123-4321")); 47 | System.out.println("Loaded " + contacts.size() + " contacts"); 48 | } 49 | 50 | public void searchContacts() { 51 | // We don't need to populate phone since we only compare on last name 52 | System.out.println(contacts.contains(new Contact("Fay", "Moro", null))); // should be true 53 | System.out.println(contacts.contains(new Contact("Bob", "Hope", null))); // should be false 54 | } 55 | 56 | public void cleanupContacts() { 57 | // we just need to remove by last name since that's all we check in the comparable 58 | contacts.delete(new Contact("Pei", "Rogan", null)); //delete a leaf node 59 | contacts.delete(new Contact("Porsche", "Gadsden", null)); //delete a node with a right node only 60 | contacts.delete(new Contact("Annetta", "Coale", null)); //delete a node with a left node only 61 | contacts.delete(new Contact("Cora", "Bonhomme", null)); //delete a node with both children 62 | contacts.delete(new Contact("Abe", "Lincoln", null)); //delete the root node 63 | System.out.println(contacts.size() + " contacts remaining"); 64 | } 65 | 66 | class Contact implements Comparable { 67 | private String firstName; 68 | private String lastName; 69 | private String phone; 70 | 71 | public Contact(String firstName, String lastName, String phone) { 72 | this.firstName = firstName; 73 | this.lastName = lastName; 74 | this.phone = phone; 75 | } 76 | 77 | public String getFirstName() { 78 | return firstName; 79 | } 80 | public void setFirstName(String firstName) { 81 | this.firstName = firstName; 82 | } 83 | public String getLastName() { 84 | return lastName; 85 | } 86 | public void setLastName(String lastName) { 87 | this.lastName = lastName; 88 | } 89 | public String getPhone() { 90 | return phone; 91 | } 92 | public void setPhone(String phone) { 93 | this.phone = phone; 94 | } 95 | 96 | @Override 97 | public int compareTo(Contact other) { 98 | // For simplicity, we're only going to sort on the contact's last name 99 | return this.lastName.compareTo(other.lastName); 100 | } 101 | 102 | @Override 103 | public String toString() { 104 | return "Contact [firstName=" + firstName + ", lastName=" + lastName + ", phone=" + phone + "]"; 105 | } 106 | } 107 | } 108 | --------------------------------------------------------------------------------