Jag behöver hjälp med java LinkedList implements Stack.
Jag försöker lösa en lab men har fastnat nu en hel dag på det här. Jag förstår inte varför jag få fel meddelande
"class 'LinkedList' must be either declared abstract or implement abstract method 'push(T)' in Stack"
kan någon hjälpa mig?
PS jag har också problem med min @Override, den säger att den inte "overrida" min push metod, antar att om första problem löses så kanske den andra funkar också, eller?
public interface Stack {
void push(T o);
T pop();
T top();
int size();
boolean isEmpty();
}
/**
* A singly linked list.
*
*/
import java.util.*;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
public class LinkedList<T> implements Stack {
private ListElement<T> first; // Points to the first element on the list.
private ListElement<T> last; // Points to the last element on the list.
private int size; // Number of elements in list.
/**
* A list element.
*/
private static class ListElement<T> {
public T data;
public ListElement<T> next;
public ListElement(T data) {
this.data = data;
this.next = null;
}
}
/**
* Creates an empty list.
*/
public LinkedList() {
// TODO "Done"
first = last = null;
size = 0;
}
public static void main(String[] args){
}
/**
* Inserts the given element at the beginning of this list.
*
* @param element An element to insert into the list.
*/
public void addFirst(T element) {
// TODO "Done"
first = last= new ListElement<T>(element);
last.next = null;
}
/**
* Inserts the given element at the end of this list.
*
* @param element An element to insert into the list.
*/
public void addLast(T element) {
// TODO "Done"
if( first == null) {
addFirst(element);
}
else
{
ListElement<T> temp = last;
last = new ListElement<T>(element);
temp.next = last;
last.next = null;
}
}
/**
* @return The head of the list.
* @throws NoSuchElementException if the list is empty.
*/
public T getFirst() {
// TODO "Done"
if(first == null) throw new NoSuchElementException();
return first.data;
}
/**
* @return The tail of the list.
* @throws NoSuchElementException if the list is empty.
*/
public T getLast() {
// TODO "Done"
if(last == null) throw new NoSuchElementException();
return last.data;
}
/**
* Returns an element from a specified index.
*
* @param index A list index.
* @return The element at the specified index.
* @throws IndexOutOfBoundsException if the index is out of bounds.
*/
public T get(int index) {
// TODO "Done"
if (index < 0 || index >= size()) throw new IndexOutOfBoundsException();
ListElement<T> temp = first;
for (int i = 0; i < index; i++) temp = temp.next;
return temp.data;
}
/**
* Removes the first element from the list.
*
* @return The removed element.
* @throws NoSuchElementException if the list is empty.
*/
public T removeFirst() {
// TODO "Done"
if(first == null) throw new NoSuchElementException();
else if (first.next == null) {
first =last= null;
return null;
}
else {
ListElement<T> temp = first;
first = first.next;
return temp.data;
}
}
/**
* Removes all of the elements from the list.
*/
public void clear() {
// TODO "Done"
first = last = null;
}
// Adds the element to the top of the stack.
@Override
public void push(T o) {
addLast(o);
}
/**Removes and returns the top element in the stack, that is the element
* that was last added to the stack. Throws an EmptyStackException
* (you need to import this, see the docs!) if the stack is empty!
* @return
*/
@Override
public T pop() {
T temp = last.data;
for(int i = 1; i < size;i ++){
last = first.next;
}
size --;
return temp;
}
/** Returns the top element in the stack without removing it. Throws an EmptyStackException (you need to
* import this, see the docs!) if the stack is empty!
* @return
*/
@Override
public T top() {
if (first == null);
throw new EmptyStackException();
else{
return last.data;
}
}
/**
* @return The number of elements in the list.
*/
public int size() {
// TODO "Done"
if( first == null)
size = 0;
else{
size++;
ListElement<T> temp = first;
while(temp.next != null) {
temp = temp.next;
size ++;
}
}
return size;
}
/**
* Note that by definition, the list is empty if both first and last
* are null, regardless of what value the size field holds (it should
* be 0, otherwise something is wrong).
*
* @return <code>true</code> if this list contains no elements.
*/
public boolean isEmpty() {
return first == null && last == null;
}
/**
* Creates a string representation of this list. The string
* representation consists of a list of the elements enclosed in
* square brackets ("[]"). Adjacent elements are separated by the
* characters ", " (comma and space). Elements are converted to
* strings by the method toString() inherited from Object.
*
* Examples:
* "[1, 4, 2, 3, 44]"
* "[]"
*
* @return A string representing the list.
*/
public String toString() {
// TODO
String list = "";
ListElement<T> current = first;
while(current.next != null){
list += current.next + ",";
}
return list;
}
}