473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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:
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
2 4536
Oralloy
988 Expert 512MB
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
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

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

Similar topics

4
by: HS-MOON | last post by:
I'm asking you to help me. I'm a beginner of studying c++. I'm trying to make the Singly Linked List(Ordered). Anyway, I've been debugging all day. I can't sort it out!! As you see, I don't...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
13
by: Raj | last post by:
Is there any way to delete a particular node from a singly linked list where the header of the list is unknown.Only pointer available is the one which points to the node to be deleted
59
by: Anando | last post by:
Hi, I have a linear singly linked list of 100 elements. I would like to prune it such that only user specified elements (say only the elements 1, 13, 78 and 100 of the original list) survive...
3
by: malik | last post by:
// Linked Lists in classes(excluding structures) without using tail pointer # include<iostream.h> # include<stdlib.h> void Swap(int num1, int num2) { int a = num1; num2 = num1; num2 = a;
6
by: Alien | last post by:
Hi, I am having problem with ordering a singly linked list. Since a singly linked list goes only one way, is it possible to order them? If my structs look like the one below. struct block...
3
by: jou00jou | last post by:
Hello, I am trying to sort a singly linked list for the following typedef typedef struct message { int messageId; char * messageText; struct message * next; } message; I have successfully...
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
4
by: saki | last post by:
How do we reverse a singly linked list without using extra memory.Extra pointers can however be used
7
by: davidson1 | last post by:
Hello friends, I want ur help regarding singly linked list in C,I tried in net,But it is confusing and difficult.I want singly linked list in a simple way of Understanding.Please Help Me I want...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.