Question
The linked list is at the core of most data structures. We see it in lists, stacks, queues and trees to name a few. From
The linked list is at the core of most data structures. We see it in lists, stacks, queues and trees to name a few.
From the lecture material create a linked list class. This class should be generic so that it will operate on any given type. In C++ we call this a template class. In Java it is a Generic. Of course in Java a generic will not operate on a primitive type but that should be ok for what we are doing
Your class should have the following functionality:
- void addToEnd(T data) - adds an item to the end of the list
- void insert(T search, T data) - Adds data as a new Node following the node that contains search
- deleteNode(T search) - Deletes the node that contains search.
- void printList() - Prints the entire linked list.
- bool contains(T search) - returns true if the list contains T and false if it doesn't
If you find there is other functionality that you feel is needed to make your list better then by all means feel free to add it.
Using the PairList class you created in project 2 remove the vector and replace it with the LinkedList you created in Task 1 above. This means that your PairList should now be derived from LinkedList and not vector.
import java.util.ArrayList;
public class PairList extends ArrayList { void addPair(T first, T second) { add(new Pair(first,second)); } T getFirst(T second) { for(Pair P : this) { if(P.getSecond().equals(second)) { return (T)P.getFirst(); } } return null; } T getSecond(T first) { for(Pair P : this) { if(P.getFirst().equals(first)) { return (T)P.getSecond(); } } return null; } void deletePair(T first, T second) { for(int x=0; x();> { Pair P = get(x); if(P.getFirst().equals(first) && P.getSecond().equals(second)) { remove(P); } } } void printList() { int count = 0; for(Pair P : this) { System.out.println( " Index [" + count + "]: " + P); count+=1; } } }
JAVA ONLY
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started