[Solved] Part 1 Implementation Doubly Linked List Attached Find Code Implementation Singly Linked L Q37241496
Part 1 – Implementation of a Doubly LinkedList
Attached you will find the code for an implementation of a SinglyLinked List.
There are 3 files:
Driver.java — testing the List functions in a main()method.
LinkNode.java — Class definition for a Node, which is theunderlying entity that stores the items for the linked list.
SinglyLinkedList.java — Class definition for the SinglyLinked 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 themain() as needed.
Carefully review the code and comments to see that you understandeverything that’s happening. If anything is unclear, or ifthere is a bug in my code, then please let me know.
A good exercise would be to comment the Driver to list allthe operations that are being performed on the list.
Task 2 – length() method – 10 points:
Write a method for the SinglyLinkedList that returns thelength of the current list. If the list is empty it should return0. You can use any implementation whatsoever.
Task 3 – Doubly Linked List – 60 points:
Use the SinglyLinkedList code as a guide for implementing aDoublyLinkedList.
You can extend the Node and SinglyLinkedList classes OR you cancopy and paste the code into new files andrefactor them. It is your choice. The crucialthing is to see how each of the methods in the Linked List getsmodified. You must implement each of the methods. No doubt,delete() and insert() are the trickiest.
Concentrate on having a well defined plan of action beforeproceeding to code. Visualize clearly what needs to happen for thenew implementations. Layout what changes are needed in notes or adesign document which you can use in the report below.
Make sure you have the following additional methods on theDoublyLinkedList:
displayReverse()
addToTail()
Be careful with your implementation and make sure you aremaintaining the structural integrity of theLinkedList.
Try one method at a time. Start with the simple ones, viz.add() and addToTail(), make sure each methodworks correctly before moving to other methods.
Note: displayReverse() is the onlyguarantee that your doubly Linked List is correct in bothdirections.
Report:
Discuss how you implement each part in detail. Discuss anydifficulties faced, and how you resolved those difficulties.
Additional resources for assignment:
1.Driver.java
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(); }}
2 .Linknode.java
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; }}
3.SingleLinkedList.java
package 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() + “–||”); }}
Notes: Please run this code in Eclipse because i am usingEclipse.
Expert Answer
Answer to Part 1 – Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked… . . .
OR

