Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package dummyHead_RAK;
- public class DoublyList {
- public DNode head;
- /* Build a Dummy Headed Circular List from the given circular array
- */
- public void buildFromArray(int[]cir,int st,int size){
- // TO DO
- head=new DNode(null,null,null);
- DNode tail=head;
- int k=st;
- for(int i=0;i<size;i++){
- DNode n=new DNode(cir[k],null,null);
- k=(k+1)%cir.length;
- tail.next=n;
- n.prev=tail;
- tail=n;
- }
- tail.next=head;
- head.prev=tail;
- }
- /* prints the elements in the list */
- public void forwardprint(){
- // TO DO
- for(DNode n=head.next;n!=head ;n=n.next){
- System.out.print(n.element+" ");
- }
- System.out.println();
- }
- public void backwardprint(){
- // TO DO
- for(DNode n=head.prev; n!=head ;n=n.prev){
- System.out.print(n.element+" ");
- } System.out.println();
- }
- /* Build a Dummy Headed Circular List from the given Non Dummy Headed Non Circular List
- */
- public void buildFromList(Node h){
- // TO DO
- head=new DNode(null,null,null);
- DNode tail=head;
- for(Node n=h;n!=null;n=n.next){
- DNode mn=new DNode(n.element,null,null);
- tail.next=mn;
- mn.prev=tail;
- tail=mn;
- }
- tail.next=head;
- head.prev=tail;
- }
- /* Build a Dummy Headed Circular List from the given Non Dummy Headed Non Circular List
- * The elements of the new Dummy Headed list must in reverse order
- */
- public void buildReverse(Node h){
- // TO DO
- DNode rev=new DNode(h.element,null,null);
- DNode tail=rev;
- for(Node n=h.next;n!=null;n=n.next){
- DNode mn=new DNode(n.element,null,null);
- mn.next=rev;
- rev.prev=mn;
- rev=mn;
- }
- DNode newHead=new DNode(null,null,null);
- newHead.next=rev;
- rev.prev=newHead;
- rev=newHead;
- tail.next=rev;
- rev.prev=tail;
- head=rev;
- }
- /* Insert the element in the given index.
- * Index 0 is the index after the dummy head
- */
- public void addElement(int element, int index){
- // TO DO
- DNode succ=null;
- DNode pred=null;
- if(index>=0 && index<countNode()){
- if(index==countNode()){
- succ=head;
- }
- else{
- succ=nodeAt(index);
- }
- DNode mn=new DNode(element,null,null);
- pred=succ.prev;
- mn.next=succ;
- mn.prev=pred;
- pred.next=mn;
- succ.prev=mn;
- }
- }
- /* Delete the element from the given index.
- * Index 0 is the index after the dummy head
- */
- public void deleteElement(int index){
- // TO DO
- DNode succ=null;
- DNode pred=null;
- Object elem=null;
- if(index>=0 && index<countNode()){
- DNode n=nodeAt(index);
- succ=n.next;
- pred=n.prev;
- pred.next=succ;
- succ.prev=pred;
- elem=n.element;
- n.next=null;
- n.prev=null;
- System.out.println(elem+" removed");
- }
- }
- public int countNode(){
- int count=0;
- for(DNode n=head.next;n!=head;n=n.next){
- count++;
- }
- return count;
- }
- public DNode nodeAt(int idx){
- int count=0;
- for(DNode n=head.next;n!=null;n=n.next){
- if(idx==count){
- return n;
- }
- count++;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement