Home > OS >  Method to insert object at index (LinkedList)
Method to insert object at index (LinkedList)

Time:12-05

I am trying to create a method that replace a specific object from my linked list by another object. replaceAtIndex(object, index). I have no idea how to get a specified index from my linked list. Here is the code for my linked list class:

public class CellList {
    
        public class cellNode{
            private cellPhone phone;
            private cellNode next;
            
            //default null
            public cellNode() {
                phone = null;
                next = null;
            }

        
            //parametrized 
            public cellNode(cellPhone phone, cellNode next) {
                this.phone = phone;
                this.next = next;
            }
            
            public cellNode(cellNode x) {
                this.phone = x.phone;
                this.next = x.next;
            }
            
            //Cloning
            protected Object clone() throws CloneNotSupportedException {
            cellNode x=new cellNode(this.phone,this.next);
            return x;
            }


            public cellPhone getPhone() {
                return phone;
            }


            public cellNode getNext() {
                return next;
            }


            public void setPhone(cellPhone phone) {
                this.phone = phone;
            }


            public void setNext(cellNode next) {
                this.next = next;
            }
            
        }
        private cellNode head;
        private int size;
        
        //default
        public CellList() {
            head=null;
            size=0;
        }
        //copy
        public CellList(CellList c) {
            this.head = c.head;
            this.size = c.size;
        }
        
        //Add a node at start
        public void addToStart(cellPhone c) {
            cellNode cn=new cellNode(c,head);
            head=cn;
            size  ;
        }

`

I tried this method but it only correctly replace my elements if the index passes is less than 1. If I try at index 3 for example, it won't replace anything at all and show me the normal list. If I try an index that is higher than my size, it will throw the exception as expected. `


        public void insertAtIndex(cellPhone c,int index) {
            if(index<0 || index>=size) {
                throw new NoSuchElementException("Out of boundary!!!");
            }
            else {
                if(index==0) {
                    addToStart(c);
                }
                else if(index>0 && index<size) {
                    cellNode curr=head.next;
                    cellNode prev=head;
                    cellNode cn=new cellNode(c,head);
                    int i=1;
                    while(curr!=null) {
                        if(i==index) {
                            prev.next=cn;
                            cn.next=curr;
                            size  ;
                            i  ;
                            return;
                        }
                        prev=curr;
                        curr=curr.next;
                    }

                }
            }
        }

`

CodePudding user response:

You don't change i in the while (curr != null) loop. If you add i , it looks like it ought to work.

  • Related