473,397 Members | 2,056 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,397 software developers and data experts.

Recursion on a linkedlist

10
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class LinkedList<T extends Comparable <T>>
  5. {
  6.     private Node head;
  7.     private Node last;
  8.     private T data;
  9.  
  10.     public LinkedList()
  11.     {
  12.         head = null;
  13.     }
  14.  
  15.     public boolean isEmpty()
  16.     {
  17.         return (head==null);
  18.     }
  19.  
  20.  
  21.     public void addToHead (T val)
  22.     {
  23.         Node n = new Node (val);
  24.         n.next = head;
  25.         head = n;
  26.     }
  27.  
  28.  
  29.     private class Node
  30.     {
  31.         public T data;
  32.         public Node next;
  33.  
  34.         Node (T val)
  35.         {
  36.             data = val;
  37.             next = null;
  38.         }
  39.     }
  40.  
  41.  
  42.  
  43.  
  44. public void insertOrdered(T val){
  45.     insertOrdered(head, val);
  46. }
  47.  
  48. public Node insertOrdered(Node head, T val)
  49.   {
  50.      Comparable <T> temp = (Comparable<T>)val;
  51.  
  52.     if (head == null || temp.compareTo(head.data)== 0)
  53.       head = new Node(val);
  54.     else
  55.       head.next = insertOrdered(head.next, val);
  56.  
  57.     return head;
  58.   }
  59.  
  60.  
  61.     public String toString()
  62.     {
  63.         String str ="";
  64.         Node curr = head;
  65.         while(curr!=null)
  66.         {
  67.             str = str + curr.data + " ";
  68.             curr = curr.next;
  69.         }
  70.         return str + "\n";
  71.     }
  72.  
  73.  
  74.  
  75.  
  76. }//end linkedList
  77.  
  78. class Link2
  79. {
  80.     public static void main(String[] arg)
  81.     {
  82.         LinkedList<Integer> s1 = new LinkedList<Integer>();
  83.  
  84.         s1.addToHead(23);
  85.         s1.addToHead(78);
  86.         s1.addToHead(100);
  87.         s1.insertOrdered(6);
  88.         s1.insertOrdered(67);
  89.         s1.insertOrdered(1);
  90.         s1.insertOrdered(7);
  91.  
  92.  
  93.  
  94.         System.out.println(s1.toString());
  95.     }
  96. }
  97.  
  98.  
I am trying to get the insertOrdered method to sort numbers as they are entered but is does not work. What might be the problem with this method? Help in rectifying!
Thank You!!
Feb 26 '11 #1
0 1079

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

Similar topics

8
by: J Peterman | last post by:
Im having a nightmare trying to understand these nodes and linked lists. I've posted my code for my node.h, node.cpp, linkedlist.h and linkedlist.cpp files in separates replies. Can someone...
10
by: cody | last post by:
Why isn't there a LinkedList in .NET? Was the reason that the mark&sweep GC algorithm has problems with heavily linked data? A LinkedList is very important is you have huge lists and append a...
1
by: Spockie | last post by:
I do not use linkedlist stdlibrary, vector i make my own, but i have problems with one issue, and that is checking if there are duplicates in the middle of inputing information line 294 ...
2
by: Justin Crites | last post by:
I have an object which I want to be serializable. I have marked with with . The object only has a single data member, which is a LinkedList<int>. This linked list is a private member and cannot...
2
by: Steve | last post by:
I just realized that I need elements in my List<> to be doubly linked. So I searched for "C# Linked List" and found the LinkedList<> class. I was happy until I looked at the docs for this class...
6
by: Phillip.Ross.Taylor | last post by:
When I designed my application I created an object called "Orderable" which exposes a public property "sequence". Then a few objects inherit from this. I'll just call them ObjectX for the sake...
3
by: huiling25 | last post by:
I don't know why the customer records cannot be inserted into the linked list and the head of the linked list keep pointing to null... //ListNode.java public class ListNode{ private Object...
1
by: sejong510 | last post by:
MSDN has an example of creating a LinkedList populated with String objects: http://msdn2.microsoft.com/en-us/library/he2s3bh7.aspx But, how would you have a LinkedList of a created class?...
1
by: CaseySimplified | last post by:
I am writing a LinkedList class from scratch without using the already defined LinkedList class. The only thing that doesn't seem to be working is adding removing and getting the last link in the...
3
by: chetah | last post by:
public class LinkedList<T>{ private Node head; public LinkedList() { head = null; } public boolean isEmpty()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.