Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package intStack;
- public interface Stack {
- public void push(int v);
- public int pop();
- public int top();
- public boolean full();
- public boolean empty();
- }
- package fixedStack;
- public class Stack implements intStack.Stack {
- private int[] s;
- private int up;
- public Stack(int size) {
- s = new int[size];
- 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() {
- return up == s.length-1;
- }
- public boolean empty() {
- return up == -1;
- }
- }
- package dynStack;
- public class Stack implements intStack.Stack {
- private int[] s;
- private int up;
- public Stack(int size) {
- s = new int[size];
- 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;
- }
- }
- package listStack;
- import java.util.Random;
- public class Stack implements intStack.Stack {
- private int[] s;
- private int up;
- public Stack(int size) {
- s = new int[size];
- up = -1;
- for (int i = 0; i < size; i++) {
- pushRandom();
- }
- }
- public void pushRandom() {
- Random obj = new Random();
- push(obj.nextInt() % 100);
- }
- public void push(int v) {
- s[++up] = v;
- }
- 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 test;
- import intStack.Stack;
- public class TestInkStack {
- public static void main(String[] args) {
- Stack[] stk = new Stack[3];
- stk[0] = new fixedStack.Stack(5);
- stk[1] = new dynStack.Stack(5);
- stk[2] = new listStack.Stack(5);
- int i, j;
- for (i = 0; i < 2; i++) {
- for (j = 1; j <= 10; j++) {
- if (stk[i].full())
- break;
- stk[i].push(j);
- }
- }
- for (i = 0; i < 3; i++) {
- while (!stk[i].empty())
- System.out.print(stk[i].pop()+ " ");
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement