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: 

  1. printList(): This function prints all elements in the linked list.
  2. addFirst(E e): This function adds a new node with element e at the front of a linked list.
  3. addLast(E e): This function adds a new node with element e at the end of a linked list.
  4. deleteFirst(): This function deletes a node at the front of a linked list. The function returns the deleted node's element.
  5. 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

1 Expert Approved Answer
Step: 1 Unlock

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

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!