468,294 Members | 1,894 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

singly linked list in sorted order

107 100+
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:
Expand|Select|Wrap|Line Numbers
  1. package LinkedList;
  3. public class Link {    
  4.         public String data;        
  5.         public Link next; 
  7.         public Link(){            
  8.             this.next = null;
  9.         }
  11.         public Link(String data){            
  12.             this.data = data;            
  13.         }
  15.         public void display(){            
  16.             System.out.println(data);            
  17.         }
  19.         public String toString(){            
  20.             return data;            
  21.         }
  23.         public static void main(String[] args) {            
  24.             LinkList theLinkedList = new LinkList();            
  26.             int size = theLinkedList.size();
  28.             System.out.println("Size of the Linked List: "+size);
  30.             System.out.println("The value stored in Linked List initially is: "+theLinkedList.firstLink);
  32.             theLinkedList.insertFirstLink("X");
  34.             System.out.println("Value of head node in Linked List after adding: " + theLinkedList.firstLink);
  36.             theLinkedList.removeFirst();
  38.             System.out.println("The value stored in Linked List after deletion: "+theLinkedList.firstLink);
  40.         }
  42. }
Expand|Select|Wrap|Line Numbers
  1. package ucsc;
  3. class LinkList{
  5.     public Link firstLink; 
  7.     LinkList(){        
  8.         firstLink = null;        
  9.     }
  11.     public int size(){
  12.         Link temp = new Link();
  13.         int counter = 0;
  15.         while(temp!=null){
  16.             temp = temp.next;
  17.             counter++;
  18.         }
  19.         return counter;
  20.     }
  22.     public boolean isEmpty(){        
  23.         return(firstLink == null);        
  24.     }
  26.     public void insertFirstLink(String data){        
  27.         Link newLink = new Link(data);        
  28.         newLink.next = firstLink;
  30.         firstLink = newLink;        
  31.     }
  33.     public Link removeFirst(){
  35.         if(!isEmpty()){        
  36.             firstLink = firstLink.next;        
  37.         } else {            
  38.             System.out.println("Empty LinkedList");            
  39.         }        
  40.         return firstLink;        
  41.     }
  43.     public void display(){        
  44.         Link theLink = firstLink;
  46.         while(theLink != null){            
  47.             System.out.println(theLink);
  49.             theLink = theLink.next;            
  51.             System.out.println();            
  52.         }
  54.     }    
  55. }
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
2 4233
983 Expert 512MB

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.   }
  8.   public void insertString(String value)
  9.   {
  10.     // stuff here
  11.   }
  13.   public void removeString(String value)
  14.   {
  15.     // stuff here
  16.   }
  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,
May 20 '13 #2
3,112 Expert 2GB
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.

Similar topics

4 posts views Thread by HS-MOON | last post: by
19 posts views Thread by RAJASEKHAR KONDABALA | last post: by
59 posts views Thread by Anando | last post: by
6 posts views Thread by Alien | last post: by
3 posts views Thread by jou00jou | last post: by
23 posts views Thread by Himanshu Chauhan | last post: by
4 posts views Thread by saki | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.