Sunday, November 4, 2007

Doubly Linked List

Program : Doubly Linked List

import java.io.*; //To implement I/O operations

class link
{
int data; //data item
link r,l; //next link in list

link(int d) //constructor
{
data=d; //initialize data
}

void displaylink() //To display the list
{
System.out.print(" "+data);
}
} //end of class link


class linklist
{
link first,last; //ref to first & last links on list
int n=0,i=0;

linklist() //constructor
{
first=null; //no links on list yet
last=null; //no links on list yet
}

void create(int d) //To insert into the list
{
link node=new link(d); //make new link
i++;
node.r=first;
if(n!=0)
(node.r).l=node;
if(n==0)
last=node;
first=node;
n++;
}

void insertright(int d,int e) //To insert to the right of a node
{
link node=new link(d); //make new link
i++;
link current=first;
while (current.data!=e)
{
current=current.r;
}
if (current==last)
{
node.l=current;
node.r=null;
current.r=node;
last=node;
}
else
{
node.l=current;
node.r=current.r;
(current.r).l=node;
current.r=node;
}
}

void insertleft(int d,int e) //To insert to the left of a node
{
link node=new link(d); //make new link
i++;
link current=last;
while (current.data!=e)
{
current=current.l;
}
if (current==first)
{
node.l=null;
node.r=current;
current.l=node;
first=node;
}
else
{
node.l=current.l;
node.r=current;
(current.l).r=node;
current.l=node;
}
}

void delete(int x) //To delete a node
{
link current=first;
while (current.data!=x)
{
if(current==last) //Check if list is empty
{
System.out.println("data not present");
return;
}
current=current.r;
}
if (current==first)
{
current=current.r;
first=current;
}
else if(current==last)
{
current=current.l;
last=current;
}
else
{
(current.l).r=current.r;
(current.r).l=current.l;
}
i--;
}

void displayright() //to display from right to left
{
link current=first; //start at the beginning of the list
int j=i;
if(i==0) //Check if list is empty
System.out.println("No items to display");
else
{
while (j>0)
{
current.displaylink(); //print data
current=current.r; //move to next link
j--;
}
}
}

void displayleft() //To display from left to right
{
link current=last; //start at the end of the list
int j=i;
if (i==0) //Check if list is empty
System.out.println("NO items for display");
else
{
while (j>0)
{
current.displaylink(); //print data
current=current.l; //move to next link
j--;
}
}
}
} //End of class linklist



public class Doubly //The main class doubleLink
{
public static void main(String s[]) //Main function
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int choice=0,ch=0,a,b;
linklist list=new linklist();
System.out.println("MENU:\n1.CREATE/MODIFY");
//Print Menu
System.out.println("\n2.INSERT");
System.out.println("\n3.DELETE");
System.out.println("\n4.DISPLAY");
System.out.println("\n5.EXIT");
do
{
System.out.print("Enter ur choice:");
choice=Integer.parseInt(in.readLine());
//Accepts choice
switch (choice)
{
case 1:
System.out.print("Enter the data to be stored:");
a=Integer .parseInt(in.readLine());
list.create(a);
break;
case 2:
System.out.print("1.Insert left");
System.out.print("\n2-Insert Right");
System.out.print("\nSpecify ur choice:");
ch=Integer.parseInt(in.readLine());
System.out.print("Enter the data to be inserted:");
a=Integer.parseInt(in.readLine());
if (ch==1)
{
System.out.print("Enter the data to be inserted:");
b=Integer.parseInt(in.readLine());
list.insertleft(a,b);
}
else if (ch==2)
{
System.out.print("Enter the data to be inserted:");
b=Integer.parseInt(in.readLine());
list.insertright(a,b);
}
break;
case 3:
System.out.print("Enter the number to be deleted:");
a=Integer.parseInt(in.readLine());
list.delete(a);
break;
case 4:
System.out.print("1.LEFT TO RIGHT");
System.out.print("\n2.RIGHT TO LEFT");
System.out.print("\nSpecify ur choice:");
ch=Integer.parseInt(in.readLine());
if(ch==1)
list.displayright();
else
list.displayleft();
System.out.println(" ");
break;
}
}
while(choice!=5);
}
catch(IOException e) //To catch any exceptions
{
}
} //End of main function
} //End of main class LinkQueue

0 comments: