Implement a linked list of integers as a
class LinkedList. Build the following methods:
✓ print that prints the content of the linked list;
✓ addFirst that adds a new node to the beginning (the head) of the
linked list;
✓ addLast that adds a new node to the end (the tail) of the linked list;
✓ indexOf that finds a specific node by its value, and returns node’s
index (node’s position from the left in the linked list); if the value
is not present in the linked list, it returns −1;
✓ deleteFirst that deletes the first node in the linked list;
✓ deleteLast that deletes the last node in the linked list.
Test your class creating a list in the main and
1) adding one by one nodes 2, 4, 8 to the tail;
2) adding nodes -2, -8 to the head;
3) adding a node 9 to the tail;
4) printing the list;
5) printing indexOf(4);
6) printing contains(9);
7) deleting one by one all the nodes in the list – either from the tail or
from the head – and printing the result after each deletion.
THEN AFTER THIS
Add a new method IN THE FORM OF O(n) reverse to the class LinkedList created in the problem
1. This method must reverse the order of the nodes in the list. For
example, having applied reverse to the list [2 → 4 → 8 → 9 → 8], we
will change it to [8 → 9 → 8 → 4 → 2].
PLEASE WRITE CODE IN JAVA
Step by stepSolved in 8 steps with 5 images
- My code does not produce the required output, can anyone let me know what seems to be off? Input: from Node import Node from LinkedList import LinkedList def print_linkedlist(list): print(f'Linked List ------ Nodes ----------------------------------') print(f'head: {list.head.name:<6}', end=' ') node = list.head while node != None: print(f"{node.name:<12}", end=' ') node = node.next print() print(f'tail: {list.tail.name:<6}', end=' ') node = list.head while node != None: print(f"data:{node.data:<7}", end=' ') node = node.next print() print(f' ', end=' ') node = list.head while node != None: if node.next != None: print(f"next:{node.next.name:<7}", end=' ') else: print("next:None") node = node.next print() print(f' ', end=' ') node = list.tail while node != None: if node.prev != None:…arrow_forwardIn python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class…arrow_forwardAdd more methods to the singly linked list class then test them• search(e) // Return one node with 3 values (stuID, stuName, stuScore) which matches agiven key e (studentID).• addAfter(e, stuID, stuName, stuScore) //Add a new node with 3 values (stuID,stuName, stuScore) after the node with the key e (studentID).• removeAt(e) //Remove a node which matches a given key e (studentID)• count() //Return a number of nodes of list.• update(stuID, stuName, stuScore) //Update the values of one node three codes below two in pictures one,typed out. public class SlinkedList<A,B,C> { private Node head; private Node tail; private int size; public SlinkedList(){ head=null; tail=null; size=0; } public int getSize(){ return size; } public boolean isEmpty(){ return size == 0; } public A getFirstStuId(){ if(isEmpty()) return null; return (A) head.getStuID();}public B getFirstStuName(){ if(isEmpty())…arrow_forward
- JAVA please Given main() in the ShoppingList class, define an insertAtEnd() method in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list. The output is: Kale Lettuce Carrots Peanuts Code provided in the assignment ItemNode.java:arrow_forwardin java please Implement findTheThird method in linked list that searches the bag for a given entry. If found, - removes the first occurrence - leave the second occurrence intact - then replace third occurrence with the string “Found3rd” - remove the rest of the occurrences Return false if no replacement happened. Otherwise, true. public boolean findTheThird (T entry) Note: You may assume that firstNode is a private data in list which references to first node.arrow_forwardGiven the source code of linked List, answer the below questions(image): A. Fill out the method printList that print all the values of the linkedList: Draw the linked list. public void printList() { } // End of print method B. Write the lines to insert 10 at the end of the linked list. You must draw the final linked List. Notice that you can’t use second or third nodes. Feel free to define a new node. Assume you have only a head node C. Write the lines to delete node 2. You must draw the final linked list. Notice that you can’t use second or third node. Feel free to define a new node. Assume you have only a head nodearrow_forward
- Implement an instance method, called getFirstHalf, that belongs to yourThingLinkedBag. The method returns as output a linked list that includes allvalues in the first half of the bag's linked list. For example, for the following bag3-->5-->8-->9. the method returns 3-->5.If the bag includes an odd number of nodes, then the middle element belongs tothe first half and is included in the output.Make sure to include the methodheader. The bag should remain unchanged (i.e., do not remove the returnedvalues from the bag).arrow_forwardComplete the code for the removeFirst method, which should remove and return the first element in the linked list. Throw aNoSuchElementException if the method is invoked on an empty list. import java.util.NoSuchElementException; public class LinkedList { private Node first; public LinkedList() { first = null; } public Object getFirst() { if (first == null) { throw new NoSuchElementException(); } return first.data; } public Object removeFirst() { // put your code here } class Node { public Object data; public Node next; } }arrow_forwardJava's LinkedList provides a method listlterator(int index) returning a Listlterator for a list. Declare a variable of LinkedList of integer numbers and write a fragment of Java code to compute and print the sum of the numbers on the list. Do not write code to insert elements to the list and assume that the list already has elements on it. You must only use an iterator obtained from the list (you can not use the get(int index) method) .arrow_forward
- Add more methods to the doubly linked list class then test them• search(e) // Return one node with 3 values (stuID, stuName, stuScore) which matches agiven key e (studentID).• addAfter(e, stuID, stuName, stuScore) //Add a new node with 3 values (stuID,stuName, stuScore) after the node with the key e (studentID).• removeAt(e) //Remove a node which matches a given key e (studentID)• count() //Return a number of nodes of list.• update(stuID, stuName, stuScore) //Update the values of one node first code below package DlinkedList;public class DLinkedList<A,B,C> { private Node<A,B,C> header; private Node<A,B,C> trailer; private int size; public DLinkedList() { header = new Node<>(null, null, null); trailer = new Node<>(null, null, null); header.setNext(trailer); trailer.setPrev(header); } public int getSize() { return size; } public boolean isEmpty() { return size==0; } public A…arrow_forwardPlease use Java Write a method “removeHighestLinkedList” that takes a LinkedList and removes the highest value.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education