![Page 1: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/1.jpg)
Stacks and Queues
Departamento de Ingeniería Telemática
Systems Programming
1
![Page 2: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/2.jpg)
Stacks
Queues
Deques – double-ended queues
Contents
2
![Page 3: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/3.jpg)
3
• Linear data structures
• Insertion and extraction into/from the (same) end
LIFO (Last-In-First-Out)
Stacks
![Page 4: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/4.jpg)
4
• Insert into one end: push(x)
• Extract from the same end: pop()
Stacks
![Page 5: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/5.jpg)
Interface for stacks
5
public interface Stack<E> {
boolean isEmpty();
int size();
E top();
void push(E info);
E pop();
}
![Page 6: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/6.jpg)
One interface, two implementations
6
• Array-based implementation:
ArrayStack
• Linked-list-based implementation:
LinkedStack
![Page 7: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/7.jpg)
ArrayStack
7
top
0 1 2 3 4 5 6 … … N-1
top
1
0 1 2 3 4 5 6 … … N-1
top
1 2 3 4
0 1 2 3 4 5 6 … … N-1
Empty stack
Stack with 1 element
Stack with 4 elements
![Page 8: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/8.jpg)
Exercise 1
8
• Create the ArrayStack class, with three attributes: int
capacity, the Object data array and int top with -1
as initial value.
• Create the class constructor, which takes just one
parameter to initialise the capacity attribute and
creates an array of such capacity.
• Implement the following methods:
o boolean isEmpty()
o int size()
o void push(Object info)
• Homework: implement these methods:
o Object pop()
o Object top()
![Page 9: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/9.jpg)
LinkedStack
9
Empty stack
Stack with 1 element
Stack with 4 elements
End for insertion and
extraction
![Page 10: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/10.jpg)
Remembering the Node class
10
public class Node<E> {
private E info;
private Node<E> next;
public Node() {…}
public Node(E info) {…}
public Node(E info, Node<E> next) {…}
public Node<E> getNext() {…}
public void setNext(Node<E> next) {…}
public E getInfo() {…}
public void setInfo(E info) {…}
}
![Page 11: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/11.jpg)
LinkedStack (I)
11
public class LinkedStack<E> implements Stack<E> {
private Node<E> top;
private int size;
public LinkedStack() {
top = null;
size = 0;
}
public boolean isEmpty() {
return (size == 0);
}
public int size() {
return size;
}
public E top() {
if(isEmpty()){
return null;
}
return top.getInfo();
}
…
Constructor
Attributes
Stack interface methods to
implement (I)
![Page 12: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/12.jpg)
LinkedStack (II)
12
…
public void push(E info){
Node<E> n = new Node<E>(info, top);
top = n;
size++;
}
public E pop() {
E info;
if(isEmpty()){
return null;
} else{
info = top.getInfo();
top = top.getNext();
size--;
return info;
}
}
}
Stack interface methods
to implement (II)
![Page 13: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/13.jpg)
13
• Linear data structures
• Insertion into one end and extraction from the
opposite end
FIFO (First-In-First-Out)
Queues
![Page 14: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/14.jpg)
14
• Insert into one end: enqueue(x)
• Extract from the opposite end: dequeue()
Queues
![Page 15: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/15.jpg)
Interface for queues
15
public interface Queue<E> {
boolean isEmpty();
int size();
E front();
void enqueue (E info);
E dequeue();
}
![Page 16: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/16.jpg)
One interface, two implementations
16
• Array-based implementation:
ArrayQueue
• Linked-list-based implementation:
LinkedQueue
![Page 17: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/17.jpg)
ArrayQueue
17
top
0 1 2 3 4 5 6 … … N-1
1
0 1 2 3 4 5 6 … … N-1
tail
1 2 5 9 3
0 1 2 3 4
4
5 6 … … N-1
Empty queue
Insertion of 1 element
Insertion of 5 extra elements
tail
top
tail
top
tail
5 9 3
0 1 2 3 4
4
5 6 … … N-1
Extraction of 2 elements
top
![Page 18: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/18.jpg)
LinkedQueue
18
Empty queue
Queue with 1element
Queue with 4 elements
Extraction end Insertion end
![Page 19: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/19.jpg)
LinkedQueue (I)
19
public class LinkedQueue<E> implements Queue<E> {
private Node<E> top = null;
private Node<E> tail = null;
private int size = 0;
public LinkedQueue(){
top = null;
tail = null;
size = 0;
}
public boolean isEmpty() {
return (size == 0);
}
public int size() {
return size;
}
public E front() {
if (isEmpty()){
return null;
} else {
return top.getInfo();
}
}
…
Constructor
Attributes
Queue interface methods
to implement(I)
![Page 20: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/20.jpg)
LinkedQueue (II)
20
…
public void enqueue (E info){
Node<E> n = new Node<E>(info, null);
if (isEmpty()){
top = n;
} else {
tail.setNext(n);
}
tail = n;
size++;
}
…
Queue interface methods
to implement (II)
![Page 21: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/21.jpg)
LinkedQueue (III)
21
…
public E dequeue(){
E info;
if (isEmpty()){
return null;
}
info = top.getInfo();
top = top.getNext();
if (isEmpty()){
tail = null;
}
size--;
return info;
}
}
Queue interface methods
to implement(III)
![Page 22: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/22.jpg)
22
• Linear data structures
o Deque (double-ended queue)
• Insertion and extraction from any end
Double-ended queues (deques)
![Page 23: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/23.jpg)
Interface for deques
23
public interface Deque<E> {
public boolean isEmpty();
public int size();
public E first();
public E last();
public void insertFirst(E info);
public void insertLast(E info);
public E removeFirst();
public E removeLast();
}
![Page 24: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/24.jpg)
Interface for deques
24
Stack Deque size() size()
isEmpty() isEmpty()
top() last()
push(x) insertLast(x)
pop() removeLast()
Queue Deque size() size()
isEmpty() isEmpty()
front() first()
enqueue(x) insertLast(x)
dequeue() removeFirst()
![Page 25: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/25.jpg)
Implementation of deques
25
• (regular) linked lists are not the best idea
because removeLast needs to traverse the list
from the beginning to find the reference to the
next-to-last element
• Solution: doubly-linked lists
![Page 26: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/26.jpg)
Doubly-linked lists
26
• Linked lists where each node, in addition to the
information and the reference to the next node in
the list, also stores a reference to the previous node
o The list can be traversed in both directions
o The cost to extract the last node is reduced
top
info info info
prev
next
prev
next
prev
next null null
tail
![Page 27: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/27.jpg)
DLNode class
public class DLNode<E> {
private E info;
private DLNode<E> prev;
private DLNode<E> next;
public DLNode() {…}
public DLNode(E info) {…}
public DLNode(E info, DLNode<E> prev, DLNode<E> next){…}
public DLNode<E> getNext(){…}
public void setNext(DLNode<E> next){…}
public DLNode<E> getPrev(){…}
public void setPrev(DLNode<E> prev){…}
public E getInfo(){…}
public void setInfo(E info){…}
}
27
![Page 28: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/28.jpg)
Exercise 2
28
• Complete the code for the DLNode class. Add three
constructors: one with no parameters, a second one
that allows to initialise the info attribute, and
another constructor to initialise all attributes.
![Page 29: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/29.jpg)
Doubly-linked lists
29
• The implementation of deques based on linked lists needs to check
in each operation that both the previous and the next node exist
• Simplification: Create two special nodes (dummy nodes), with no
data, one at the beginning and another at the end of the list:
o An empty list only contains these two nodes.
o In each insertion or extraction operation, both the previous and the next
node always exist, without needing to check.
o top and tail references never change.
![Page 30: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/30.jpg)
Double queue class (DLDeque) with
doubly-linked lists
30
public class DLDeque<E> implements Deque<E>{
private DLNode<E> top;
private DLNode<E> tail;
private int size;
public DLDeque(){
top = new DLNode<E>();
tail = new DLNode<E>();
tail.setPrev(top);
top.setNext(tail);
size = 0;
}
…
Constructor
Attributes
![Page 31: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/31.jpg)
Exercise 3
31
• Implement the following methods in the DLDeque
class:
o boolean isEmpty()
o int size()
o E first()
o E last()
![Page 32: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/32.jpg)
32
public void insertFirst(E info) {
DLNode<E> second = top.getNext();
DLNode<E> first = new DLNode<E>(info, top, second);
second.setPrev(first);
top.setNext(first);
size++;
}
Double queue class (DLDeque) with doubly-
linked lists: Insertion at the beginning
![Page 33: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/33.jpg)
33
public E removeFirst() { if (top.getNext() == tail){
return null;
}
DLNode<E> first = top.getNext();
E info = first.getInfo();
DLNode<E> second = first.getNext();
top.setNext(second);
second.setPrev(top);
size--;
return info;
}
Double queue class (DLDeque) with doubly-linked
lists: Extraction from the beginning
Madrid Miami Múnich Moscú
first top
second tail
![Page 34: Stacks and Queues - UC3MDoubly-linked lists 29 • The implementation of deques based on linked lists needs to check in each operation that both the previous and the next node exist](https://reader034.vdocuments.us/reader034/viewer/2022042316/5f0592717e708231d413a036/html5/thumbnails/34.jpg)
Exercise 4
34
• Implement the following methods in the DLDeque
class:
o void insertLast (E info)
• Homework:
o E removeLast()