Question

Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List.

There are 3 files:

Driver.java -- testing the List functions in a main() method.

LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list.

SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here.

Task 1 -

Review & Testing: Create a new project and add these classes into the project. Make sure that everything works. You can try out other tests in the main() as needed. Carefully review the code and comments to see that you understand everything that's happening. If anything is unclear, or if there is a bug in my code, then please let me know. A good exercise would be to comment the Driver to list all the operations that are being performed on the list.

Task 2 :

Write a method for the SinglyLinkedList that returns the length of the current list. If the list is empty it should return 0. You can use any implementation whatsoever.

Task 3 - Doubly Linked List Use the SinglyLinkedList code as a guide for implementing a DoublyLinkedList.

You can extend the Node and SinglyLinkedList classes OR you can copy and paste the code into new files and refactor them.

It is your choice. The crucial thing is to see how each of the methods in the Linked List gets modified.

You must implement each of the methods. No doubt, delete() and insert() are the trickiest. Concentrate on having a well defined plan of action before proceeding to code. Visualize clearly what needs to happen for the new implementations.

Layout what changes are needed in notes or a design document which you can use in the report below.

Make sure you have the following additional methods on the DoublyLinkedList:

displayReverse()

addToTail()

Be careful with your implementation and make sure you are maintaining the structural integrity of the LinkedList.

Try one method at a time. Start with the simple ones, viz. add() and addToTail(), make sure each method works correctly before moving to other methods.

Task:4

Report(need in google Doc file): Discuss how you implement each part in detail. Discuss any difficulties faced, and how you resolved those difficulties.

Please see below all three addition instruction which I mention Beginning of the Question .please use in the code

(1) Driver.java package LinkedList;

package LinkedList;

public class driver {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                SinglyLinkedList s = new SinglyLinkedList();
                
                s.display();
                s.add(3);
                s.display();
                s.add(5);
                s.display();
                s.add(13);
                s.display();
                s.add(19);
                s.display();
                s.add(23);
                s.display();
                System.out.println(s.search(9));
                System.out.println(s.search(3));
                System.out.println(s.remove(19));
                System.out.println(s.remove(5));
                System.out.println(s.remove(23));
                s.display();
        }
package LinkedList;

public class LinkNode {
        //Data
        private int item;
        private LinkNode next;
        
        LinkNode(int i){
                this.item = i;
        }
        
        //Methods
        public int getItem() {
                return item;
        }
        public void setItem(int item) {
                this.item = item;
        }
        public LinkNode getNext() {
                return next;
        }
        public void setNext(LinkNode next) {
                this.next = next;
        }
ackage LinkedList;

public class SinglyLinkedList {
        //Data
        private LinkNode head;
        
        //Methods
        //Constructor
        SinglyLinkedList(){ //Empty Constructor
                head = null;    //Indicate that this is an empty list
        }
        
        /*SinglyLinkedList(int i){ //Second overloaded constructor 
        }*/
        
        //Regular Methods
        //add()
        public void add(int item) {
                //new node with this incoming value; make sure that the next pointer is pointing to the correct place
                LinkNode newNode = new LinkNode(item);
                newNode.setNext(head);  //This is redundant code, because as a result of the construction, the next reference would have defaulted to null
                                                                //Once we change null to head then we are generalizing the code without in any way changing the first add
                //head is pointing to this node
                head = newNode;
        }
        
        //remove()
        public boolean remove(int r) {
                LinkNode remItem = search(r);
                if(remItem == null) {
                        return false;
                }else {
                        if(head.getItem() == r) {
                                //special case to deal with
                                head = head.getNext();
                                return true;
                        }
                        LinkNode iterator = head;
                        //LinkNode previous = head;
                        while(iterator != null) {
                                if(iterator.getNext().getItem() == r) {
                                        //LinkNode previous = iterator;
                                        break;
                                }
                                iterator = iterator.getNext();
                        }
                        
                        iterator.setNext(iterator.getNext().getNext());
                        return true;
                }
        }
        
        //search()
        public LinkNode search(int x) {
                /*if(head == null) {
                        return null;    //null indicates item was not found
                }*/
                
                LinkNode iterator = head;
                
                while(iterator != null) {
                        if(iterator.getItem() == x) {
                                return iterator;
                        }
                        iterator = iterator.getNext();
                }
                
                return null;    //null indicates item was not found
        }
        
        //display()
        public void display() {
                if(head == null) {
                        System.out.println("Empty List");
                        return;
                }
                
                //Linked List Traversal
                LinkNode iterator = head;
                System.out.print("List: --> ");
                while(iterator.getNext() != null) {
                        System.out.print(iterator.getItem() + "-->");
                        iterator = iterator.getNext();
                }
                System.out.println(iterator.getItem() + "--||");
        }
}

Note:I am using Eclipse so please make sue i can run my code in Eclipse

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//########################### PGM START #####################################

package LinkedList;

public class driver {

    public static void main(String[] args) {
            // TODO Auto-generated method stub
           DoublyLinkedList s = new DoublyLinkedList();
          
            s.display();
            s.add(3);
            s.display();
            s.add(5);
            s.display();
            s.add(13);
            s.display();
            s.add(19);
            s.display();
            s.add(23);
            s.display();
            s.displayReverse();
            s.addToTail(10);
            s.display();
            s.displayReverse();
            System.out.println("Length of linked list: "+s.listLength());
            System.out.println(s.search(9));
            System.out.println(s.search(3).getItem());
            System.out.println(s.remove(19));
            System.out.println(s.remove(5));
            System.out.println("Length of linked list: "+s.listLength());
            System.out.println(s.remove(23));
            s.displayReverse();
            s.display();
    }
}

//-----------------------------------------------------------------------------------------------------

package LinkedList;

public class LinkNode {
    //Data
    private int item;
    private LinkNode next;
    private LinkNode prev;
  
    LinkNode(int i){
            this.item = i;
    }
  
    //Methods
    public int getItem() {
            return item;
    }
    public void setItem(int item) {
            this.item = item;
    }
    public LinkNode getNext() {
            return next;
    }
    public void setNext(LinkNode next) {
            this.next = next;
    }
    public LinkNode getPrev() {
        return prev;
    }
    public void setPrev(LinkNode prev) {
        this.prev = prev;
    }
}

//--------------------------------------------------------------------------------------------------------------------

package LinkedList;

public class DoublyLinkedList {
    //Data
    private LinkNode head;
  
    //Methods
    //Constructor
    DoublyLinkedList(){ //Empty Constructor
            head = null;    //Indicate that this is an empty list
    }
  
  
    //Regular Methods
    //add()
    public void add(int item) {
          //new node with this incoming value; make sure that the next pointer is pointing to the correct place
          LinkNode newNode = new LinkNode(item);
          newNode.setNext(head); //This is redundant code, because as a result of the construction, the next reference would have defaulted to null
                                                            //Once we change null to head then we are generalizing the code without in any way changing the first add
         
          newNode.setPrev(null);
          if(head!=null)
              head.setPrev(newNode);
        
          //head is pointing to this node
          head = newNode;
    }
    //function to add item to the end of the list
    public void addToTail(int item) {
       LinkNode newNode = new LinkNode(item);
       LinkNode last=head;
      
       newNode.setNext(null);
       if (head == null) {
            newNode.setPrev(null);
            head = newNode;
            return;
        }
      
       while (last.getNext() != null)
              last = last.getNext();
      
       last.setNext(newNode);
       newNode.setPrev(last);
      
    }
    //remove()
    public boolean remove(int r) {
            LinkNode remItem = search(r);
            if(remItem == null) {
                    return false;
            }else {
                    if(head.getItem() == r) {
                            //special case to deal with
                            head = head.getNext();
                            head.setPrev(null);
                            return true;
                    }
                    LinkNode iterator = head;
                    //LinkNode previous = head;
                    while(iterator != null) {
                            if(iterator.getNext().getItem() == r) {
                                    //LinkNode previous = iterator;
                                    break;
                            }
                            iterator = iterator.getNext();
                    }
                  
                    iterator.setNext(iterator.getNext().getNext());
                    //set the previous pointer if the node deleted is not the last node
                    if(iterator.getNext()!=null)
                       iterator.getNext().setPrev(iterator);
                  
                    return true;
            }
    }
  
    //search()
    public LinkNode search(int x) {
            /*if(head == null) {
                    return null;    //null indicates item was not found
            }*/
          
            LinkNode iterator = head;
          
            while(iterator != null) {
                    if(iterator.getItem() == x) {
                            return iterator;
                    }
                    iterator = iterator.getNext();
            }
          
            return null;    //null indicates item was not found
    }
  
    //display()
    public void display() {
            if(head == null) {
                    System.out.println("Empty List");
                    return;
            }
          
            //Linked List Traversal
            LinkNode iterator = head;
            System.out.print("\nTraversal in forward direction \n");
            System.out.print("List: --> ");
            while(iterator.getNext() != null) {
                    System.out.print(iterator.getItem() + "-->");
                    iterator = iterator.getNext();
            }
            System.out.println(iterator.getItem() + "--||");
    }
    //function to reverse a list
    public void displayReverse() {
         
           //if head is null print empty list
           if(head == null) {
              System.out.println("Empty List");
              return;
          }
            System.out.print("\nTraversal in reverse direction \n");
          
            //iterate till last node of the list
            LinkNode last = head;
            System.out.print("List: --> ");
            while(last.getNext() != null) {
               last= last.getNext();
            }
         
            //from last node print list element till the begining
            while (last.getPrev() != null) {
               System.out.print(last.getItem() + "-->");
                last = last.getPrev();
            }
            System.out.println(last.getItem() + "--||");
    }
    //function to find the length of linked list
    public int listLength() {
       int count=0;
      
       //if list is not empty
       if(head != null) {
           LinkNode temp=head;
           //count each node in the list
           while(temp!=null) {
               temp=temp.getNext();
               count+=1;
           }
       }
       //return count
       return count;
      
    }
}


//################################# PGM END #########################

OUTPUT
#########

<terminated> driver Java Application] C:\Program Files Javajdk-11.0.2 bin Empty List Traversal in forward direction List: -->

Add a comment
Know the answer?
Add Answer to:
Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT