/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tradsecexempel; import java.util.logging.Logger; /** * * @author Administrator */ //DEFINIERAR KLASS FÖR LÄNKAD LISTA class LankadLista { int tal; LankadLista next; } public class Main { public static void main(String args[]) { //int tabb[] = new int[50]; boolean last = false; LankadLista l = new LankadLista(); LankadLista forsta = new LankadLista(); LankadLista sista = new LankadLista(); forsta = sista = l; l.tal = 7; l.next = null; //JUST NU HAR VI EN NOD I LISTAN: //......... //. 7 . X . //......... LankadLista l2 = new LankadLista(); l2.tal = 8; l2.next = l.next; sista = l2; l.next = l2; //JUST NU HAR VI TVÅ NODER I LISTAN: //......... ......... //. 7 . .---->. 8 . X . //......... ......... //VISAR ALLA NODER I LISTAN PÅ BILDSKRÄMEN for(LankadLista current = forsta; !last; current = current.next) { System.out.println(current.tal); if(current.next == null) last = true; } //---------------------------------------- last = false; //SÄTTER IN EN NY NOD GENAST EFTER DEN NODEN SOM INNEHÅLLER DATAT 7 for(LankadLista current = forsta; !last; current = current.next) { if(current.tal == 7) { LankadLista nyobj = new LankadLista(); nyobj.tal = 2; nyobj.next = current.next; current.next = nyobj; } if(current.next == null) last = true; } //--------------------------------------------------------------- //JUST NU HAR VI TRE NODER I LISTAN: //......... ......... ......... //. 7 . .---->. 2 . .----->. 8 . X . //......... ......... ......... //VISAR ALLA NODER I LISTAN PÅ BILDSKRÄMEN last = false; System.out.println("--------"); for(LankadLista current = forsta; !last; current = current.next) { System.out.println(current.tal); if(current.next == null) last = true; } //---------------------------------------- //TAR BORT DEN NOD UR LISTAN SOM INNEHÅLLER DATAT 8 LankadLista curr = forsta; LankadLista foreg = curr; last = false; for(; !last; curr = curr.next) { if(curr.tal == 8) { foreg.next = curr.next; } if(curr.next == null) last = true; foreg = curr; } //-------------------------------------------------- //JUST NU HAR VI TVÅ NODER I LISTAN: //......... ......... //. 7 . .---->. 2 . X . //......... ......... //VISAR ALLA NODER I LISTAN PÅ BILDSKRÄMEN System.out.println("--------"); last = false; for(LankadLista current = forsta; !last; current = current.next) { System.out.println(current.tal); if(current.next == null) last = true; } //----------------------------------------- } }