Listex
Allikas: Lambda
public class Listex { static int[] a; public static void main(String[] arg) { System.out.println("listex starts"); // create and initialise an array a a=new int[5]; a[0]=10; a[1]=20; a[2]=30; a[3]=40; a[4]=50; for(int i=0; i<a.length; i++) { System.out.println("a elem "+i+" is: "+a[i]); } // build a new list from array a, storing start at ls Listnode ls=null; // list start Listnode ln=null; // current (temporary) list node Listnode lp=null; // previous (temporary) list node for(int i=0; i<a.length; i++) { ln=new Listnode(); if (i==0) ls=ln; ln.data=a[i]; ln.next=null; if (lp!=null) lp.next=ln; lp=ln; } // go through the list, print out all data, count els int count=0; for(ln=ls; ln!=null; ln=ln.next) { count++; System.out.println("node data: "+ln.data); } // go through the list and store data to a new array int[] b; b=new int[count]; count=0; for(ln=ls; ln!=null; ln=ln.next) { b[count]=ln.data; count++; } // go thorugh array b and print els for(int i=0; i<b.length; i++) { System.out.println("b elem "+i+" is: "+b[i]); } // add a new node containing 100 // after el containing 30 Listnode nn; for(ln=ls; ln!=null; ln=ln.next) { if (ln.data==30) { nn=new Listnode(); nn.data=100; nn.next=ln.next; ln.next=nn; break; } } ls.listprint(); System.out.println("listex ends"); } } class Listnode { int data; Listnode next; int listprint() { // go through the list, print out all data, count els Listnode ln; int count=0; for(ln=this; ln!=null; ln=ln.next) { count++; System.out.println("node data: "+ln.data); } return count; } }