Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the implementation of a List ADT using linked nodes In this lab, you are going to play a role as an ADT programmer again

Complete the implementation of a List ADT using linked nodes In this lab, you are going to play a role as an ADT programmer again to finish implementing a list ADT using linked node. Recall the data structure of linked lists 32 ->987-> 42-> 77-> 135 5 List The List interface has been provided as List.java. Given this interface, the application programmer has developed a simple application TestList.java to use the List ADT. Your task for todays lab is to fill in the three operations/methods in the List ADT: public void insertHead(E item) public void insertTail(E item) public void insertAtPosition (E item, int position) A backbone of the ADT is provided in BasicLinkedList.java. You will need to fill in the code in each of the three methods. 1

/** * Defines the interface to a list collection. * The implementation of the list is hidden. * */ public interface List { /** Pre: Post: the list is unchanged. * Return: true if this list contains no elements; false otherwise */ public boolean isEmpty(); /** Pre: Post: the list is unchanged. * Return: the number of elements in this list. */ public int size(); /** * Check if an element exists in the list Pre: Post: the list is unchanged. Return: true if the specified element is found in this list and false otherwise. Throws an EmptyCollectionException if the list is empty. */ public boolean contains (E targetElement); /** Deletes the first element in this list and returns a reference to it. Throws an EmptyCollectionException if the list is empty. Pre: Post: the first element is removed from the list Return: a reference to the removed element */ public E deleteHead(); /** Removes the last element in this list and returns a reference to it. Throws an EmptyCollectionException if the list is empty. Pre: Post: the last element is removed from the list Return: a reference to the removed element */ public E deleteTail(); /** Removes the first instance of the specified element from this list and returns a reference to it. Throws an EmptyCollectionException if the list is empty. Throws a ElementNotFoundException if the specified element is not found in the list. Pre: targetElement :: E, the target element to be removed Post: the element is removed from the list Return: a reference to the removed element */ public E deleteElement(E targetElement);

/** Insert a new node to the head of the list. Pre: item :: E, content in the new node Post: new node is inserted to the head of the list Return: nothing */ public void insertHead(E item); /** Insert a new node to the end of the list. Pre: item :: E, content in the new node Post: new node is inserted to the end of the list Return: nothing */ public void insertTail(E item); /** Insert a new node at a specific position in the list. Pre: item :: E, content in the new node position :: the position of the new node Post: new node is inserted at position in the list Return: nothing */ public void insertAtPosition (E item, int position); /** Retrive the element at a specific position in the list. Pre: position :: Integer, a position in the list Post: the list is unchanged Return: the element at the position */ public E get(int position); /** * Useful method for pretty print Pre: Post: the list is unchanged. Return: Returns a string representation of this list. */ public String toString()}

2.public class TestList { public static void main(String[] args) { List L = new BasicLinkedList<> (); L.insertTail(7); L.insertTail(5); if (L.isEmpty()) System.out.println("The list is empty"); else System.out.println("The list is not empty"); L.insertHead(10); L.insertHead(18); L.insertAtPosition(12, 3); System.out.println("Current size of the list: " + L.size()); System.out.println("The 3rd element in the list is: " + L.get(3)); Integer el = 11; if(L.contains(el)) System.out.println(el + " is found in the list"); else System.out.println(el + " is not found in the list"); System.out.println("The current content in the list is : " + L.toString()); L.deleteHead(); System.out.println("After removing head, the current content in the list is : " + L.toString()); L.deleteTail(); System.out.println("After removing tail, the current content in the list is : " + L.toString()); el = 10; L.deleteElement(el); System.out.println("After removing the element " + el + ", the current content in the list is : " + L.toString()); L.insertTail(20); System.out.println("Current size of the list: " + L.size()); } i will post third program in next question its too long

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

socialist egalitarianism which resulted in wage levelling;

Answered: 1 week ago

Question

soyuznye (all-Union, controlling enterprises directly from Moscow);

Answered: 1 week ago