Last active
December 19, 2021 10:26
-
-
Save sunmeat/7ef041f887082d1bdba63f8973605d1e to your computer and use it in GitHub Desktop.
arraylist based stack [not finished!]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package collections; | |
class MyArrayList { // этот класс необходимо полностью заменить на вашу реализацию эррейлиста!!! | |
private int size = 0; | |
private int[] data; | |
public MyArrayList() { | |
this(10); | |
} | |
public MyArrayList(int capacity) { | |
if (capacity < 10) { | |
capacity = 10; | |
} | |
data = new int[capacity]; | |
} | |
public void pushBack(int value) { | |
// ensureCapacitу... | |
data[size++] = value; | |
} | |
public void pushFront(int value) { | |
// ensureCapacity... | |
for (int i = size; i > 0; i--) { | |
data[i] = data[i - 1]; | |
} | |
data[0] = value; | |
size++; | |
} | |
public void clear() { | |
for (int i = 0; i < size; i++) { | |
data[i] = 0; | |
} | |
size = 0; | |
} | |
public boolean isEmpty() { | |
return size == 0; | |
} | |
public int getSize() { | |
return size; | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
class Stack { | |
private MyArrayList data; | |
public Stack() { | |
data = new MyArrayList(); | |
} | |
public void clear() { | |
data.clear(); | |
} | |
public boolean isEmpty() { | |
return data.getSize() == 0; | |
} | |
public int getCount() { | |
return data.getSize(); | |
} | |
// заталкивание | |
public void push(int value) { | |
data.pushBack(value); | |
} | |
// выталкивание | |
public int pop() { | |
// data.popBack(); // реализации этого метода в моём примере нет... поэтому, возвращается -1. | |
return -1; | |
} | |
} | |
/////////////////////////////////////////////////////////////////////// | |
class Program { | |
public static void main(String[] args) { | |
Stack st = new Stack(); | |
st.push(10); | |
st.push(20); | |
st.push(30); | |
System.out.println(st.pop()); | |
int temp = st.pop(); | |
System.out.println(temp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment