intermediate/ProfileADT

From ggc

Jump to: navigation, search

001 package intermediate;
002 //start auto-imports
003 import com.sun.org.apache.bcel.internal.generic.*;
004 import java.util.*;
005 //end auto-imports
006 
007 
008 /**
009  * All about my application.
010  @author Jam Jenkins
011  */
012 public class ProfileADT
013 {
014   public static class Linky<T>
015   {
016     public class Node<T>
017     {
018       T data;
019       Node<T> next;
020     }
021 
022     private Node<T> head;
023 
024     public Linky()
025     {
026       head=null;
027     }
028 
029     public boolean isEmpty()
030     {
031       return head==null;
032     }
033 
034     public void addFirst(T data)
035     {
036       if(isEmpty())
037       {
038         Node<T> node=new Node<T>();
039         node.data=data;
040         node.next=null;
041         head=node;
042       }
043       else
044       {
045         Node<T> node=new Node<T>();
046         node.data=data;
047         node.next=head;
048         head=node;
049       }
050     }
051 
052     public T removeFirst()
053     {
054       if(isEmpty())
055         return null;
056       T data=head.data;
057       head=head.next;
058       return data;
059     }
060 
061     public String toString()
062     {
063       String result="head->";
064       Node<T> current=head;
065       while(current!=null)
066       {
067         result+=current.data+"->";
068         current=current.next;
069       }
070       result+="null";
071       return result;
072     }
073   }
074 
075   public static void fill(List list, int size)
076   {
077     for(int i=0; i<size; i++)
078     {
079       int number=(int)(Math.random()*100000);
080       list.add(0, number);
081     }
082   }
083 
084 
085   public static int getMiddle(List<Integer> list)
086   {
087     return list.get(list.size()/2);
088   }
089 
090   public static void main(String[] args)
091   {
092     Linky<String> linky=new Linky<String>();
093     System.out.println(linky);
094     linky.addFirst("Jam");
095     System.out.println(linky);
096     linky.addFirst("Jelly");
097     System.out.println(linky);
098     linky.addFirst("Roll");
099     System.out.println(linky);
100     String data=linky.removeFirst();
101     System.out.println("Removed first: "+data);
102     System.out.println("List is now: "+linky);
103 
104     /*
105     LinkedList<Integer> list=new LinkedList<Integer>();
106     ArrayList<Integer> random=new ArrayList<Integer>();
107     fill(random, 100);
108     long start=System.currentTimeMillis();
109     long count=0;
110     while(System.currentTimeMillis()<start+1000)
111     {
112       random.get(random.size()-1);//getMiddle(random);
113       count++;
114   }
115     System.out.println("Random access add: "+count);
116     fill(list, 100);
117     start=System.currentTimeMillis();
118     count=0;
119     while(System.currentTimeMillis()<start+1000)
120     {
121       list.get(list.size()-1);//getMiddle(list);
122       count++;
123   }
124     System.out.println("Sequential access add: "+count);
125     */
126   }
127 }


Download/View intermediate/ProfileADT.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter