public class MyList<E> {
private int size = 0;
private static final int DEFAULT_CAPACITY = 10;
private Object elements[];
public MyList() {
elements = new Object[DEFAULT_CAPACITY];
}
public void add(E e) {
if (size == elements.length) {
ensureCapa();
}
elements[size++] = e;
}
private void ensureCapa() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
@SuppressWarnings("unchecked")
public E get(int i) {
if (i>= size || i <0) {
throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
}
return (E) elements[i];
}
}
class Node {
Node next = null;
int data;
public Node(int d) {
data = d;
}
void appendToTail(int d) {
Node end = new Node(d);
Node n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
}
}
Node deleteNode(Node head, int d) {
Node n = head;
if (n.data == d) {
return head.next; /* moved head */
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head; /* head didn’t change */
}
n = n.next;
}
}
public class Stack{
Node top;
public void add(int a){
if (top == null){
top = new Node(a);
}else{
top.next = top;
top.value = a;
}
}
public void del(int a){
Node2 current = top ;
//if head
if (top.value == a){
top = top.next;
return;
}
while(current.next != null){
if(current.next.value== a){
current.next= current.next.next;
return;
}
current = current.next;
}
}
public void loop(){
if (top == null){return;};
Node2 current = top;
while(current != null){
System.out.println(current.value);
current = current.next;
}
}
}
public class Queue{
Node head;
public void add(int a){
Node2 nodeToAdd = new Node(a);
if (head == null){
head = nodeToAdd;
}else{
Node2 current = head ;
while(current != null){
if(current.next == null){
current.next = nodeToAdd ;
return;
}else{
current= current.next;
}
}
}
}
public void del(int a){
Node2 current = head ;
//if head
if (head.value == a){
head = head.next;
}
while(current.next != null){
if(current.next.value== a){
current.next= current.next.next;
return;
}
}
}
public void loop(){
if (head == null){return;};
Node2 current = head;
while(current != null){
System.out.println(current.value);
current = current.next;
}
}
}