Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csit;
- public class Node<T>{
- private T value;
- private Node<T> next;
- public Node(T value){
- this.value = value;
- this.next = null;
- }
- public Node(T value, Node<T> next){
- this.value = value;
- this.next = next;
- }
- public T getValue(){
- return value;
- }
- public Node<T> getNext(){
- return next;
- }
- public boolean hasNext(){
- return next != null;
- }
- public void setValue(T value){
- this.value = value;
- }
- public void setNext(Node<T> next){
- this.next = next;
- }
- public String toString(){
- return this.value.toString();
- }
- }
- public class Stack<T>{
- private Node<T> first;
- public Stack(){
- this.first = null;
- }
- public boolean isEmpty(){
- return this.first == null;
- }
- public void push(T x){
- this.first = new Node<T>(x, this.first);
- }
- public T pop(){
- T x = this.first.getValue();
- this.first = this.first.getNext();
- return x;
- }
- public T top(){
- return this.first.getValue();
- }
- public String toString(){
- String str = "[";
- Node<T> pos = this.first;
- while(pos != null){
- str = str + pos.getValue().toString();
- if(pos.getNext() != null) str = str + ", ";
- pos = pos.getNext();
- }
- str = str + "]";
- return str;
- }
- }
- public class Queue<T>{
- private Node<T> first;
- private Node<T> last;
- public Queue(){
- this.first = null;
- this.last = null;
- }
- public boolean isEmpty(){
- return this.first == null;
- }
- public void insert(T x){
- Node<T> temp = new Node<T>(x);
- if(this.last == null){
- this.first = temp;
- }else{
- this.last.setNext(temp);
- }
- this.last = temp;
- }
- public T remove(){
- T x = this.first.getValue();
- this.first = this.first.getNext();
- if(this.first == null) this.last = null;
- return x;
- }
- public T head(){
- return this.first.getValue();
- }
- public String toString(){
- String str = "[";
- Node<T> pos = this.first;
- while(pos != null){
- str = str + pos.getValue();
- if(pos.hasNext()) str = str + ", ";
- pos = pos.getNext();
- }
- str = str + "]";
- return str;
- }
- }
- public class BinNode<T>{
- private BinNode<T> left;
- private T value;
- private BinNode<T> right;
- public BinNode(T value){
- this.left = null;
- this.value = value;
- this.right = null;
- }
- public BinNode(BinNode<T> left, T value, BinNode<T> right){
- this.left = left;
- this.value = value;
- this.right = right;
- }
- public T getValue(){
- return this.value;
- }
- public void setValue(T value){
- this.value = value;
- }
- public BinNode<T> getLeft(){
- return this.left;
- }
- public BinNode<T> setLeft(BinNode<T> left){
- return this.left = left;
- }
- public boolean hasLeft(){
- return this.left != null;
- }
- public BinNode<T> getRight(){
- return this.right;
- }
- public BinNode<T> setRight(BinNode<T> right){
- return this.right = right;
- }
- public boolean hasRight(){
- return this.right != null;
- }
- public String toString(){
- return "" + this.value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment