Question: Create a generic class Node for a linked list that contains a variable element that holds the data of generic type E , and another
Create a generic class Node for a linked list that contains a variable element that holds the data of generic type E, and another variable next of generic type Node that will be used to link the next element in the linked list.
Create a class CustomLinkedList to implement a singly linked list and implement the following functions:
- printList(): This function prints all elements in the linked list.
- addFirst(E e): This function adds a new node with element e at the front of a linked list.
- addLast(E e): This function adds a new node with element e at the end of a linked list.
- deleteFirst(): This function deletes a node at the front of a linked list. The function returns the deleted node's element.
- deleteLast(): This function deletes a node at the end of a linked list. The function returns the deleted node's element.
Create two lists in the main function with the following data:
List 1: 10, 4, 5 18, 20
List 1 should be created using addFirst(E e) and then the items in the list should be printed. For this list, show the operation of deleteFirst() by deleting the element at index 2.
List2: "Apple", "Banana", "Cherry", "Grape", "Orange"
List 2 should be created using addLast(E e) and then the items in the list should be printed. For this list, show the operation of deleteLast() by deleting the element at index 2.
Step by Step Solution
There are 3 Steps involved in it
Heres a Java implementation of the generic Node class and the CustomLinkedList class with the requested functions You can use this code in your main f... View full answer
Get step-by-step solutions from verified subject matter experts
