Traversing Linked List In Reverse Order:
package linkedList;
import java.util.List;
public interface LinkedListInterface<T>
{
void add(T t);
List<T> readNodeValues();
List<T> readNodeValuesInReverseOrder();
}
/********************************************/
package linkedList;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractLinkedListImpl<T> implements LinkedListInterface<T>
{
protected Node start = null;
static class Node
{
private Node next = null;
private Object value;
}
@Override
public void add(T t) {
Node n = new Node();
n.value = t;
if(this.start == null)
{
this.start = n;
}
else
{
//create temp node to point to start
Node temp = this.start;
//traverse to end of linked list whre next = null
while(temp.next != null)
{
temp = temp.next;
}
temp.next = n;
}
}
@Override
public List<T> readNodeValues() {
List<T> list = null;
while(this.start!=null)
{
list = new ArrayList<T>();
list.add((T)start.value);
this.start=this.start.next;
}
return list;
}
@Override
public List<T> readNodeValuesInReverseOrder() {
List<T> list = null;
traverse(this.start);
return list;
}
private void traverse(Node start)
{
if(start == null)
{
return;
}
traverse(start.next);
System.out.println(start.value);
}
}
/********************************/
package linkedList;
public class MyLinkedList<T> extends AbstractLinkedListImpl<T>
{
public MyLinkedList()
{
this.start = null;
}
public static void main(String [] str)
{
MyLinkedList<String> linkedList = new MyLinkedList<String>();
linkedList.add(new String("Debasish"));
linkedList.add(new String ("Subhasish"));
linkedList.add(new String ("Krishna"));
linkedList.add(new String ("Kanha"));
linkedList.readNodeValuesInReverseOrder();
}
}
package linkedList;
import java.util.List;
public interface LinkedListInterface<T>
{
void add(T t);
List<T> readNodeValues();
List<T> readNodeValuesInReverseOrder();
}
/********************************************/
package linkedList;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractLinkedListImpl<T> implements LinkedListInterface<T>
{
protected Node start = null;
static class Node
{
private Node next = null;
private Object value;
}
@Override
public void add(T t) {
Node n = new Node();
n.value = t;
if(this.start == null)
{
this.start = n;
}
else
{
//create temp node to point to start
Node temp = this.start;
//traverse to end of linked list whre next = null
while(temp.next != null)
{
temp = temp.next;
}
temp.next = n;
}
}
@Override
public List<T> readNodeValues() {
List<T> list = null;
while(this.start!=null)
{
list = new ArrayList<T>();
list.add((T)start.value);
this.start=this.start.next;
}
return list;
}
@Override
public List<T> readNodeValuesInReverseOrder() {
List<T> list = null;
traverse(this.start);
return list;
}
private void traverse(Node start)
{
if(start == null)
{
return;
}
traverse(start.next);
System.out.println(start.value);
}
}
/********************************/
package linkedList;
public class MyLinkedList<T> extends AbstractLinkedListImpl<T>
{
public MyLinkedList()
{
this.start = null;
}
public static void main(String [] str)
{
MyLinkedList<String> linkedList = new MyLinkedList<String>();
linkedList.add(new String("Debasish"));
linkedList.add(new String ("Subhasish"));
linkedList.add(new String ("Krishna"));
linkedList.add(new String ("Kanha"));
linkedList.readNodeValuesInReverseOrder();
}
}
No comments:
Post a Comment