Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Praktische Informatik (http://www.informatikerboard.de/board/board.php?boardid=6)
--- Implementierung einer Liste (http://www.informatikerboard.de/board/thread.php?threadid=2915)


Geschrieben von ubik am 14.03.2016 um 15:24:

  Implementierung einer Liste

Hallo,

ich habe Probleme bei der Implementierung einer doppelt verketteten Liste. Ich studiere an der Fernuni Hagen und habe eigentlich so ziemlich alles aus dem Skript abgeschrieben.

Wenn ich allerdings Main.java ausführe, bekomme ich eine Exception:

Exception in thread "main" java.lang.NullPointerException
at List.insert(List.java:43)
at Main.main(Main.java:7)

Offensichtlich liegt das daran, dass isempty() nicht richtig funktioniert.

Ich verstehe auch nicht, warum front() einfach return this; zurückgeben soll. Damit wird doch die Listenklasse zurückgegeben und nicht das erste Element?

Und schon gar nicht verstehe ich, wie der last-Zeiger gesetzt werden soll.

Hier ist der Code:

Main.java:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
public class Main
{
  public static void main(String[] args)
  {
    List l1 = new List();

    l1.insert(l1.front(), new Elem(5.32, 1));

    Pos iterator = l1.front;

    while(!l1.eol(iterator))
    {
      iterator = l1.next(iterator);
    }
  }
}


List.java:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
public class List extends Pos
{
  public Pos front, last;

  public Pos front()
  {
    return this;
  }

  public boolean isempty()
  {
    return (front() == null);
  }

  public List empty()
  {
    return null;
  }

  public Pos next(Pos p)
  {
    return p.succ;
  }

  public boolean bol(Pos p)
  {
    return (front() == p);
  }

  public boolean eol(Pos p)
  {
    return (this.last == p);
  }

  public List insert(Pos p, Elem el)
  {
    Pos q = new Pos();
    q.value = el;
    if(!(eol(p) || isempty()))
    {
      q.pred = p;
      q.succ = p.succ;
      p.succ.pred = q;
      p.succ = q;
    }
    else
    {
      q.pred = p;
      q.succ = null;
      p.succ = q;
      pred = q; // Last-Zeiger aendern
    }
    return this;
  }

  public List delete(Pos p)
  {
    Pos q;
    if(!isempty())
    {
      if(eol(p))
      {
        if(p == this.front)
        {
          last = null;
          p.succ = null;
        }
        else
        {
          q = this.front;
          while(q.succ != p) q = q.succ;
          last = q;
          p.succ = null;
        }
      }
      else
      {
        q = p.succ;
        if(q == last) last = p;
        p.succ = p.succ.succ;
      }
    }

    return this;
  }
}


Elem.java:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
public class Elem
{
  double coeff;
  int exp;

  Elem(double i, int j)
  {
    this.coeff = i;
    this.exp = j;
  }

  public double getCoeff()
  {
    return this.coeff;
  }
}


Pos.java:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
public class Pos
{
  Elem value;
  Pos pred, succ;

  public Elem getValue()
  {
    return this.value;
  }
} 



Geschrieben von eulerscheZahl am 14.03.2016 um 19:40:

 

Versuche mal
code:
1:
2:
if (p.succ != null)
				p.succ.pred = q;


isempty() ist immer false: front() liefert this. Wenn this kann aber nicht null sein (wie sollte man isempty() sonst überhaupt aufrufen können?)

front() gibt ein Pos zurück, wovon List ja erbt.
Hätte ich auch anders aufgebaut: den Vorgänger und Nachfolger in Elem gespeichert und nicht in Pos.

Und last ist ja auch immer noch null.


Forensoftware: Burning Board, entwickelt von WoltLab GmbH