Esempio n. 1
0
 public int removeFirst()
 {
     if (count == 0)
     {
         throw new Exception("No such element exists!");
     }
     else if (count == 1)
     {
         //make first and last null
         //save the data you want to return before changing
         //the references
         int temp = first.getData();
         first = last = null;
         count--;
         return(temp);
     }
     else
     {
         //save the data
         int temp = first.getData();
         //save a reference to the second node
         MyLinkedListNode tempref = first.getNext();
         //break the next link from first to the second node
         first.setNext(null);
         //break the link from second node back to first node
         tempref.setPrevious(null);
         //reset first to the second node
         first = tempref;
         count--;
         return(temp);//return the data of the remove node
     }
 }
Esempio n. 2
0
        //===============================================
        //methods:
        public void addFirst(int data)
        {
            MyLinkedListNode node = new MyLinkedListNode(data);

            //check if empty
            if (count == 0)
            {
                first = last = node;
            }
            else
            {
                first.setPrevious(node);
                node.setNext(first);
                first = node;
            }
            count++;
        }
Esempio n. 3
0
        public void addLast(int data)
        {
            MyLinkedListNode node = new MyLinkedListNode(data);

            //check if empty
            if (count == 0)
            {
                first = last = node;
            }
            else
            {
                //connect last to node
                last.setNext(node);
                //connect node back to last
                node.setPrevious(last);
                //make the node the last one
                last = node;
            }
            count++;
        }
Esempio n. 4
0
        MyLinkedList ReverseLinkedList(MyLinkedList myLinkedList)
        {
            MyLinkedListNode left     = myLinkedList.getFirst();
            MyLinkedListNode right    = myLinkedList.getLast();
            MyLinkedListNode tempNode = null;

            //Shuffle the  right neighbours of left node to point to right.
            left.getNext().setPrevious(right);

            //Shuffle the left of neighbours of right node to point to left.
            right.getPrevious().setNext(left);

            // make sure that the old left neighbor of right node is  now the recent  left neighbour of left node
            // and the old left neighbour of  left node is now  the recent left neighbor of right node
            tempNode = left.getPrevious();
            left.setPrevious(right.getPrevious());
            right.setPrevious(tempNode);

            // make sure that the old right neighbor of right node is  now the recent  right neighbour of left node
            // and the old right neighbour of left node is now  the recent right neighbor of right node
            tempNode = left.getNext();
            left.setNext(right.getNext());
            right.setNext(tempNode);

            //Arrange the first and last node as they are at the edge
            myLinkedList.setFirstNode(right);
            myLinkedList.setLastNode(left);

            //Get the first and last after reversing the left and right edge nodes.
            left  = myLinkedList.getFirst();
            right = myLinkedList.getLast();

            //This is for the remaining inner nodes.
            for (int i = 1; i < myLinkedList.getCount() / 2; i++)
            {
                left = left.getNext();
                if (left == right) //left and right have joined  for even length  of linked list.
                {
                    break;
                }

                right = right.getPrevious();

                if (left == right)//left and right have joined  for odd length  of linked list.
                {
                    break;
                }
                //Shuffle the refences of neighbours of left to point to right.
                if (left.getNext() == right) // left and right about to join
                {
                    left.getPrevious().setNext(right);
                    right.getNext().setPrevious(left);

                    left.setNext(right.getNext());
                    right.setPrevious(left.getPrevious());

                    left.setPrevious(right);
                    right.setNext(left);
                    break;
                }

                left.getNext().setPrevious(right);
                left.getPrevious().setNext(right);
                //Shuffle the refences of neighbours of right to point to left.
                right.getNext().setPrevious(left);
                right.getPrevious().setNext(left);

                // make sure that the old left neighbor of right node is  now the recent  left neighbour of left node
                // and the old left neighbour of  left node is now  the recent left neighbor of right node
                tempNode = left.getPrevious();
                left.setPrevious(right.getPrevious());
                right.setPrevious(tempNode);

                // make sure that the old right neighbor of right node is  now the recent  right neighbour of left node
                // and the old right neighbour of left node is now  the recent right neighbor of right node
                tempNode = left.getNext();
                left.setNext(right.getNext());
                right.setNext(tempNode);

                tempNode = left;
                left     = right;
                right    = tempNode;
            }
            return(myLinkedList);
        }