Linked-lists always mess with me a bit so it’s good that I used one here. My solution is to use two classes:
StacksQueues.java as the main program (calls the operations and prints the results) and
Store.java as the data structure.
Store.java includes two internal classes:
Head is just the placeholder that links to the first item in the list and is created upon calling
new Store() from
StacksQueues. It also handles the
print() function that formats the double-linked-list as a string surrounding the comma-separated values with square-brackets. It was important to note that the head only links forward to the first node in the list, there is no previous call to get to the head since it’s not useful. Creating an iterator to move through the list calls
head.next so the head isn’t considered part of the list.
Nodes are each node in the DLL; they contain a
next link to the following node, a
prev link to the preceding node, and a
value of the node. The node constructor requires the internal value to store and each link has to be set based on where it is being added to the DLL.