{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lect4 - Stacks Queues Deques CSC212 1 Stacks Queues Stack...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Stacks, Queues & Deques CSC212
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Stacks & Queues Stack: Last In First Out (LIFO). Used in procedure calls, to compute arithmetic expressions etc. Queue: First In First Out (FIFO). Used in operating systems, simulations etc. Priority Queues: Highest priority item is served first. Used in operating systems, printer servers etc.
Background image of page 2
3 Stack (Linked Implementation) TOP
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 ADT Stack: Specification Elements: The elements are of a variable type <Type>. In a linked implementation an element is placed in a node. public class Node<T> extends Object { public T data; public Node<T> next; public Node () { data = null; next = null; } public Node (T val) { data = val; next = null; } }
Background image of page 4
5 ADT Stack: Specification Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element called top . Domain: the number of elements in the stack is bounded therefore the domain is finite. Type of elements: Stack
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 ADT Stack: Specification Operations: All operations operate on a stack S. 1. Method Push (Type e) requires: Stack S is not full. input: Type e. results: Element e is added to the stack as its most recently added elements. output: none. 1. Method Pop (Type e) requires : Stack S is not empty. input : results : the most recently arrived element in S is removed and its value assigned to e. output : Type e. 1. Method Empty (boolean flag) input : results : If Stack S is empty then flag is true, otherwise false. output : flag.
Background image of page 6
7 ADT Stack: Specification Operations : 1. Method Full (boolean flag). requires : input : . results : If S is full then Full is true, otherwise Full is false. output : flag.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 ADT Stack (Linked Implementation) TOP Data Element Pointer A Linked Implementation of the Stack.
Background image of page 8
9 ADT Stack (Linked Implementation) public class LinkStack<T> { private Node<T> top; /* Creates a new instance of LinkStack */ public LinkStack() { top = null; } public boolean empty(){ return top == null; }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 ADT Stack (Linked Implementation) public boolean full(){ return false; } public void push(T e){ Node<T> tmp = new Node(e); tmp.next = top; top = tmp; }
Background image of page 10
11 ADT Stack (Linked Implementation) public T pop(){ T e = top.data; top = top.next; return e; } }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
12 Stack: Array Implementation public class ArrayStack<T> { private int maxsize; private int top; private T[] nodes; /** Creates a new instance of ArrayStack */ public ArrayStack(int n) { maxsize = n; top = -1; nodes = (T[]) new Object[n]; }
Background image of page 12
13 Stack: Array Implementation public boolean empty(){ return top == -1; } public boolean full(){ return top == maxsize-1; }
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
14 Stack: Array Implementation public void push(T e){ nodes[++top] = e; } public T pop(){ return nodes[top--]; } }
Background image of page 14
15 Applications of Stacks Some applications of stacks are: Balancing symbols.
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}