Systems Programming Stacks and Queues Departamento de Ingeniera - - PowerPoint PPT Presentation

systems programming
SMART_READER_LITE
LIVE PREVIEW

Systems Programming Stacks and Queues Departamento de Ingeniera - - PowerPoint PPT Presentation

Systems Programming Stacks and Queues Departamento de Ingeniera Telemtica 1 Con Conten ents ts Stacks Queues Deques double-ended queues 2 Stacks Linear data structures Insertion and extraction into/from the


slide-1
SLIDE 1

Stacks and Queues

Departamento de Ingeniería Telemática

Systems Programming

1

slide-2
SLIDE 2

 Stacks  Queues  Deques – double-ended queues

Con Conten ents ts

2

slide-3
SLIDE 3

3

  • Linear data structures
  • Insertion and extraction into/from the (same) end

 LIFO (Last-In-First-Out)

Stacks

slide-4
SLIDE 4

4

  • Insert into one end: push(x)
  • Extract from the same end: pop()

Stacks

slide-5
SLIDE 5

Interface for stacks

5

public interface Stack<E> { boolean isEmpty(); int size(); E top(); void push(E info); E pop(); }

slide-6
SLIDE 6

One interface, two implementations

6

  • Array-based implementation:

 ArrayStack

  • Linked-list-based implementation:

 LinkedStack

slide-7
SLIDE 7

ArrayStack

7

top

1 2 3 4 5 6 … … N-1

top

1

1 2 3 4 5 6 … … N-1

top

1 2 3 4

1 2 3 4 5 6 … … N-1

Empty stack Stack with 1 element Stack with 4 elements

slide-8
SLIDE 8

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:
  • boolean isEmpty()
  • int size()
  • void push(Object info)
  • Homework: implement these methods:
  • Object pop()
  • Object top()
slide-9
SLIDE 9

LinkedStack

9

Empty stack Stack with 1 element Stack with 4 elements

End for insertion and extraction

slide-10
SLIDE 10

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) {…} }

slide-11
SLIDE 11

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)

slide-12
SLIDE 12

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)

slide-13
SLIDE 13

13

  • Linear data structures
  • Insertion into one end and extraction from the
  • pposite end

 FIFO (First-In-First-Out)

Queues

slide-14
SLIDE 14

14

  • Insert into one end: enqueue(x)
  • Extract from the opposite end: dequeue()

Queues

slide-15
SLIDE 15

Interface for queues

15

public interface Queue<E> { boolean isEmpty(); int size(); E front(); void enqueue (E info); E dequeue(); }

slide-16
SLIDE 16

One interface, two implementations

16

  • Array-based implementation:

 ArrayQueue

  • Linked-list-based implementation:

 LinkedQueue

slide-17
SLIDE 17

ArrayQueue

17

top

1 2 3 4 5 6 … … N-1

1

1 2 3 4 5 6 … … N-1

tail

1 2 5 9 3

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

1 2 3 4

4

5 6 … … N-1 Extraction of 2 elements

top

slide-18
SLIDE 18

LinkedQueue

18

Empty queue Queue with 1element Queue with 4 elements

Extraction end Insertion end

slide-19
SLIDE 19

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)

slide-20
SLIDE 20

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)

slide-21
SLIDE 21

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)

slide-22
SLIDE 22

22

  • Linear data structures
  • Deque (double-ended queue)
  • Insertion and extraction from any end

Double-ended ended queues queues (deques deques)

slide-23
SLIDE 23

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(); }

slide-24
SLIDE 24

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()

slide-25
SLIDE 25

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
slide-26
SLIDE 26

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

  • The list can be traversed in both directions
  • The cost to extract the last node is reduced

top

info info info prev next prev next prev next

null null tail

slide-27
SLIDE 27

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

slide-28
SLIDE 28

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.

slide-29
SLIDE 29

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:

  • An empty list only contains these two nodes.
  • In each insertion or extraction operation, both the previous and the next

node always exist, without needing to check.

  • top

and tail references never change.

slide-30
SLIDE 30

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

slide-31
SLIDE 31

Exercise 3

31

  • Implement the following methods in the DLDeque

class:

  • boolean isEmpty()
  • int size()
  • E first()
  • E last()
slide-32
SLIDE 32

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

slide-33
SLIDE 33

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

slide-34
SLIDE 34

Exercise 4

34

  • Implement the following methods in the DLDeque

class:

  • void insertLast (E info)
  • Homework:
  • E removeLast()