Pointer

huch, da war mein beitrag wohl völlig umsonst
das code beispiel von swalbking is so eins einer solchen liste

lg roflomfg
 
Also der Code schaut schon mal ganz nice aus! ich werde mir gleich mal alles ein wenig genauer betrachten. Ist sicherlich noch ausbaufähig ^^
Dankeschön schon mal

zu roflomfg: Ich muss das für die Ausbildung machen im Sinne von Java lernen und Datenstruckturen (nur halt schon was heftiger)!
 
Hi djjada,

Du kannst die Pointer einfach über Objektreferenzen Darstellen.

Hab kurz mal was dazu vorbereitet

Code:
package de.germo.linkedList;

public class ListElement {
	
	private ListElement previous = null;
	private ListElement next = null;
	private String content = "";
	
	
	/**
	 * @param content
	 */
	public ListElement(String content) {
		this.content = content;
	}
	
	
	
	/**
	 * @return the next
	 */
	public ListElement getNext() {
		return next;
	}



	/**
	 * @param next the next to set
	 */
	public void setNext(ListElement next) {
		this.next = next;
	}



	/**
	 * @return the previous
	 */
	public ListElement getPrevious() {
		return previous;
	}



	/**
	 * @param previous the previous to set
	 */
	public void setPrevious(ListElement previous) {
		this.previous = previous;
	}



	/**
	 * Fügt ein Element nach sich selber ein
	 * @param e einzufügendes Element
	 */
	public void addElementAfter(ListElement e) {
		e.setPrevious(this);
		e.setNext(this.next);
		if (next != null) {
			next.setPrevious(e);
		}
		this.next = e;
	}
	
	/**
	 * Fügt ein Element vor sich selber ein
	 * @param e einzufügendes Element
	 */
	public void addElementBefore(ListElement e) {
		e.setNext(this);
		e.setPrevious(this.previous);
		if (previous != null) {
			previous.setNext(e);
		}
		this.previous = e;
	}
	
	public void remove() {
		previous.setNext(next);
		next.setPrevious(previous);
	}
	
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		String retval = "";
		retval += content;
		if (this.next != null) {
			retval += next;
		}
		
		return retval;
	}
	
	public static void main(String args[]) {
		ListElement le1 = new ListElement("Dies ");
		ListElement le2 = new ListElement("ist ");
		ListElement le3 = new ListElement("eine ");
		ListElement le4 = new ListElement("Liste.");
		le1.addElementAfter(le2);
		le2.addElementAfter(le3);
		le3.addElementAfter(le4);
		System.out.println(le1);
		ListElement le5 = new ListElement("verkettete ");
		le3.addElementAfter(le5);
		System.out.println(le1);
		le5.remove();
		System.out.println(le1);
		ListElement le6 = new ListElement("veränderte ");
		le4.addElementBefore(le6);
		System.out.println(le1);
	}

}

Ausgabe:

Code:
Dies ist eine Liste.
Dies ist eine verkettete Liste.
Dies ist eine Liste.
Dies ist eine veränderte Liste.


Edit: da war ich wohl zu Langsam ;)
 
Hallo,

schau mal hier:
Java:
/**
 * 
 */
package de.tutorials;

/**
 * @author Thomas.Darimont
 * 
 */
public class LinkedListExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.add("abc");
        list.add("def");
        list.add("ghi");

        System.out.println(list.toString());
        System.out.println(list.get(0));
        System.out.println(list.get(1));
        System.out.println(list.get(2));
        // System.out.println(list.get(3));

        // list.remove("abc");
        // list.remove("def");
        list.remove("ghi");
        System.out.println(list.toString());
    }

    static class LinkedList {
        Node root;
        int size;

        public void add(Object value) {
            if (null == root) {
                root = new Node(value);
            } else {
                Node current = root;
                while (null != current.next) {
                    current = current.next;
                }

                Node toBeAdded = new Node(value);
                current.add(toBeAdded);
            }
            size++;
        }

        public boolean remove(Object value) {
            boolean removed = false;
            Node current = root;
            while (null != current && !removed) {
                if (value.equals(current.value)) {
                    Node previous = current.previous;
                    Node next = current.next;
                    if (current == root) { // was root node..
                        root = next;
                    } else {
                        if (null != next) { // end of the list?
                            next.previous = previous;
                        }
                        previous.next = next;
                    }
                    removed = true;
                    size--;
                }
                current = current.next;
            }

            return removed;
        }

        public Object get(int index) {
            if (index >= size) {
                throw new IndexOutOfBoundsException("Index: " + index
                        + " is greater or equal to list size: " + size);
            }

            Node current = root;
            int currentIndex = 0;
            while (currentIndex < index) {
                current = current.next;
                currentIndex++;
            }
            return current.value;

        }

        static class Node {
            Node previous;
            Node next;
            Object value;

            /**
             * @param value
             */
            public Node(Object value) {
                super();
                this.value = value;
            }

            public void add(Node toBeAdded) {
                this.next = toBeAdded;
                toBeAdded.previous = this;
            }

            /**
             * @return the previous
             */
            public Node getPrevious() {
                return previous;
            }

            /**
             * @param previous
             *            the previous to set
             */
            public void setPrevious(Node previous) {
                this.previous = previous;
            }

            /**
             * @return the next
             */
            public Node getNext() {
                return next;
            }

            /**
             * @param next
             *            the next to set
             */
            public void setNext(Node next) {
                this.next = next;
            }

            /**
             * @return the value
             */
            public Object getValue() {
                return value;
            }

            /**
             * @param value
             *            the value to set
             */
            public void setValue(Object value) {
                this.value = value;
            }

            @Override
            public String toString() {
                return String.valueOf(this.value);
            }
        }

        /**
         * @return the size
         */
        public int getSize() {
            return size;
        }

        @Override
        public String toString() {

            StringBuilder stringBuilder = new StringBuilder("[");

            if (null != root) {
                Node current = root;
                while (null != current) {
                    stringBuilder.append(current.value);
                    if (null != current.next) {
                        stringBuilder.append(" ");
                    }
                    current = current.next;
                }
            }

            stringBuilder.append("]");

            return stringBuilder.toString();
        }
    }

}

Gruß Tom
 
Zurück