Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Klasse für Nodes
- public class MyNode <T>{
- MyNode next;
- T data;
- public MyNode(T in)
- {
- next = null;
- this.data = in;
- }
- public void Print()
- {
- System.out.println(this.data.toString());
- }
- public MyNode getNext()
- {
- return this.next;
- }
- public void setNext(MyNode n)
- {
- this.next = n;
- }
- }
- //Klasse für die Liste
- public class MyList {
- MyNode first;
- public MyList()
- {
- first=null;
- }
- public void add(MyNode n)
- {
- if (first==null) this.first = n;
- else
- {
- MyNode it;
- for( it = this.first; it.getNext()!=null; it=it.getNext());
- it.setNext(n);
- }
- }
- public void printAll()
- {
- if (first==null) System.out.println("Keine Nodes!");
- else
- {
- MyNode it;
- for( it = this.first; it!=null; it=it.getNext())
- {
- it.Print();
- }
- }
- }
- public void reverse() // rekursiv! noch überarbeiten Q_Q
- {
- if (first==null) return;
- else
- {
- MyNode it;
- MyNode oldfirst = new MyNode(0);
- MyNode newfirst = new MyNode(0);
- for( it = this.first; it!=null; it=it.getNext())
- {
- this.first = newfirst;
- it = oldfirst;
- first.next = oldfirst;
- }
- }
- }
- }
- //Die Main
- public class Main {
- public static void main(String[] args)
- {
- MyList list = new MyList();
- list.add(new MyNode(3));
- list.add(new MyNode("blafasel"));
- list.add(new MyNode(true));
- list.printAll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement