Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package com.chamika;
  2.  
  3. import java.util.Iterator;
  4.  
  5. /**
  6.  * Created by dinu on 12/13/17.
  7.  */
  8. public class Stack {
  9.     public static void main(String[] args) {
  10.         StackDataStructure<Integer> stackDataStructure = new StackDataStructure();
  11.         stackDataStructure.print();
  12.         stackDataStructure.push(9);
  13.         stackDataStructure.push(7);
  14.         stackDataStructure.push(89);
  15.         stackDataStructure.print();
  16.         stackDataStructure.pop();
  17.         stackDataStructure.print();
  18.         stackDataStructure.pop();
  19.         stackDataStructure.print();
  20.         stackDataStructure.pop();
  21.         stackDataStructure.print();
  22.  
  23.         StackDataStructure<String> stringstack = new StackDataStructure<>();
  24.         stringstack.push("Amma");
  25.         stringstack.push("Thaththa");
  26.         stringstack.push("Baba");
  27.         stringstack.print();
  28.  
  29.         System.out.println("Testing Iterable");
  30.         for (StackNode node : stringstack) {
  31.             System.out.println(node.value);
  32.         }
  33.  
  34. //        StackNode<String> s1 = new StackNode<>();
  35. //        s1.value = "A";
  36. //        StackNode<String> s2 = new StackNode<>();
  37. //        s2.value = "B";
  38. //        StackNode<String> s3 = new StackNode<>();
  39. //        s3.value = "C";
  40. //        s1.next = s2;
  41. //        s2.next = s3;
  42. //        for(String s:s1){
  43. //
  44. //        }
  45.  
  46.     }
  47. }
  48.  
  49. class StackDataStructure<T> implements Iterable<StackNode> {
  50.     StackNode<T> tail;
  51.     StackNode<T> head;
  52.  
  53.     void push(T value) {
  54.         if (head == null) {
  55.             head = new StackNode();
  56.             head.value = value;
  57.             head.next = null;
  58.             tail = head;
  59.             tail.previous = null;
  60.         } else {
  61.             tail.next = new StackNode();
  62.             tail.next.value = value;
  63.             tail.next.previous = tail;
  64.             tail.next.next = null;
  65.             tail = tail.next;
  66.         }
  67.     }
  68.  
  69.     StackNode pop() {
  70.         if (head == null) {
  71.             return null;
  72.         } else {
  73.             StackNode node = tail;
  74.             if (tail.previous == null) {
  75.                 head = null;
  76.                 tail = null;
  77.                 return node;
  78.             }
  79.             tail = tail.previous;
  80.             tail.next = null;
  81.             return node;
  82.         }
  83.     }
  84.  
  85.     void print() {
  86.         System.out.println("Printing Stack");
  87.         if (head == null) {
  88.             System.out.println("Stack is Empty");
  89.         } else {
  90.             StackNode node = head;
  91.             while (node != null) {
  92.                 System.out.print(node.value + " ");
  93.                 node = node.next;
  94.             }
  95.         }
  96.         System.out.println("\nPrinting Stack Finished");
  97.     }
  98.  
  99.     @Override
  100.     public Iterator<StackNode> iterator() {
  101.         return new StackNodeIterator(head);
  102.     }
  103. }
  104.  
  105. class StackNode<T> {
  106.     T value;
  107.     StackNode next;
  108.     StackNode previous;
  109.  
  110.     public StackNode getNext() {
  111.         return next;
  112.     }
  113. }
  114.  
  115. class StackNodeIterator implements Iterator<StackNode> {
  116.  
  117.     StackNode next;
  118.  
  119.     public StackNodeIterator(StackNode next) {
  120.         this.next = next;
  121.     }
  122.  
  123.     @Override
  124.     public boolean hasNext() {
  125.         return (next != null);
  126.     }
  127.  
  128.     @Override
  129.     public StackNode next() {
  130.         StackNode ret = next;
  131.         next = next.next;
  132.         return ret;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement