0 Daumen
438 Aufrufe

Sehr geehrte Community,

bei der anderen Frage, zu dieser Thematik, habe ich leider nicht meine Antwort gefunden. Ich entschuldige mich, falls es doch irgendwo vorhanden ist und ich es nicht fand. Achtung wird recht lang. :)

Die Aufgabe ist die Implementierung einer verketteten Liste. Zusätzlich zu der Verkettung mit direkten Nachbarn ist jeder Knoten auch noch mit – soweit vorhanden – seinem achtnächsten Nachbarn verbunden (zur Verdeutlichung ist eine der Abkürzungskanten exemplarisch hervorgehoben) -> Skizze im Bild.

Implementieren Sie die oben abgebildete Listenstruktur, bestehend aus einer Klasse SpeedList<T> (die eine Schnittstelle ISpeedList implementiert) und einer dazugehörigen Knotenklasse. In ihrer Implementierung sollen, wann immer möglich und sinnvoll, die Abkürzungskanten verwendet werden, um die Navigation innerhalb der Liste zu beschleunigen. Die Schnittstelle sowie die zu implementierende Klasse sind im Eclipse-Projekt zu dieser Aufgabe enthalten.



Folgendes ist schon gegeben:

/**
* A simple list interface
* @param <T> The type of list element
*/
public interface ISpeedList<T> {

/**
* Returns the current number of elements in the list
*
* @return Current number of elements in the list
*/
public int size();

/**
* Inserts an element at the beginning of the list
*
* @param item Item to be inserted
*/
public void prepend(T t);

/**
* Returns the element at the specified position in the list
*
* @param pos The position of the element in the list starting from 0
*
* @return The specified element in the list
*
* @throws IndexOutOfBoundsException If the requested element is out of
* range
*/
public T getElementAt(int pos);

/**
* Returns the next 8th element of the specified element in the list
*
* @param pos The position of the specified element in the list starting
* from 0
*
* @return The next 8th element of the specified element
*
* @throws IndexOutOfBoundsException If the requested element is out of
* range
*/
public T getNext8thElementOf(int pos);

}


Hier soll alles implementiert werden

public class SpeedList<T> implements ISpeedList<T> {

@Override
public int size() {

return 0;
}

@Override
public void prepend(T t) {

}

@Override
public T getElementAt(int pos) {

return null;
}

@Override
public T getNext8thElementOf(int pos) {

return null;
}

}

Ich habe mir den Code bzw. die Seite von Codeadventurers angeschaut, doch werde nicht ganz schlau daraus, auch nicht von JavaBeginners. Daher bitte ich um Hilfe.

Avatar von

1 Antwort

0 Daumen

Ich habe nun folgendes :)

Ich bitte um Hilfe für getElementAt und getNext8thElementOf




public class SpeedList<T> implements ISpeedList<T> {

private int counter = 0;

class Node {

// Attributes for Node
private T obj;
private Node next;

// Constructor for Node
public Node(T o, Node n) {
obj = o;
next = n;
}

public Node() {
obj = null;
next = null;
}

// Methods

public void setElement(T o) {
obj = o;
}

public T getElement() {
return obj;
}

public void setNext(Node n) {
next = n;
}

public Node getNext() {
return next;
}

}

// Attributes for the List
private Node head;

// Constructor for the List
public SpeedList() {
head = new Node();
}

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

@Override
public void prepend(T t) {

// Add new Node behind head
Node n = new Node(t, head.getNext());
head.setNext(n);
counter++;

}

@Override
public T getElementAt(int pos) {

Node n = head;


return null;
}

@Override
public T getNext8thElementOf(int pos) {
return this.getNext8thElementOf(8);
}

}


Avatar von

Ein anderes Problem?

Stell deine Frage

Willkommen bei der Stacklounge! Stell deine Frage einfach und kostenlos

x
Made by a lovely community