Advertisement
MnMWizard

StackClass

Apr 16th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. //Mason Marnell
  2. //Stack class
  3.  
  4.  
  5.  
  6.     public class Stack{
  7.        
  8.     Node node;
  9.    
  10.     public Stack(String value){
  11.         node = new Node();
  12.         node.prev = null;
  13.         node.value = value;
  14.     }
  15.    
  16.     public void push(String value){
  17.         Node temp = new Node();
  18.         if(node==null){
  19.             node = new Node();
  20.             node.value = value;
  21.             return;
  22.         }
  23.         temp.value = value;
  24.         temp.prev = node;
  25.         node = temp;
  26.     }
  27.    
  28.     public String pop(){
  29.         if(node==null)
  30.         return null;
  31.         String value = node.value;
  32.         node = node.prev;
  33.         return value;
  34.     }
  35.    
  36.     public String peek(){
  37.         return node.value;
  38.     }
  39.    
  40.     public String toString(){
  41.         Node temp = node;
  42.         String info = "";
  43.         while(temp.prev != null){
  44.             info += temp.value + " ";
  45.             temp = temp.prev;
  46.         }
  47.         info += temp.value +" ";
  48.         return info;
  49.         }
  50.     }
  51. //--------------------------------------------------------------------------------------------------------------------------------
  52. //StackClass class
  53.  
  54.  
  55. public class StackClass {
  56.    
  57.     public static void main(String[] args){
  58.         Stack B = new Stack("house");
  59.         B.push("Cat");
  60.         System.out.println(B.pop());
  61.         B.push("dog");
  62.         B.push("horse");
  63.         B.push("hamburger");
  64.         System.out.println(B);
  65.         System.out.println(B.pop());
  66.         System.out.println(B.pop());
  67.         System.out.println(B.pop());
  68.         System.out.println(B.pop());
  69.         System.out.println(B.pop());
  70.         System.out.println(B.pop());
  71.     }
  72.    
  73.    
  74. }
  75.  
  76. //----------------------------------------------------------------------------------------------------------------------------
  77. //Node class
  78.  
  79. public class Node {
  80.     String value;
  81.     Node prev;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement