Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StackInterface;
- public interface Stack {
- public void push (int v);
- public int pop();
- public int top();
- public boolean full();
- public boolean empty();
- }
- ///
- package MainPack;
- public class Stack {
- public static void main(String[] args) {
- int n = 5, p = 7;
- StackInterface.Stack[] stk = new StackInterface.Stack[3];
- stk[0] = new FixedStack.Stack(n);
- stk[1] = new DynStack.Stack(n);
- stk[2] = new ListStack.Stack();
- for (int i = 0; i < 3; i++) {
- // перебирает три массива
- for (int j=1; !stk[i].full() && j<=p ;j++) {
- stk[i].push(j);
- }
- while (!stk[i].empty())
- System.out.print(stk[i].pop());
- System.out.println(" ");
- }
- System.out.println(" ");
- }
- }
- ///
- package ListStack;
- public class Stack implements StackInterface.Stack {
- private static class Node
- {
- int value;
- Node next;
- Node(int v, Node n) {
- value = v;
- next = n;
- }
- }
- private Node head;
- public Stack () {
- head = null;
- }
- public void push (int k)
- {
- head = new Node(k, head);
- }
- public int pop()
- {
- int k = head.value;
- head = head.next;
- return k;
- }
- public int top()
- {
- return head.value;
- }
- public boolean full()
- {
- return false;
- }
- public boolean empty()
- {
- return head == null;
- }
- }
- ///
- package FixedStack;
- public class Stack implements StackInterface.Stack{
- private int s[];
- private int up;
- public Stack (int v)
- {
- s=new int [v];
- up=-1;
- }
- public void push (int k)
- {
- s[++up]=k;
- }
- public int pop()
- {
- return s[up--];
- }
- public int top()
- {
- return s[up];
- }
- public boolean full()
- {
- return up==s.length-1;
- }
- public boolean empty()
- {
- return up==-1;
- }
- }
- ///
- package DynStack;
- public class Stack implements StackInterface.Stack{
- private int s[];
- private int up;
- public Stack (int v)
- {
- s=new int [v];
- up=-1;
- }
- public void push (int v)
- {
- s[++up]=v;
- }
- public int pop()
- {
- return s[up--];
- }
- public int top()
- {
- return s[up];
- }
- public boolean full()
- {
- if (up==s.length-1)
- {
- int[] t = new int[s.length * 2];
- int i;
- for (i = 0; i < s.length; i++)
- t[i] = s[i];
- s = t;
- }
- return false;
- }
- public boolean empty()
- {
- return up==-1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement