Question
Please do this in Java. ONLY WEEK 1 IS NECESSARY! Attatched at the bottom are my doublylinkedlist, node, and main java classes that could be
Please do this in Java. ONLY WEEK 1 IS NECESSARY! Attatched at the bottom are my doublylinkedlist, node, and main java classes that could be built off of. (Section 3 in pictures irrelevant to the task)
// MAIN:
import java.util.Scanner;
public class MainLists { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // Creating object of Doubly linkedList linkedList list = new linkedList(); System.out.println(" Now for the DOUBLY linked list, WHICH IS NOW USER FRIENDLY!! :)"); char ch; // Perform Doubly list operations --> New update from first submission :) // This program is now 100% user friendly and usable using switch statements :) do { System.out.println(" Doubly Linked List Operations "); System.out.println("1. insert at begining"); System.out.println("2. insert at end"); System.out.println("3. insert at position"); System.out.println("4. delete at position"); int choice = scan.nextInt(); switch (choice) { case 1: // Case for entering an integer at the top of the Doubly linked list System.out.println("Enter integer element to insert"); list.insertAtStart( scan.nextInt() ); break; case 2: // Case for entering an integer at the tail of the Doubly linked list System.out.println("Enter integer element to insert"); list.insertAtEnd( scan.nextInt() ); break; case 3: // Case for entering an integer into any spot of the Doubly linked list System.out.println("Enter integer element to insert"); int num = scan.nextInt(); System.out.println("Enter position"); int pos = scan.nextInt(); if (pos list.getSize() ) System.out.println("Invalid position "); else list.insertAtPos(num, pos); break; case 4: // Case for deleting an integer into any spot of the Doubly linked list System.out.println("Enter position"); int p = scan.nextInt() ; if (p list.getSize() ) System.out.println("Invalid position "); else list.deleteAtPos(p); break; default: // Default case if user input was not one of the choices. System.out.println("Wrong Entry!! "); break; } // Displays the Doubly linked List list.display(); // Calls the display method to display the list. System.out.println(" Do you want to continue (y=yes or n=no) "); ch = scan.next().charAt(0); // will accept y or n in order to continue with the list
} while (ch == 'Y'|| ch == 'y'); // will iterate the do-while loop if choice was a "y" } }
// NODE:
public class Node { public int data; // integer placehold for data public Node next; // points to next node in list public Node prev; // points to previous node in list /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } // Method to set link to next node public void setLinkNext(Node n) { next = n; } // Method to set link to previous node public void setLinkPrev(Node p) { prev = p; } // Method to get link to next node public Node getLinkNext() { return next; } // Method to get link to previous node public Node getLinkPrev() { return prev; } // Method to set data to node public void setData(int d) { data = d; } // Method to get data from node public int getData() { return data; } }
// DOUBLY LINKED LIST: public class linkedList { public Node start; // Start of list dependent on Node public Node end ; // End of list dependent on Node public int size; // Int size holder for list
/* Constructor */ public linkedList() { start = null; // Start set to null end = null; // End set to null size = 0; // size set to 0 } // Method to check if list is empty public boolean isEmpty() { return start == null; } // Method to get size of list public int getSize() { return size; } // Method to insert element at begining public void insertAtStart(int val) { Node nptr = new Node(val, null, null); //creation of new Node if(start == null) { start = nptr; //sets nptr to head end = start; } else { start.setLinkPrev(nptr); nptr.setLinkNext(start); start = nptr; //head of list equal to nptr } size++; //increments size of list } // Method to insert element at end public void insertAtEnd(int val) { Node nptr = new Node(val, null, null); //creation of new Node if(start == null) { start = nptr; // sets start equal to node value of nptr end = start; // sets end qual to start (quivelent to end) } else { nptr.setLinkPrev(end); end.setLinkNext(nptr); end = nptr; // sets end to nptr } size++; //increment the size of the list } // Method to insert element at any position public void insertAtPos(int val , int pos) { Node nptr = new Node(val, null, null); //creation of new Node if (pos == 1) { insertAtStart(val); // will insert the node at the start if position chosen is 1 return; } Node ptr = start; //sets ptr to start for (int i = 2; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started