Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. package StackInterface;
  2.  
  3. public interface Stack {
  4.         public void push (int v);
  5.         public int pop();
  6.         public int top();
  7.         public boolean full();
  8.         public boolean empty();
  9.     }
  10.  
  11. ///
  12.  
  13. package MainPack;
  14.  
  15. public class Stack {
  16.     public static void main(String[] args) {
  17.         int n = 5, p = 7;
  18.         StackInterface.Stack[] stk = new StackInterface.Stack[3];
  19.         stk[0] = new FixedStack.Stack(n);
  20.         stk[1] = new DynStack.Stack(n);
  21.         stk[2] = new ListStack.Stack();
  22.  
  23.     for (int i = 0; i < 3; i++) {
  24.         // перебирает три массива
  25.         for (int j=1; !stk[i].full() &&  j<=p ;j++) {
  26.             stk[i].push(j);
  27.         }
  28.         while (!stk[i].empty())
  29.             System.out.print(stk[i].pop());
  30.             System.out.println(" ");
  31.  
  32.         }
  33.         System.out.println(" ");
  34.                 }
  35.  
  36.         }
  37.  
  38. ///
  39.  
  40. package ListStack;
  41.  
  42. public class Stack implements StackInterface.Stack {
  43.     private static class Node
  44.     {
  45.         int value;
  46.         Node next;
  47.         Node(int v, Node n) {
  48.             value = v;
  49.             next = n;
  50.         }
  51.     }
  52.     private Node head;
  53.     public Stack () {
  54.         head = null;
  55.     }
  56.     public void push (int k)
  57.     {
  58.         head = new Node(k, head);
  59.     }
  60.     public int pop()
  61.     {
  62.         int k = head.value;
  63.         head = head.next;
  64.         return k;
  65.     }
  66.     public int top()
  67.     {
  68.         return head.value;
  69.     }
  70.     public boolean full()
  71.     {
  72.         return false;
  73.     }
  74.     public boolean empty()
  75.     {
  76.         return head == null;
  77.     }
  78. }
  79.  
  80. ///
  81.  
  82. package FixedStack;
  83.  
  84.     public class Stack implements StackInterface.Stack{
  85.         private int s[];
  86.         private int up;
  87.         public Stack (int v)
  88.         {
  89.             s=new int [v];
  90.             up=-1;
  91.         }
  92.         public void push (int k)
  93.         {
  94.             s[++up]=k;
  95.         }
  96.         public int pop()
  97.         {
  98.             return s[up--];
  99.         }
  100.         public int top()
  101.         {
  102.             return s[up];
  103.         }
  104.         public boolean full()
  105.         {
  106.             return up==s.length-1;
  107.         }
  108.         public boolean empty()
  109.         {
  110.             return up==-1;
  111.         }
  112.     }
  113.  
  114. ///
  115.  
  116. package DynStack;
  117.     public class Stack implements StackInterface.Stack{
  118.         private int s[];
  119.         private int up;
  120.         public Stack (int v)
  121.         {
  122.             s=new int [v];
  123.             up=-1;
  124.         }
  125.         public void push (int v)
  126.         {
  127.             s[++up]=v;
  128.         }
  129.         public int pop()
  130.         {
  131.             return s[up--];
  132.         }
  133.         public int top()
  134.         {
  135.             return s[up];
  136.         }
  137.         public boolean full()
  138.         {
  139.             if (up==s.length-1)
  140.             {
  141.                 int[] t = new int[s.length * 2];
  142.                 int i;
  143.                 for (i = 0; i < s.length; i++)
  144.                     t[i] = s[i];
  145.                 s = t;
  146.             }
  147.             return false;
  148.         }
  149.         public boolean empty()
  150.         {
  151.             return up==-1;
  152.         }
  153.  
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement