♠ Posted by Unknown in Artificial Intelligent,Data Structure at 10:04
Input Restricted Double Ended Queue
/*Author: Viral Vyas
* IRD Linked List
*/
public class IRDNode {
IRDNode prv;
import java.io.*;
import java.lang.*;
private int iData;
IRDNode next;
public IRDNode()
{ prv = null; iData = 0; next = null; }
public IRDNode(int i)
{ prv = null; iData = i; next = null; }
public int printNode()
{ return iData; }
}
public class IRDMethods {
IRDNode root;
public IRDMethods()
{ root = null; }
public boolean isEmpty()
{
if(root == null)
return true;
else
return false;
}
public void insertRight(int i)
{
IRDNode newNode = new IRDNode(i);
if(isEmpty())
{
root = newNode;
}
else
{
IRDNode current = new IRDNode();
current = root;
while(current.next != null)
current = current.next;
current.next = newNode;
newNode.prv = current;
current = newNode;
}
}
public IRDNode deleteLeft()
{
IRDNode temp = root;
root = root.next;
return(temp);
}
public IRDNode deleteRight()
{
IRDNode current = new IRDNode();
current = root;
IRDNode previous = new IRDNode();
while(current.next != null)
{
previous = current;
current = current.next;
}
previous.next = null;
current.prv = null;
return(current);
}
public void displayList()
{
IRDNode current = new IRDNode();
current = root;
System.out.print("The List : ");
while(current != null)
{
System.out.print(current.printNode() + " ");
current = current.next;
}
System.out.print("\n");
}
}
public class IRDMain {
public static void main(String[] args) {
IRDMethods list = new IRDMethods();
list.insertRight(10);
list.insertRight(20);
list.insertRight(30);
list.insertRight(40);
list.displayList();
//Deleted From Left Side
list.deleteLeft();
list.displayList();
//Deleted From Right Side
list.deleteRight();
list.displayList();
}
}
Output:
The List : 10 20 30 40
The List : 20 30 40
The List : 20 30
0 comments:
Post a Comment