That's where the toList method comes in. When called, this method returns a List representation of the Deque]. For example, if the Deque] has had addLast (5), called on it, then the result of addLast (9) addLast (10), then addFirst (3) 9 toList() should be a List with 3 at the front, then 5, then 9, then 10. If printed in Java, it'd show up as [3, 5, 9, 10]. Write the toList method. The first line of the method should be something like List returnList = new ArrayList<>()]. This is one location where you are allowed to use a Java data structure.
That's where the toList method comes in. When called, this method returns a List representation of the Deque]. For example, if the Deque] has had addLast (5), called on it, then the result of addLast (9) addLast (10), then addFirst (3) 9 toList() should be a List with 3 at the front, then 5, then 9, then 10. If printed in Java, it'd show up as [3, 5, 9, 10]. Write the toList method. The first line of the method should be something like List returnList = new ArrayList<>()]. This is one location where you are allowed to use a Java data structure.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Expert Solution
Step 1 : Code (Explained with comment)
Copy function's internal part
import java.util.List;
import java.util.ArrayList;
public class Deque<T> {
class ListNode<T> {
T v;
ListNode<T> next;
ListNode<T> prev;
ListNode(T v) {
this.v = v;
}
ListNode(T v, ListNode<T> next, ListNode<T> prev) {
this.v = v;
this.next = next;
this.prev = prev;
}
}
int size;
ListNode<T> sentinel;
Deque() {
sentinel = new ListNode<T>(null);
sentinel.next = sentinel;
sentinel.prev = sentinel;
size = 0;
}
public void addFirst(T x) {
ListNode<T> newNode = new ListNode<T>(x, sentinel.next, sentinel);
sentinel.next.prev = newNode;
sentinel.next = newNode;
size++;
}
public void addLast(T x) {
ListNode<T> newNode = new ListNode<T>(x, sentinel, sentinel.prev);
sentinel.prev.next = newNode;
sentinel.prev = newNode;
size++;
}
public List<T> toList() {
// COPY CODE FROM HERE------------------------------
List<T> returnList = new ArrayList<>();
ListNode<T> p = sentinel.next; // sentinel.next is the first node
while (p != sentinel) { // sentinel is the last node
returnList.add(returnList.size(), p.v); // add to the end of the list
p = p.next; // move to the next node
}
return returnList; // return the list
// TO HERE-------------------------------------------
}
public static void main(String[] args) {
Deque<Integer> deque = new Deque<Integer>();
deque.addLast(5);
deque.addLast(9);
deque.addLast(10);
deque.addFirst(3);
System.out.println(deque.toList());
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education