intermediate/SortedLinkedList

From ggc

Jump to: navigation, search

001 package intermediate;
002 
003 
004 /**
005  * This class is an implementation of a basic
006  * doubly-linked list of Strings that remains
007  * sorted.
008  @author Jam Jenkins
009  */
010 public class SortedLinkedList
011 {
012   /**
013    * This is an inner class that only has use
014    * within the SortedLinkedList.  It holds a
015    * sequence of characters as its data and a
016    * pointer to the next and previous node.  Users
017    * of the SortedLinkedList class should not ever
018    * need to see or use the Node class.
019    */
020   private class Node
021   {
022     String data;
023     Node next;
024     Node prev;
025 
026     public Node(String data, Node prev, Node next)
027     {
028       this.data=data;
029       this.next=next;
030       this.prev=prev;
031     }
032   }
033 
034   /**where the list begins*/
035   private Node head;
036   /**where the list ends*/
037   private Node tail;
038 
039   /**adds the data at the appropriate place in the sorted list
040    @param data the information to add to the list
041   */
042   public void add(String data)
043   {
044     if(head==null)
045     {
046       Node node=new Node(data, nullnull);
047       head=node;
048       tail=node;
049     }
050     else
051     {
052       Node current=head;
053       while(data.compareTo(current.data)>&& current.next!=null)
054         current=current.next;
055       if(data.compareTo(current.data)>0)
056       {
057         Node node=new Node(data, tail, null);
058         tail.next=node;
059         tail=node;
060       }
061       else if(current.prev==null)
062       {
063         Node node=new Node(data, null, head);
064         head.prev=node;
065         head=node;
066       }
067       else
068       {
069         Node node=new Node(data, current.prev, current);
070         current.prev.next=node;
071         current.prev=node;
072       }
073     }
074   }
075 
076   /**removes the element if it exists, otherwise returns null
077    @param index which element to remove.
078    @return the String at the given position
079    * Zero indicates the first element and the valid indexes go
080    * up to one less than the number of elements
081    */
082   public String remove(int index)
083   {
084     //add your implementation here
085     return null;
086   }
087 
088   /**gets the element if it exists, otherwise returns null
089    @param index which element to return.
090    * Zero indicates the first element and the valid indexes go
091    * up to one less than the number of elements
092    */
093   public String get(int index)
094   {
095     //add your implementation here
096     return null;
097   }
098 
099   /**merges the current linked list with the other linked list
100    * Note: this method changes the value of the parameter!
101    * This and other are identical after this call.  After calling
102    this method, this and other have the same contents which is
103    * the union of the original contents, still ordered.
104    @param other the list to merge with this one.
105    */
106   public void mergeTogether(SortedLinkedList other)
107   {
108     //add your implementation here
109   }
110 
111   /**converts the list into a readable text version
112    @return the text version of the list
113   */
114   public String toString()
115   {
116     Node current=head;
117     String total="head<->";
118     while(current!=null)
119     {
120       total+=current.data+"<->";
121       current=current.next;
122     }
123     total+="tail";
124     return total;
125   }
126 
127   /**this performs tests of SortedLinkedList to make sure it
128    * works as specified
129    @param args not used
130   */
131   public static void main(String[] args)
132   {
133     SortedLinkedList list=new SortedLinkedList();
134     list.add("Jam");
135     list.add("Jenkins");
136     list.add("Christopher");
137     System.out.println("list is "+list);
138   }
139 }


Download/View intermediate/SortedLinkedList.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