Recursive Logic for Linked List in Java - java

I am trying to find a way to write a java program with recursion logic for insertion, searching as well as traversal for singly linked list. But, I don't know how I can do it while my head node is private. Here is the piece of code I have written :
class Node {
int data;
Node next;
}
public class SingleList {
private Node head;
public SingleList() {
head = null;
}
void insert(Node temp, int num, int n) {
//Suggest some code here
}
boolean search(Node temp, int num) {
//Suggest some code here
}
void traverse(Node temp) {
//Suggest some code here
}
}

You don't have any problem to access the head from your class.
In addition, the Node class is probably irrelevant for any other class other than SingleList class, it will be a better idea to include it within the SingleList class
public class SingleList {
private class Node {
int data;
Node next;
}
// Rest of your class code...
}

Related

Initiating a binary search tree using a rootNode that is null

I have this node class
public class Node {
Node right;
Node left;
int value;
Node(int value) {
this.value = value;
}
Node insertValue(int v){
if(this == null){
return new Node(v);
}
//Rest of the method
}
}
I want to make a rootNode that is the tree. If it is null, the tree is empty. Edit: The methods to populate the tree, find minimum, etc. must be recursive methods of class Node
class Main{
public static void main(String[] args) {
Node rootNode = null;
rootNode.insertValue(5);
}
}
Of course, since rootNode == null, I can't use it to call insertValue.
I am looking for a way to populate an empty tree(rootNode == null) with inner methods. Any tips on how I can do this?
You can create a new class Tree which contains (only) the root node and methods to work with like insertValue(). The Node class will become more or less a private helper class for the internals of the tree.
public class Tree {
private Node root;
public void insertValue(int v) {}
public int getSize() {}
public int getValue(int index) {}
}

Printing linked list in java

I couldn't print the linked list it infinitely prints 4 as the value. What is wrong with this code ? In the below code, I am trying to implement a singly linked list after adding every element while am trying to print it infinitely prints 4 as output any idea why?
import java.util.*;
class Linkedit{
static class Node{
static Node next;
int data;
Node(int t){
data=t;
next=null;
}
}
Node head=null;
public void insert(int data){
Node k=new Node(data);
k.next=head;
head=k;
}
public void show(){
Node a=head;
while(a!=null){
System.out.println(a.data);
a=a.next;
}
}
public static void main(String args[]){
Linkedit g=new Linkedit();
g.insert(3);
g.insert(4);
g.insert(5);
g.show();
}
}
Here's the problem:
static Node next;
The next member should not be static, since each Node should have a different next value.
Changing
static class Node {
static Node next;
int data;
Node(int t){
data=t;
next=null;
}
}
to
static class Node {
Node next;
int data;
Node(int t){
data=t;
next=null;
}
}
Solves your problem.

Why using a class as a variable member inside that said class

I am new to Java and been studying it. So I am having trouble to understand what are these and how it being processed by Java also why are we declaring these variables like that. I mean can you educate me on this?
Public abstract class ListItem {
protected Listitem leftLink = null;
protected Listitem rightLink = null;
protected Object value;
some code here
}
Thanks in advance!
Why declare a Class field which has the ClassName as variable type instead of a int, string... ?
Because the developer needs to. Sometimes, an instance of a class must reference another instance of the same class. A typical example is the LinkedList.
Consider a linked list as a sequence of nodes. Each node knows the next one to be linked. Here would be a naive implementation of a node:
class Node<T> {
private Node<T> next;
private T value;
Node(T value) {
this.value = value;
}
public T getValue() {
return value;
}
void setNext(Node<T> next) {
this.next = next;
}
}
As you can see, the class Node contains a variable member of type Node, to reference the next element of the linked list. Finally, a simplistic implementation of the linked list would be:
class LinkedList<T> {
private Node<T> first;
private Node<T> last;
private int length = 0;
public void add(T value) {
Node<T> node = new Node<T>(value);
if(length != 0) {
last.setNext(node);
last = node;
}
else {
first = node;
last = node;
}
length++;
}
public Node<T> getFirst() {
return first;
}
}
When a new node is added to the collection, the previous last node references it and therefore, becomes the new last node.

Inserting a node to the middle of a doubly linked list

I want to insert a Node in to the middle of a doubly linked list.This is my implementation.
My default argument is the node before the to-be inserted node .
public class DListNode{
public DListNode prev,next;
public Object item;
public DListNode(Object i,DListNode n,DListNode p){
prev=p;
next=n;
item=i;
}
public DListNode(Object i){
this(i,null,null);
}
}
public void insertAfter(Object e,DListNode firstNode) throws UnderFlow{
if(isEmpty()||(head==tail))
throw new UnderFlow("need at least two nodes");
else{
DListNode v=firstNode.next;
DListNode k=new DListNode(e,v,firstNode);
firstNode =k;
v.prev=k;
}
}
public static void main (String args[]) throws UnderFlow{
DList d=new DList();
d.insertFront("d");
d.insertFront("b");
d.insertAfter("c",d.head);
System.out.println(d.toString());
}
Though I can't see where I am making the mistake it only prints out b,d.
c is not inserted in the middle.
Please can someone point out where I am making the mistake
I would suggest you to use variable names with some meaning. Your names like k, v are terrible to understand
Instead of firstNode =k; should be firstNode.next =k; but it's better to rewrite with proper names. Like this
DListNode next=firstNode.next;
DListNode newOne=new DListNode(e,next,firstNode);
firstNode.next =newOne;
next.prev=newOne;

Java Searching LinkedList for data return true/false?

I need write a method to loop through a linked list searching to see if Object data is in linked list. Any help?
public class LinkedList {
private LinkedListNode head;
public boolean find(Object data){
for(somethinggoeshere..){
if(head==data){
return true;
}else{
return false;
}
}
Any help?
Edit: My LinkedListNode class:
public class LinkedListNode {
private Object data;
private LinkedListNode next;
public LinkedListNode(Object data, LinkedListNode next) {
super();
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
}
Edit: Final solution for those who are interested:
public class LinkedList {
private LinkedListNode head;
public boolean find(Object data){
LinkedListNode temp = head;
while(temp!= null) // check if you have reached the tail
{
if(data.equals(temp.getData()))
{
return true;
}
temp = temp.getNext(); // move to the next node
} // end of while loop
return false;
} // end of find method
Assuming that you wrote the code for LinkedListNode, you should know wether or not is Iterable, and thus be able to loop through it with a for-each loop like that.
As it stands, you should traverse the nodes either recursively or in an iterative manner, by using some form of "next" pointers that is held within each node, and in essence do a linear search through the links until you find the data you are looking for, or return null.
Here's a link for some help on implementing a linked list:
http://www.danielacton.com/Data-Structures/Linked-List/Java/
You need to loop through your LinkedList and search for the data. If you reach tail of the list and still unable to find the data this implies that data is not in the LinkedList.
I am assuming that LinkedListNode has a member variable data to store the data in each node. Below is the corrected code :
public class LinkedList {
private LinkedListNode head;
public boolean find(Object data)
{
LinkedListNode temp = head;
while(temp!= null) // check if you have reached the tail
{
if(data.equals(temp.getData()))
{
return true;
}
temp = temp.getNext(); // move to the next node
} // end of while loop
return false;
} // end of find method
}

Categories