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

Recursion on a LinkedList

10
Expand|Select|Wrap|Line Numbers
  1. public class LinkedList<T>{
  2.     private Node head;
  3.  
  4.  
  5.     public LinkedList()
  6.     {
  7.         head = null;
  8.     }
  9.  
  10.     public boolean isEmpty()
  11.     {
  12.         return (head==null);
  13.     }
  14.  
  15.     public void addToHead(T val )
  16.     {
  17.         Node n = new Node(val);
  18.         n.next = head;
  19.         head   = n;
  20.     }
  21.  
  22.  
  23.     public T getHeadData(){
  24.         if(head == null){
  25.             throw new RuntimeException("attempt to get data from an empty list !");
  26.         }
  27.         else{
  28.             return head.data;
  29.         }
  30.     }
  31.  
  32.     public void deleteHead(){
  33.         if(head ==  null){
  34.             throw new RuntimeException("Deleting from an empty list !");
  35.         }else{
  36.             head= head.next;
  37.         }
  38.     }
  39.  
  40.     public void addToTail(T val)//show no result
  41.     {
  42.         Node n = new Node (val);    
  43.         if(head==null)
  44.             head = n;
  45.         else{
  46.             Node curr = head;
  47.             curr = curr.next;
  48.             curr.next = n;
  49.             addToTail(val);
  50.         }    
  51.     }//end addtail
  52.  
  53.  
  54.  
  55.     public void delete(T val){//does not return a result (shows no errors)
  56.         Node curr=null;
  57.         if(head !=null){
  58.             if(head.data==val)
  59.                 head = head.next;
  60.             else{
  61.                 curr = head;
  62.                 curr = curr.next;
  63.                 delete(curr.data);
  64.             }
  65.         }
  66.     }
  67.  
  68.     public void insertSorted(T val){
  69.         Node y = new Node(val);
  70.  
  71.         if(head==null || val < head.data)//operator < cannot be applied to T, T
  72.             head = y;
  73.         else
  74.             head = head.next;
  75.             insertSorted(head.data);
  76.     }
  77.  
  78.  
  79.     public boolean contains(T val)
  80.     {
  81.  
  82.         if(head!=null){
  83.             if(head.data==val)
  84.                 return true;
  85.                 head = head.next;
  86.             return contains(val);
  87.         }
  88.         return false;
  89.  
  90.     }//end of contains
  91.  
  92.  
  93.     public String toString(){
  94.         String str = " ";
  95.         Node curr = head;
  96.         while(curr !=null){
  97.             str = str + curr.data +" ";
  98.             curr = curr.next;
  99.         }
  100.         return str + "\n";
  101.     }
  102.  
  103.  
  104.     public class Node{
  105.         public T data;
  106.         public Node next;
  107.  
  108.         public Node(T val){
  109.             data = val;
  110.             next = null;
  111.         }
  112.     }//End of node class
  113. }//Linked List
  114.  
  115.  
  116. class TestLinkedList{
  117.     public static void main(String[] args){
  118.  
  119.         LinkedList<Integer>ST = new LinkedList<Integer>();
  120.  
  121.         for(int i = 0; i < 10; i++){  //correctly load linkedlist
  122.             ST.addToHead(i);
  123.         }
  124.  
  125.  
  126.             System.out.println(ST.toString()); //print list
  127.  
  128.  
  129.             boolean y = ST.contains(12);
  130.            System.out.println(y);
  131.  
  132.  
  133.            ST.addToTail(200); //returns nothing
  134.                System.out.println(ST.toString());
  135.  
  136.  
  137.             ST.delete(5);
  138.             System.out.println(ST.toString());
  139.  
  140.             insertSorted(11);
  141.             System.out.println(ST.toString());
  142.  
  143.     }//end main
  144. }//end class
  145.  
  146. Need help.
  147. public void addToTail(T val)
  148. public void delete(T val)
  149. public void insertSorted(T val)
  150. I need help to get the above functions to work recursively.  I am able to get them work iteratively, no problem.  When I try to write the functions re cursively, I am not able to see any results. I am at a lost understanding why.
  151.  
  152.  
Feb 24 '10 #1
3 4187
jkmyoung
2,057 Expert 2GB
public void addToTail(T val)
public void delete(T val)
public void insertSorted(T val)

You need to create functions pointing to the Nodes.
Each Node does not point to a LinkedList, it points to another Node!

eg create a function
private void addToTail(T val, Node n)

Expand|Select|Wrap|Line Numbers
  1. public void addToTail(T val){
  2.   base case // need to check if there exists a node..otherwise
  3.   addtoTail(val, head);
  4. }
  5. private void addToTail(T val, Node n){
  6.   if (n.next = null){
  7.     n.next = new Node(val);
  8.   } else {
  9.     addToTail(val, n.next)
  10.   }
  11. }
  12.  
Do something similar for the rest and you should be set.
Feb 24 '10 #2
chetah
10
Thanks you very much for your response. However, I still have a problem, when I am call a function from the main program.

public static void main(etc){

ST.addToTail(Node x, 12) // I am unable to access the Node class from LinkedList.
where am I going wrong?
Feb 24 '10 #3
jkmyoung
2,057 Expert 2GB
Are you calling it from the main program? It should just be:
ST.addToTail(12)

The class will then call addToTail(head, 12) on itself
Feb 25 '10 #4

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 ...
10
by: LP | last post by:
Hi, I was asked at the tech screening what the linked list was which I answered with "academic" definition. Then a guy asked me how I would implement a linked list in C# and what would be a good...
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.