Please Implement below method and write a class to test the result. import java.
ID: 3726435 • Letter: P
Question
Please Implement below method and write a class to test the result.
import java.util.Iterator;
import net.datastructures.*;
public class MyArrayPositionalList<E> implements PositionalList<E>{
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public Position<E> first() {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> last() {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> before(Position<E> p) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> after(Position<E> p) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> addFirst(E e) {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> addLast(E e) {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> addBefore(Position<E> p, E e) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> addAfter(Position<E> p, E e) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public E set(Position<E> p, E e) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public E remove(Position<E> p) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterator<Position<E>> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterable<Position<E>> positions() {
// TODO Auto-generated method stub
return null;
}
}
Explanation / Answer
public class MyArrayStack<E> extends ArrayList<E> implements Stack<E> {
public MyArrayStack() {
super();
}
Public MyArrayStack(int initialsize) {
Super(initialsize);
@Override
public void push(E e) {
add(e);
Return e;
}
@Override
public E top() {
int n = size();
if (n<=0){
return null;
else {
return get(n-1);
}
}
@Override
public E pop() {
int n= size();
if ( n<=0){
return null;
} else return remove(n-1);
}
}
@Override
public int size() {
return super.size();
}
@Override
public boolean isEmpty() {
return super.isEmpty();
}
}
2. The MyLinkedListQueue can be implemented by extending LinkedList by the code given below :
import java.util.LinkedList;
import net.datastructures.*;
public class MyLinkedListQueue<E> extends LinkedList<E> implements
Queue<E>{
@Override
public void enqueue(E e) {
Node<E> node = new Node<E> (e);
if (isEmpty())
front = node;
else
rear.setNext (node);
rear = node;
count++;
}
@Override
public E first() throws QueueEmptyException {
if(isEmpty())
throw new QueueEmptyException(“Queue”);
return front.getElement();
}
@Override
public E dequeue() throws QueueEmptyException {
if(isEmpty())
throw new QueueEmptyException(“Queue”);
E result = front.getElement();
front = front.getNext();
count--;
if (isEmpty())
rear=null;
return result;
}
@Override
public int size() {
return count();
}
@Override
public boolean isEmpty() {
return (front==rear);
}
}