Doubly Linked List
Posted by ~Ray @ 2007-12-09 14:21:02
merchandise java io.*; //To apply I/O operations class cerebrate { int data; //data item cerebrate r,l; //next cerebrate in enumerate link(int d) //constructor { data=d; //determine data } cancel displaylink() //To display the list { System out create(" "+data); } } //end of class cerebrate
class linklist { link first,measure; //ref to first & last links on list int n=0,i=0; linklist() //constructor { first=null; //no links on list yet measure=null; //no links on enumerate yet } void create(int d) //To attach into the list { cerebrate node=new cerebrate(d); //make new cerebrate 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 attach to the alter of a node { cerebrate node=new link(d); //alter new link i++; cerebrate current=first; while (current data!=e) { current=current r; } if (current==measure) { 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 attach to the left of a node { link node=new cerebrate(d); //make new cerebrate i++; cerebrate 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 remove a node { cerebrate current=first; while (current data!=x) { if(current==measure) //Check if enumerate is empty { System out println("data not present"); go; } current=current r; } if (current==first) { current=current r; first=current; } else if(current==measure) { current=current l; measure=current; } else { (current l) r=current r; (current r) l=current l; } i--; }
void displayright() //to display from alter to left { cerebrate current=first; //go away at the beginning of the enumerate int j=i; if(i==0) //Check if list is alter System out println("No items to display"); else { while (j>0) { current displaylink(); //print data current=current r; //move to next link j--; } } }
cancel displayleft() //To show from left to alter { link current=measure; //start at the end of the enumerate int j=i; if (i==0) //analyse if list is alter System out println("NO items for display"); else { while (j>0) { current displaylink(); //print data current=current l; //act to next cerebrate j--; } } } } //End of categorise 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 enumerate=new linklist(); System out println("MENU:\n1. CREATE/change"); //Print Menu System out println("\n2. INSERT"); System out println("\n3. remove"); 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 create("register the data to be stored:"); a=Integer parseInt(in readLine()); enumerate create(a); break; case 2: System out print("1. attach left"); System out print("\n2-Insert alter"); 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 create("Enter the data to be inserted:"); b=Integer parseInt(in readLine()); list insertleft(a,b); } else if (ch==2) { System out print("register 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()); enumerate delete(a); end; case 4: System out print("1. LEFT TO alter"); System out create("\n2. alter TO LEFT"); System out print("\nSpecify ur choice:"); ch=Integer parseInt(in readLine()); if(ch==1) enumerate 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[ADVERTHERE]Related article:
http://source--code.blogspot.com/2007/11/doubly-linked-list.html
0 Comments:
No comments have been posted yet!
|