package colletion.arrlist;
import java.util.ArrayList;
class ArrayStack<E> {
ArrayList<E> arrayStack = new ArrayList<>();
public void push(E e) {
arrayStack.add(e);
}
public E pop() {
if(arrayStack.isEmpty()){
E e = null;
return e;
}
return arrayStack.remove(arrayStack.size()- 1);
}
}
public class ArrayStackTest {
public static void main(String[] args) {
ArrayStack<Integer> intStack = new ArrayStack<>();
intStack.push(1);
intStack.push(2);
intStack.push(3);
intStack.push(4);
System.out.println(intStack.pop());
System.out.println(intStack.pop());
System.out.println(intStack.pop());
System.out.println(intStack.pop());
System.out.println(intStack.pop());
}
}
👉 실행화면
4
3
2
1
null
package colletion.arrlist;
import java.util.LinkedList;
class LinkedStack<E> {
LinkedList<E> linkedSatck = new LinkedList<>();
public void push (E e) {
linkedSatck.addLast(e);
}
public E pop() {
if(linkedSatck.isEmpty()){
E e = null;
return e;
}
return linkedSatck.removeLast();
}
}
public class LinkedStackTest {
public static void main(String[] args) {
LinkedStack<Integer> intStack = new LinkedStack<>();
intStack.push(1);
intStack.push(2);
intStack.push(3);
intStack.push(4);
System.out.println(intStack.pop());
System.out.println(intStack.pop());
System.out.println(intStack.pop());
System.out.println(intStack.pop());
System.out.println(intStack.pop());
}
}
👉 실행결과
4
3
2
1
null
package colletion.arrlist;
import java.util.ArrayList;
class MyQueue<E> {
private ArrayList<E> arrayQueue = new ArrayList<>();
public void enQueue(E data) {
arrayQueue.add(data);
}
public E deQueue( ) {
int len = arrayQueue.size();
if(len ==0) {
System.out.println("큐가 비었습니다.");
E e = null;
return e;
}
return (arrayQueue.remove(0));
}
}
public class QueueTest {
public static void main(String[] args) {
MyQueue<Integer> intQueue = new MyQueue<>();
intQueue.enQueue(1);
intQueue.enQueue(2);
intQueue.enQueue(3);
intQueue.enQueue(4);
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
}
}
👉 실행화면
1
2
3
4
큐가 비었습니다.
null
package colletion.arrlist;
import java.util.LinkedList;
class LinkedMyQueue<E> {
private LinkedList<E> arrayQueue = new LinkedList<>();
public void enQueue(E data) {
arrayQueue.addLast(data);
}
public E deQueue() {
if (arrayQueue.isEmpty()) {
E e = null;
return e;
}
return arrayQueue.remove();
}
}
public class LinkedQueueTest {
public static void main(String[] args) {
LinkedMyQueue<Integer> intQueue = new LinkedMyQueue<>();
intQueue.enQueue(1);
intQueue.enQueue(2);
intQueue.enQueue(3);
intQueue.enQueue(4);
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
}
}
👉 실행결과
1
2
3
4
null