Using Doubly Linked List, and Sorting methods: (In Java) Introduction: In this p
ID: 3801921 • Letter: U
Question
Using Doubly Linked List, and Sorting methods: (In Java)
Introduction:
In this project, we use the same file structure of Artist and Art to extend the concepts of array and linked list and have them applied to sorting. The input files are named p7arts.txt and p7artists.txt.
Description:
As you have learned so far that array is a very efficient way of handling a collection of related data, but the problem is that the size needs to be predetermined and storage needs to be reserved. However, in many real world applications, the amount of data is hard to predict. So, we will need to employ dynamic memory allocation approach, which means we allocate storage only when needed.
Assignments:
Implement a doubly linked list class so that you can use it to arrange the input file to produce the output similar to the following (Name this file p7out(yourName).txt:
Sample Output:
Artistist ID
Artist Name
Art ID
Art Title
Appraised Value
1
Acconci
1038
Spring Flowers
800
1050
Cattle Ranch
10000
1103
Trail End
8000
2
Budd
1042
Coffee on the Trail
7544
3
Carpenter
1013
Superstitions
78000
1021
Bead Wall
14000
1034
Beaver Pole Jumble
28000
1063
Asleep in the Garden
110000
Your data structure should be an array of Artist and each artist will maintain a list that contains all his/her works in ascending order on ArtID. You may assume that p7artists.txt has been properly sorted and when you search for an artist, use Binary Search.
Design and implement your doubly linked list so that you will be able to place data in ascending order. There is plenty of information available online. For example, the following segment of code from the web should give you a good starting point
Hints:
(1) you need to have data arranged in order and (2) you need to use a doubly linked list.
What methods shall you include in your class? Be creative. You may just implement a minimal set of operations that enable you to sort the list in a convenient way.
public class DoublyLinkedListImpl<E> {
private Node head;
private Node tail;
private int size;
public DoublyLinkedListImpl(){
size = 0;
}
/**
* this class keeps track of each element information
* @author java2novice
*/
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
}
p7artist.txt:
p7arts.txt:
Artistist ID
Artist Name
Art ID
Art Title
Appraised Value
1
Acconci
1038
Spring Flowers
800
1050
Cattle Ranch
10000
1103
Trail End
8000
2
Budd
1042
Coffee on the Trail
7544
3
Carpenter
1013
Superstitions
78000
1021
Bead Wall
14000
1034
Beaver Pole Jumble
28000
1063
Asleep in the Garden
110000
Explanation / Answer
First of all,
1) you need to have data arranged in order
To fetch all the Data in less than one second .. I will use this below method.
boolean fileExists = Files.exists(Paths.get("C://p7arts.txt "), LinkOption.NOFOLLOW_LINKS);
if (fileExists) {
List<String> lines = Files.readAllLines(Paths.get("C://p7arts.txt "), StandardCharsets.UTF_8);
Iterator it = lines.iterator();
TreeSet set = new TreeSet();
while (it.hasNext()) {
set.add(it.next());
}
Once u add into Tree set. It will be completely arranged in ascending order .
Follow this process for other File too..
(2) you need to use a doubly linked list.
public class DoublyLinkedList<E> {
private Node head;
private Node tail;
private int size;
public DoublyLinkedList(){
size = 0;
}
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
}
There is one more Option like
Keeping a LinkedList in sorted ordered negates better benefit of a LinkedList which is the O(1) insertion at head and tail and normally this seems to be like a job for a TreeMap where the key is the ID and the value is the object. So i feel that would give you iteration in ID order plus much faster lookup.
What methods shall you include in your class? Be creative. You may just implement a minimal set of operations that enable you to sort the list in a convenient way.
try to use these below methods which might help u.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.