Klassen Innere Klassen, verkettete Liste

atomvirus

Mitglied
Hallo zusammen!
Bin gerade dabei eine vergangenes Beispiel zu verarbeiten...
Nur ist mir das System mit den Inneren Klassen, also jetzt die Knoten und das Zusammenspiel nicht mehr klar... Will schlussendlich auf eine destruktive liste umstellen, nur fehlt mir der ansatz was ich dazu alles brauch, bzw. das System...

Java:
public class IntList {
	private class IntNode { // Innere Klasse für die Knoten

		int data; // Int - Variable zum speichern des Integers (Objekt)
		IntNode link; //

		IntNode() { // default Konstruktor
		}

		IntNode(int i, IntNode n) { // Konstruktor m. Parameterübergabe
		}
	}

	private IntNode start; // Instanzvariable start

	public IntList() { // Parameterloser Konstruktor
		start = null;
	}

	private IntList(IntNode n) { // nicht public?!
		start = n;
	}

	public IntList add(int i) {
		return new IntList(new IntNode(i, start));
	}

	public IntList tail() {
		return new IntList(start.link);
	}

	public int head() {
		return start.data;
	}

	public boolean notEmpty() {
		return start != null;
	}
}
 
Was ist dir denn nicht klar? Sieht wie ein immutabler verketteter Stack aus, der nur ein Element enthalten können soll und mit sich selbst verkettet ist. ???:L Und was ist eine "destruktive liste"? Eine, die nicht das macht, was man von ihr verlangt? Google spuckt mir da garnichts aus.
 
ja eben, mir ist dieses Bsp. auch nicht mehr klar...
im endeffekt will i aus dieser Liste, oder halt selbst eine StringList erstellen... Aber eben nicht non-destruktiv, sprich das jedes mal wenn ein neuer Knoten dazu kommt eine neue liste erstellt wird, sondern das einfach ein knoten hintenangefügt wird, sprich destruktiv... Ist so ein typischen Beispiel für verkettete Listen u. innere Klassen, nur steh i da schon ein bisschen daneben ???:L
 
Ach so, dann ist destruktiv wohl das selbe wie mutable. Kenne es nur als Gegenteil von konstruktiv.

Habs dir mal runtergetippt. Ist ungetestet, wie ich mich kenne mindestens 3 Bugs, eher 5. Könnten auch noch mehr sein. Ich denke aber nicht, dass dir das im Endeffekt sehr viel hilft, stell einfach deine Fragen. Die langweiligen Methoden, die man nicht so oft braucht habe ich ausgelassen.

Java:
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Liste<T> implements List<T>
{
	private int size;
	private Node first;
	
	public Liste()
	{
		super();
	}
	 
	public int size()
	{
		return(size);
	}
	public boolean isEmpty()
	{
		return(size == 0);
	}
	public void clear()
	{
		first = null;
		size = 0;
	}
		
	public boolean add(T e)
	{
		Node addNode = new Node(e);
		if(size == 0)
			first = addNode;
		else
		{
			Node nextNode = null;
			for(int i = 0; i < size - 1; i++)
			{
				if(nextNode == null)
					nextNode = first.getLink();
				else
					nextNode = nextNode.getLink();
			}
			nextNode.setLink(addNode);
		}
		size++;
		
		return(true);
	}
	public T remove(int index)
	{
		if(index > size - 1)
			throw new IndexOutOfBoundsException();
		
		Node nextNode = null;
		for(int i = 0; i < index - 1; i++)
		{
			if(nextNode == null)
				nextNode = first.getLink();
			else
				nextNode = nextNode.getLink();
		}
		
		Node thisNode = nextNode;
		nextNode = thisNode.getLink();
		Node theNodeAfterNext = nextNode.getLink();
		
		thisNode.setLink(theNodeAfterNext);
		size--;
		return(nextNode.getData());
	}
	public boolean contains(Object o)
	{
		Node nextNode = null;
		for(int i = 0; i < size - 1; i++)
		{
			if(nextNode == null)
				nextNode = first.getLink();
			else
				nextNode = nextNode.getLink();
			
			if(nextNode.getData().equals(o))
				return(true);
		}
		return(false);
	}
	public boolean remove(Object o)
	{
		Node nextNode = null;
		for(int i = 0; i < size - 1; i++)
		{
			if(nextNode == null)
				nextNode = first.getLink();
			else
				nextNode = nextNode.getLink();
			
			if(nextNode.getData().equals(o))
			{
				Node theNodeAfterNext = nextNode.getLink();
				if(theNodeAfterNext != null)
					nextNode.setLink(theNodeAfterNext.getLink());
				else
					nextNode.setLink(null);
				size--;
				return(true);
			}
		}
		return(false);
	}
	public boolean addAll(Collection<? extends T> c)
	{
		for(T data:c)
			add(data);
		return(false);
	}	
	public T get(int index)
	{
		if(index > size - 1)
			throw new IndexOutOfBoundsException();
		
		Node nextNode = null;
		for(int i = 0; i < index - 1; i++)
		{
			if(nextNode == null)
				nextNode = first.getLink();
			else
				nextNode = nextNode.getLink();
		}
		return(nextNode.getLink().getData());
	}
	public T set(int index, T element)
	{
		if(index > size - 1)
			throw new IndexOutOfBoundsException();
		
		Node nextNode = null;
		for(int i = 0; i < index - 1; i++)
		{
			if(nextNode == null)
				nextNode = first.getLink();
			else
				nextNode = nextNode.getLink();
		}
		nextNode = nextNode.getLink();
		T currentData = nextNode.getData();
		nextNode.setData(element);
		return(currentData);
	}

	public boolean removeAll(Collection<?> c)
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	public void add(int index, T element)
	{
		throw new UnsupportedOperationException("Method not supported");
	}	
	public boolean addAll(int index, Collection<? extends T> c)
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	public boolean containsAll(Collection<?> c)
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	public int indexOf(Object o)
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	public int lastIndexOf(Object o)
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	public boolean retainAll(Collection<?> c)
	{
		throw new UnsupportedOperationException("Coder hasnt understand.");
	}
	public ListIterator<T> listIterator()
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	public ListIterator<T> listIterator(int index)
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	public Iterator<T> iterator()
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	public Object[] toArray()
	{
		throw new UnsupportedOperationException("Method not supported");
	}	
	public List<T> subList(int fromIndex, int toIndex)
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	@SuppressWarnings("hiding")
	public <T> T[] toArray(T[] a)
	{
		throw new UnsupportedOperationException("Method not supported");
	}
	private class Node
	{
		private T data;
		private Node link;

		private Node(T d)
		{
			super();
			data = d;
		}
		public void setData(T d)
		{
			data = d;
		}
		public T getData()
		{
			return(data);
		}
		public Node getLink()
		{
			return(link);
		}
		public void setLink(Node l)
		{
			link = l;
		}
	}
}
 
danke für die Antwort, nur die haut mich total aus dem Konzept 😀
...mittels wärs sicher auch einfacher, bzw. bequemer, nur sollte ichs schaffen ohne irgegendeinen import von java.utils :-/

________________________


public interface StringListIterator {
boolean hasNext(); // true gdw es git ein weiters Element
String getNext(); // liefert naechstes Element; requires hasNext()
}

public interface StringList {
void add(String s); // fuegt s vorn hinzu
void removeFirst(); // loescht erstes Element; requires nrOfElems > 0
int nrOfElems(); // Anzahl Elemente
StringListIterator giveIterator(); // liefert Iterator; Durchlauf vom ersten zum letzten Element
}

...sollte nur diese zwei Interfaces benutzen, und eben in der 1. Implementierung eine verzeigte Liste aus in einer inneren Klassen definierten Knoten. Als altes anschauungsbeispiel ist eben die IntList gedacht, nur eben mutable... find auf google auch jetzt nichts brauchbares...
 
Zuletzt bearbeitet:
So jetzt hab ichs mal ohne java.util. gemacht, funktioniert jetzt auch, habs schon getestet...

Nur brauch ich noch nen "Iterator" mit hasnext(); und getnext(); , nur für was, welchen Sinn hat der und was soll der ausgeben?


...hier mal mein code
Java:
public class Liste implements StringList { // Klasse für die Liste
	public class StringNode { // Unterklasse für die Knoten
		StringNode nach; // Referenz auf Nachfolger
		String wert; // das Objekt
	}

	public Liste() {
	}; // default Konstruktor

	StringNode erstes;

	public boolean istLeer() { // istLeer Methode
		return erstes == null;
	}

	public int nrOfElems() { // Gibt die Länge der Kette
		int i = 0;
		StringNode e = erstes;
		while (e != null) {
			e = e.nach;
			i++;
		}
		return i;
	}

	public void add(String j) {
		StringNode neu = new StringNode();
		neu.nach = erstes;
		neu.wert = j;
		erstes = neu;
	}

	public void removeFirst() {
		if (erstes == null) {
		} else {
			erstes = erstes.nach;
		}
	}

	public String ausgabe() {
		StringNode e = erstes;
		String s = "";
		while (e != null) {
			s += e.wert + " ";
			e = e.nach;
		}
		s += " ";
		return s;
	}

	@Override
	public StringListIterator giveIterator() {
		// TODO Auto-generated method stub
		return null;
	}
}
 
Iterator kannst du keinen einbauen, weil das Interface dazu im Pakage java.util liegt.

btw.
[java=34]
if (erstes == null) {
}
[/code]

???:L

[java=40]
String s = "";
while (e != null) {
s += e.wert + " ";
e = e.nach;
}
s += " ";
[/code]

Gaaaaanz schlecht.
 
Nur brauch ich noch nen "Iterator" mit hasnext(); und getnext(); , nur für was, welchen Sinn hat der und was soll der ausgeben?

üblicherweise liefert hasNext() true zurück, wenn noch noch elemente vorhanden sind und getNext() holt das nächste element aus der collection und rückt den iterator zum nächsten element.

@volgagia: weil es ein interface ist muss es implementiert werden.
 
Zuletzt bearbeitet:
Danke für die Hilfe...hab das mit den Iterator mal in nen Versuch umgesetzt, nur hab i jetzt einen Fehler seit ich die add & removeFirst nochmals geändert habe und zwar bei der Inneren Klasse für den Iterator MyIterator bei der übergabe von current = start

Java:
	// Instanzvariable
	private StringNode start;
	private StringListIterator myStringListIterator;

	// Constructor
	public LinkedStringList() {
		start = null;
	}

	// hinzufügen
	public void add(String s) {
		StringNode neu = new StringNode();
		neu.link = start;
		neu.data = s;
		start = neu;
	}

	// loeschen
	public void removeFirst() {
		if (start == null) {
		} else {
			start = start.link;
		}
	}

	// gibt Iterator
	public StringListIterator giveIterator() {
		return new MyIterator();
	}

	// innere Klasse fuer Iterator
	private class MyIterator implements StringListIterator {
		StringNode current;

		MyIterator() {

			current = start;
		}

		public String getNext() { // requires hasNext
			String result = current.data;
			current = current.link;
			return result;
		}

		public boolean hasNext() {
			return current != null;
		}

	}
}

...hier noch ein Testprogramm

Java:
public class BspTest {

	public static void main(String[] args) {
		String str = "Test";
		LinkedStringList myLinkedStringList = new LinkedStringList(); // leere
																		// Liste

		for (int i = 0; i < 5; i++) {
			myLinkedStringList.add(str + i);
		}

		StringListIterator myStringListIterator = myLinkedStringList.giveIterator();

		if (myLinkedStringList.notEmpty()) {

			do {
				System.out.println(myStringListIterator.getNext());
			} while (myStringListIterator.hasNext());

		}

		System.out.println("Anzahl der Elemente:");
		System.out.println(myLinkedStringList.nrOfElems());

		System.out.println("------------");
		System.out.println("myLinkedStringList.removeFirst()");
		System.out.println("------------");
		myLinkedStringList.removeFirst();
		
		if (myLinkedStringList.notEmpty()) {

			do {
				System.out.println(myStringListIterator.getNext());
			} while (myStringListIterator.hasNext());

		}

		System.out.println("Anzahl der Elemente:");
		System.out.println(myLinkedStringList.nrOfElems());
		}
	}
 
Zuletzt bearbeitet:

Zurück
Oben