By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,648 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,648 IT Pros & Developers. It's quick & easy.

singly linked list in sorted order

100+
P: 107
i have been given an assignment to create a singly linked list which maintains order. but i am not sure whether to use a sorting method or to take care of the sorting while adding the values in the list. i guess i am correct to decide the second option because if we insert values according to the order, then the list is automatically a sorted one.

my code for now is as below:
Link.java
Expand|Select|Wrap|Line Numbers
  1. package LinkedList;
  2.  
  3. public class Link {    
  4.         public String data;        
  5.         public Link next; 
  6.  
  7.         public Link(){            
  8.             this.next = null;
  9.         }
  10.  
  11.         public Link(String data){            
  12.             this.data = data;            
  13.         }
  14.  
  15.         public void display(){            
  16.             System.out.println(data);            
  17.         }
  18.  
  19.         public String toString(){            
  20.             return data;            
  21.         }
  22.  
  23.         public static void main(String[] args) {            
  24.             LinkList theLinkedList = new LinkList();            
  25.  
  26.             int size = theLinkedList.size();
  27.  
  28.             System.out.println("Size of the Linked List: "+size);
  29.  
  30.             System.out.println("The value stored in Linked List initially is: "+theLinkedList.firstLink);
  31.  
  32.             theLinkedList.insertFirstLink("X");
  33.  
  34.             System.out.println("Value of head node in Linked List after adding: " + theLinkedList.firstLink);
  35.  
  36.             theLinkedList.removeFirst();
  37.  
  38.             System.out.println("The value stored in Linked List after deletion: "+theLinkedList.firstLink);
  39.  
  40.         }
  41.  
  42. }
  43.  
LinkList.java
Expand|Select|Wrap|Line Numbers
  1. package ucsc;
  2.  
  3. class LinkList{
  4.  
  5.     public Link firstLink; 
  6.  
  7.     LinkList(){        
  8.         firstLink = null;        
  9.     }
  10.  
  11.     public int size(){
  12.         Link temp = new Link();
  13.         int counter = 0;
  14.  
  15.         while(temp!=null){
  16.             temp = temp.next;
  17.             counter++;
  18.         }
  19.         return counter;
  20.     }
  21.  
  22.     public boolean isEmpty(){        
  23.         return(firstLink == null);        
  24.     }
  25.  
  26.     public void insertFirstLink(String data){        
  27.         Link newLink = new Link(data);        
  28.         newLink.next = firstLink;
  29.  
  30.         firstLink = newLink;        
  31.     }
  32.  
  33.     public Link removeFirst(){
  34.  
  35.         if(!isEmpty()){        
  36.             firstLink = firstLink.next;        
  37.         } else {            
  38.             System.out.println("Empty LinkedList");            
  39.         }        
  40.         return firstLink;        
  41.     }
  42.  
  43.     public void display(){        
  44.         Link theLink = firstLink;
  45.  
  46.         while(theLink != null){            
  47.             System.out.println(theLink);
  48.  
  49.             theLink = theLink.next;            
  50.  
  51.             System.out.println();            
  52.         }
  53.  
  54.     }    
  55. }
  56.  
Kindly, advice me what is the best thing for me to do. If i am rite with my move then i need to know how to do the insert operation. how do we loop through each node of a linked list and what is the algorithm for me to follow.

i can think about iterating through each node and comparing the value in the node with the value i am about to add, if(currentnode.next.element > value && currentnode.element < value) then i can create a node in between those nodes and insert the new element. but i am not sure how to implement my idea in code.

any suggestions would be appreciated.
May 19 '13 #1
Share this Question
Share on Google+
2 Replies


Oralloy
Expert 100+
P: 983
raamay,

The point of a storage object, like your LinkedList, is to guard the client from having to deal with the implementation details.

In your case, having an exposed method like insertFirstLink makes the user of your class have to deal with the details of its implementation.

Perhaps you want to have a simple interface, instead, and keep the first/last/other details of list management hidden?

The basic, exposed (public) interface should probably look something like this:
Expand|Select|Wrap|Line Numbers
  1. class LinkedList
  2. {
  3.   public LinkedList()
  4.   {
  5.     // stuff here
  6.   }
  7.  
  8.   public void insertString(String value)
  9.   {
  10.     // stuff here
  11.   }
  12.  
  13.   public void removeString(String value)
  14.   {
  15.     // stuff here
  16.   }
  17.  
  18.   public int size()
  19.   {
  20.     // stuff here
  21.   }
  22. };
With the insertString and removeString methods having responsibility for all list manipulation and maintenence. They should work correctly regardless of where in the list a value is inserted, whether the list is empty, or the insert/remove position. This way, client code never sees the details of the implementation, it just uses your class in a very simple and logical fashion.

What I have not included are methods for accessing the data in the list. Since this is pretty obviously a school assignment, you can do one of two things:
  1. Expose the list structure to the client, and allow the client class to walk the list directly.
  2. Embed any list processing functions into the LinkedList class, which keeps the details private.
What you do, of course, depends on the project requirements. This is a learning task, so learn to deal with list manipulation, but also learn a bit about abstraction and encapsulation while you are at it.

Again, to reiterate - the value of having a LinkedList class is to protect the client classes from having to know (and deal with) the details of the list's implementation. By encapsulating the implementation, you can provide correct behaviour without having to worry about your client classes inadvertently breaking the implementation.

Good Luck,
Oralloy
May 20 '13 #2

Nepomuk
Expert 2.5K+
P: 3,112
Hi raamay!

A general rule to follow is: optimize for commonly used features. In your case this means that if you insert new objects into the list often but only output the list rarely, you should sort the list on output. But if you enter the items only very rarely and read the list often, sort them when putting them into the list.

Concerning what Oralloy said about the public interface, you could have a look at the List interface from the Java Collections Framework. While it may not be necessary for your LinkedList to implement the List interface, it may be a good idea to look at it so as to decide which publicly available functions you need and what kind of signature they should have.
May 21 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.