ZeroByter

CSIT classes

Apr 10th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package csit;
  2.  
  3. public class Node<T>{
  4.     private T value;
  5.     private Node<T> next;
  6.  
  7.     public Node(T value){
  8.         this.value = value;
  9.         this.next = null;
  10.     }
  11.  
  12.     public Node(T value, Node<T> next){
  13.         this.value = value;
  14.         this.next = next;
  15.     }
  16.  
  17.     public T getValue(){
  18.         return value;
  19.     }
  20.  
  21.     public Node<T> getNext(){
  22.         return next;
  23.     }
  24.  
  25.     public boolean hasNext(){
  26.         return next != null;
  27.     }
  28.  
  29.     public void setValue(T value){
  30.         this.value = value;
  31.     }
  32.  
  33.     public void setNext(Node<T> next){
  34.         this.next = next;
  35.     }
  36.  
  37.     public String toString(){
  38.         return this.value.toString();
  39.     }
  40. }
  41.  
  42. public class Stack<T>{
  43.     private Node<T> first;
  44.    
  45.     public Stack(){
  46.         this.first = null;
  47.     }
  48.  
  49.     public boolean isEmpty(){
  50.         return this.first == null;
  51.     }
  52.  
  53.     public void push(T x){
  54.         this.first = new Node<T>(x, this.first);
  55.     }
  56.  
  57.     public T pop(){
  58.         T x = this.first.getValue();
  59.         this.first = this.first.getNext();
  60.         return x;
  61.     }
  62.  
  63.     public T top(){
  64.         return this.first.getValue();
  65.     }
  66.  
  67.     public String toString(){
  68.         String str = "[";
  69.        
  70.         Node<T> pos = this.first;
  71.         while(pos != null){
  72.             str = str + pos.getValue().toString();
  73.             if(pos.getNext() != null) str = str + ", ";
  74.             pos = pos.getNext();
  75.         }
  76.         str = str + "]";
  77.  
  78.         return str;
  79.     }
  80. }
  81.  
  82. public class Queue<T>{
  83.     private Node<T> first;
  84.     private Node<T> last;
  85.  
  86.     public Queue(){
  87.         this.first = null;
  88.         this.last = null;
  89.     }
  90.    
  91.     public boolean isEmpty(){
  92.         return this.first == null;
  93.     }
  94.  
  95.     public void insert(T x){
  96.         Node<T> temp = new Node<T>(x);
  97.         if(this.last == null){
  98.             this.first = temp;
  99.         }else{
  100.             this.last.setNext(temp);
  101.         }
  102.         this.last = temp;
  103.     }
  104.  
  105.     public T remove(){
  106.         T x = this.first.getValue();
  107.         this.first = this.first.getNext();
  108.         if(this.first == null) this.last = null;
  109.         return x;
  110.     }
  111.  
  112.     public T head(){
  113.         return this.first.getValue();
  114.     }
  115.  
  116.     public String toString(){
  117.         String str = "[";
  118.         Node<T> pos = this.first;
  119.         while(pos != null){
  120.             str = str + pos.getValue();
  121.             if(pos.hasNext()) str = str + ", ";
  122.             pos = pos.getNext();
  123.         }
  124.         str = str + "]";
  125.         return str;
  126.     }
  127. }
  128.  
  129. public class BinNode<T>{
  130.     private BinNode<T> left;
  131.     private T value;
  132.     private BinNode<T> right;
  133.  
  134.     public BinNode(T value){
  135.         this.left = null;
  136.         this.value = value;
  137.         this.right = null;
  138.     }
  139.  
  140.     public BinNode(BinNode<T> left, T value, BinNode<T> right){
  141.         this.left = left;
  142.         this.value = value;
  143.         this.right = right;
  144.     }
  145.  
  146.     public T getValue(){
  147.         return this.value;
  148.     }
  149.  
  150.     public void setValue(T value){
  151.         this.value = value;
  152.     }
  153.  
  154.     public BinNode<T> getLeft(){
  155.         return this.left;
  156.     }
  157.  
  158.     public BinNode<T> setLeft(BinNode<T> left){
  159.         return this.left = left;
  160.     }
  161.  
  162.     public boolean hasLeft(){
  163.         return this.left != null;
  164.     }
  165.  
  166.     public BinNode<T> getRight(){
  167.         return this.right;
  168.     }
  169.  
  170.     public BinNode<T> setRight(BinNode<T> right){
  171.         return this.right = right;
  172.     }
  173.  
  174.     public boolean hasRight(){
  175.         return this.right != null;
  176.     }
  177.  
  178.     public String toString(){
  179.         return "" + this.value;
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment