diff --git a/data_structures_homework/Account.java b/data_structures_homework/Account.java new file mode 100644 index 0000000..150c3c4 --- /dev/null +++ b/data_structures_homework/Account.java @@ -0,0 +1,58 @@ + +package data_structures_homework; + +import java.util.*; + +public class Account { + +private int accNum; +private String accBranch; + + public int getAccNum() { + return accNum; + } + + public void setAccNum(int accNum) { + this.accNum = accNum; + } + + public String getAccBranch() { + return accBranch; + } + + public void setAccBranch(String accBranch) { + this.accBranch = accBranch; + } + + + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Account other = (Account) obj; + if (this.accNum != other.accNum) { + return false; + } + return true; + } + + +@Override + public int hashCode() { + int hash = 7; + hash = 73 * hash + this.accNum; + return hash; + } + + + + +} diff --git a/data_structures_homework/ArrList.java b/data_structures_homework/ArrList.java new file mode 100644 index 0000000..6e4f132 --- /dev/null +++ b/data_structures_homework/ArrList.java @@ -0,0 +1,46 @@ +package data_structures_homework; + + +import java.util.*; + + +public class ArrList { + + public static void main(String [] args){ + + + Account account1 = new Account(); + account1.setAccNum(1); + account1.setAccBranch("NEDBANK"); + + Account account2 = new Account(); + account2.setAccNum(2); + account2.setAccBranch("STANDARD BANK"); + + Account account3 = new Account(); + account3.setAccNum(3); + account3.setAccBranch("FNB"); + + Account account4 = new Account(); + account4.setAccNum(4); + account4.setAccBranch("ABSA"); + + List listAccounts = new ArrayList(); + listAccounts.add(account1); + listAccounts.add(account2); + listAccounts.add(account3); + listAccounts.add(account4); + + System.out.println("The size of the list: " + listAccounts.size()); + + Set hassh = new HashSet(); + hassh.add(account1); + hassh.add(account2); + hassh.add(account3); + hassh.add(account4); + + System.out.println("The items in the hassh bag: " + hassh.size()); + + } + +} diff --git a/data_structures_homework/Company.java b/data_structures_homework/Company.java new file mode 100644 index 0000000..a639602 --- /dev/null +++ b/data_structures_homework/Company.java @@ -0,0 +1,102 @@ +import java.util.*; + + +public class Company { + +private String compName; +private int compNum; +private String compLocation; + + public String getCompName() { + return compName; + } + + public int getCompNum() { + return compNum; + } + + public String getCompLocation() { + return compLocation; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 97 * hash + this.compNum; + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Company other = (Company) obj; + if (this.compNum != other.compNum) { + return false; + } + return true; + } + + + + + + + + + + //Constructor that initializes the class +public Company(Builder builder){ + +compName=builder.compName; +compNum=builder.compNum; +compLocation=builder.compLocation; + +} + +public static class Builder{ + +private String compName; +private int compNum; +private String compLocation; + + public Builder compName(String val){ + + this.compName = val; + return this; + } + + public Builder compNum(int val){ + + this.compNum = val; + return this; + } + + public Builder compLocation(String val){ + + this.compLocation = val; + return this; + } + + public Company build(){ + + return new Company(this); + + } + + + +} + + + + + +} diff --git a/data_structures_homework/LinkyList.java b/data_structures_homework/LinkyList.java new file mode 100644 index 0000000..9fdca0f --- /dev/null +++ b/data_structures_homework/LinkyList.java @@ -0,0 +1,108 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package data_structures_homework; + +/** + * + * @author BenitoKriel + */ +public class LinkyList { + + + private static class Node { + private Company data; + private Node nextLink; + + public Node(Company data, Node next) { + this.data = data; + this.nextLink = next; + } + + public Node(Company data) { + this.data = data; + this.nextLink = null; + } + + + } + + private Node head; // sometimes called top + private int size; + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + // Node Insertion in a Linked List + // 1. Insert element at the start + + public void addAtHead(Company data) { + head = new Node(data, head); + size++; + } + + public void addToTail(Company data) { + Node newNode = new Node(data); + Node curr = head; + + if (head == null) + head = newNode; + + while (curr.nextLink != null) { + curr = curr.nextLink; + } + curr.nextLink = newNode; + + } + + Node InsertNth(Company data, int position) { + Node node = head; + if (position == 0) { + node = new Node(data, head); + return node; + } else { + while (--position > 0) { + node = node.nextLink; + } + Node next = node.nextLink; + node.nextLink = new Node(data, next); + return head; + } + } + + public void addSorted(Company data) { + Node newNode = new Node(data); + Node curr = head; + + if (curr.nextLink == null || curr.data.getCompNum() > data.getCompNum()) { + newNode.nextLink = head; + return; + } + + while (curr.nextLink != null && curr.nextLink.data.getCompNum() < data.getCompNum()) { + curr = curr.nextLink; + } + + newNode.nextLink = curr.nextLink; + curr.nextLink = newNode; + } + + // TRAVERSE A LINKED LIST + + public void print() { + Node temp = head; + while (temp != null) { + System.out.println(temp.data.getCompName() + ", " + temp.data.getCompNum() + ", "); + temp = temp.nextLink; + } + + } + +}