class Debtor{ String name; int amount; Debtor nextSame; Debtor next; Debtor(String name, int amount){ this.name = name; this.amount = amount; this.nextSame = null; this.next = null; } Debtor(String name, int amount, Debtor nextSame, Debtor next){ this.name = name; this.amount = amount; this.nextSame = nextSame; this.next = next; } } class List{ Debtor first; Debtor current; Debtor prev; void toBegin(){ this.prev = null; this.current = this.first; } void next(){ this.prev = this.current; this.current = this.current.next; } void nextSame(){ this.prev = this.current; this.current = this.current.nextSame; } void add(String name, int amount){ if (this.first==null) { this.first = new Debtor(name,amount); } else { Debtor x = null; Debtor same = null; if (this.first.amount < amount) { x = this.first; this.first = new Debtor(name,amount); this.first.next = x; x = this.first; while (x.next != null) { x = x.next; if (x.name.equals(name)) { same = x; } } this.first.nextSame = same; } else { x = this.first; same = (x.name.equals(name)) ? x : null; while ((x.next != null) && (x.next.amount >= amount)) { x = x.next; if (x.name.equals(name)) { same = x; } } if (same == null) { Debtor x2 = x; while (x2.next != null) { x2 = x2.next; if (x.name.equals(name)) { same = x2; } } x.next = new Debtor(name, amount, x2, x.next); } else { Debtor newItem = new Debtor(name, amount, same.nextSame, x.next); x.next = newItem; same.nextSame = newItem; } } } } void print(){ if(this.first!=null){ this.toBegin(); while(this.current!=null){ System.out.println(this.current.name + "\t" + this.current.amount); this.next(); } } } void search(String name) { Debtor lastSearched = null; if(this.first!=null){ this.toBegin(); while(this.current!=null){ if (this.current.name.equals(name)) break; this.next(); } if (this.current!=null) { System.out.print(this.current.name + "\t" + this.current.amount); while(this.current.nextSame!=null){ this.nextSame(); System.out.print(" " + this.current.amount); } System.out.println(); } else { System.out.println("Error: Debtor '" + name + "' not found!!!"); } } } } class Cv05Pr2{ public static void main(String[] args){ List debtors = new List(); debtors.add("Pavel",100); debtors.add("Honza",200); debtors.add("Eva",25); debtors.add("Jana",50); debtors.add("Pavel",15); System.out.println("All debtors:"); debtors.print(); System.out.println(); System.out.println("Debet from deptor 'Pavel':"); debtors.search("Pavel"); } }