Name: Nguyen Huu Thang ID Student: GCD201443 Class: GCD1001 Lap 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Name: Nguyen Huu Thang

ID Student: GCD201443
Class: GCD1001

LAP 1

package implementations;

import interfaces.List;

import java.util.Arrays;
import java.util.Iterator;

public class ArrayList<E> implements List<E> {


private static final int DEFAULT_CAPACITY = 4;
private Object[] elements;
private int size;

public ArrayList(){
elements = new Object[DEFAULT_CAPACITY];
}
@Override
public boolean add(E element) {
if (elements.length == size){
elements = grow();
}
elements[size++] = element;
return true;
}
private Object[] grow(){
return Arrays.copyOf(elements, elements.length*2);
}

@Override
public boolean add(int index, E element) {
checkIndex(index);
if (elements.length ==size){
elements = grow();
}
for (int i = size;i>=index +1;i--){
elements[i] =elements[i-1];
}
elements[index]= element;
size++;
return true;
}
private void checkIndex(int index){
if (index<0 ||index>=size) throw new IndexOutOfBoundsException("Index
out of bound:"
+index +" out of "+size);
}
private E getElement(int index){
return (E)elements[index];
}
@Override
public E get(int index) {
checkIndex(index);
return getElement(index);
}

@Override
public E set(int index, E element) {
checkIndex(index);
elements[index] = element;
return null;
}
private Object[] shrink(){
return Arrays.copyOf(elements,elements.length/2);
}
private void ensureCapacity(){
if (size < elements.length/3) {
elements = shrink();
}

}
public int getCapacity(){
return elements.length;
}
@Override
public E remove(int index) {
checkIndex(index);
E element = getElement(index);
for (int i=index;i<size-1;i++){
elements[i] = elements[i+1];
}
size--;
ensureCapacity();
return element;
}

@Override
public int size() {
return size;
}
public String toString(){
StringBuilder result = new StringBuilder();
// result.append();
for (int i = 0;i<size;i++){
if (i==size-1) result.append(elements[i]);
else result.append(elements[i]+"\n");
}
return result.toString();
}
@Override
public int indexOf(E element) {
for (int i=0;i<size;i++){
if (elements[i].equals(element)){
return i;
}
}
return -1;
}

@Override
public boolean contains(E element) {
return indexOf(element) !=-1;
}

@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
private int index = 0;
@Override
public boolean hasNext() {
return index<size;
}

@Override
public E next() {
E element = getElement(index);
index++;
return element;
}
};
}
}

package implementations;

import interfaces.AbstractStack;

import java.util.Iterator;

public class Stack<E> implements AbstractStack<E> {


private Node<E> top;
private int size;
private static class Node<E>{
E element;
Node<E> previous;
public Node( E element){
this(element,null);
}
public Node(E element, Node<E> previous){
this.element = element;
this.previous = previous;
}
}
public Stack(){
}

@Override
public void push(E element) {
Node<E> newNode = new Node<>(element);
newNode.previous = top;
top = newNode;
size++;
}

@Override
public E pop() {
ensureNonEmpty();
E data = top.element;
top = top.previous;
size--;
return data;
}

private void ensureNonEmpty() {


if (size == 0) throw new IllegalStateException("Stack is Empty!!! Can
not pop!");
}

@Override
public String toString() {
Node<E> current = top;
StringBuilder result = new StringBuilder();
while(current!=null){
result.append(current.element+ " ");
current = current.previous;
}
return result.toString();
}

@Override
public E peek() {
return null;
}

@Override
public int size() {
return size;
}

@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
Node<E> current = top;
@Override
public boolean hasNext() {
return current!= null;
}

@Override
public E next() {
E element = current.element;
current = current.previous;
return element;
}
};
}
}

import implementations.*;

import java.util.Iterator;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
testArrayList();
testStack();

}
public static void testArrayList(){
ArrayList<Integer> myArrayList = new ArrayList<>();
myArrayList.add(3);
myArrayList.add(3);
myArrayList.add(4);
myArrayList.add(5);
myArrayList.add(3);

System.out.println(myArrayList);
myArrayList.add(2,8);
myArrayList.set(4,10);
System.out.println("After adding:");
System.out.println(myArrayList);
System.out.println("Index of 5 is "+ myArrayList.indexOf(10));
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");
System.out.println("Delete element "+ myArrayList.remove(1) + " out
of Arraylist");

System.out.println(myArrayList);
System.out.println("Capacity of the Arraylist "+
myArrayList.getCapacity());

ArrayList<String> myStringArrayList = new ArrayList<>();


myStringArrayList.add("Hello");
myStringArrayList.add("Welcome");
myStringArrayList.add("Goodbye");
myStringArrayList.add("See you");
myStringArrayList.add("International");
myStringArrayList.add("Student");
System.out.println(myStringArrayList);
System.out.println("Index of 5 Welcome "+
myStringArrayList.indexOf("Welcome"));
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string you want to search:");
String input=sc.next();
System.out.println("Index of "+input+" is "+
myStringArrayList.indexOf(input));
ArrayList<Student> myStudentList = new ArrayList<Student>();
Student student2 = new Student(1, "Huynh Minh Huy", 19, 7.0);
Student student3 = new Student(4, "Vo Anh Tu", 20, 10.0);
GreatStudent student4 = new GreatStudent(1,"Nguyen Tan Minh Duc", 29,
10.0,"Boi loi");
GreatStudent student5 = new GreatStudent(19,"Tran Duc Linh", 22,
10.0,"Toan");
myStudentList.add(student2);
myStudentList.add(student3);
myStudentList.add(student4);
myStudentList.add(student5);
System.out.println(myStudentList);
System.out.println("Index of Student with ID 4 is" +
myStudentList.indexOf(new Student(4)));
Iterator<Integer> myIntIterator = myArrayList.iterator();
while(myIntIterator.hasNext()){
System.out.println(myIntIterator.next());
}
}

public static void testStack(){


Stack2<Integer> myIntegerStack = new Stack2<>();
myIntegerStack.push(3);
myIntegerStack.push(4);
myIntegerStack.push(7);
myIntegerStack.push(5);
System.out.println(myIntegerStack);
System.out.println("Pop element from stack:");
System.out.println(myIntegerStack.pop());
System.out.println(myIntegerStack.pop());
System.out.println("The stack after pop:");
System.out.println(myIntegerStack);

Iterator<Integer> myIntIterator = myIntegerStack.iterator();


while(myIntIterator.hasNext()){
System.out.println(myIntIterator.next());
}
Stack2<Student> myStudentStack = new Stack2<>();
Student student2 = new Student(1, "Huynh Minh Huy", 19, 7.0);
Student student3 = new Student(4, "Vo Anh Tu", 20, 10.0);
GreatStudent student4 = new GreatStudent(1,"Nguyen Tan Minh Duc", 29,
10.0,"Boi loi");
GreatStudent student5 = new GreatStudent(19,"Tran Duc Linh", 22,
10.0,"Toan");
myStudentStack.push(student2);
myStudentStack.push(student3);
myStudentStack.push(student4);
myStudentStack.push(student5);
System.out.println(myStudentStack);
}
}

You might also like