Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will create a linked list as shown in class. You must use t

ID: 3890461 • Letter: W

Question

Write a program that will create a linked list as shown in class. You must use the method taught in class and not the library Linked List class in Java. The linked list can be of any type (integer, character, double etc.), therefore, you can use the program you starting implementing in class Include the following requirements .A node class which will define your node A linked list class which will have your main method for testing and the following methods o A method to inscrt a node at the front of the list o A method to insert a node at the back of the list o A method to remove a node at the front of the list o o o A method to remove a node at the back of the list. A method to insert anywhere in the list A method to remove anywhere in the list A print method to output your list. Your main method should have objects created to test your list. . You can name your methods however you wish. Choosc a name that describes what the mcthod does. In order to obtain full marks each of the requirements must be included in your programm

Explanation / Answer

class LinkedList { Node head; // Globally Declare head of list class Node /* Class Node*/ { int data; Node next; Node(int d) // Node Class Constructer { data = d; next = null; } } public void insertAtFront(int new_data) // Function to insert new element At Start of Linked List { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } public void insertAtEnd(int new_data) // Function to insert new element At the End of Linkedlist { Node new_node = new Node(new_data); if (head == null) { head = new Node(new_data); return; } new_node.next = null; Node last = head; while (last.next != null) last = last.next; last.next = new_node; return; } public void insertAtGivenPos(int pos, int new_data) // Function to insert new element At Given Position in Linked List { if (head == null&& pos != 0) { System.out.println("New Data Can't be inserted at given Position"); return; } Node temp = head; if (pos == 0) { insertAtFront(new_data); return; } for (int i=0; temp!=null && i 7 -> null llist.printList(); llist.insertAtFront(1); // Insert 1 at the beginning. So linked list becomes 1 -> 2 -> 7 -> null llist.printList(); llist.insertAtEnd(3); // Insert 3 at the End. So linked list becomes 1 -> 2 -> 7 -> 3-> null llist.printList(); llist.insertAtGivenPos(1, 8); // Insert 8 at Position 1 So linked list becomes 1 -> 8 -> 2 -> 7 -> 3-> null llist.printList(); llist.deleteNodeFromLast(); // Delete node from End So linked list becomes 1 -> 8 -> 2 -> 7 -> null llist.printList(); llist.deleteNodeFromFront(); // Delete node from Front So linked list becomes 8 -> 2 -> 7 -> null llist.printList(); llist.deleteNodeFromGivenPos(1); // Delete node from Last So linked list becomes 8 -> 7 -> null llist.printList(); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote