21
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Embed Size (px)

Citation preview

Page 1: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

COP3538 – Data Structures Using OOPChapter 4 – Stacks and Queues

Page 2: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Abstract Data Structures An array is a concrete structure

The Java language defines Structure Rules for storing and retrieving values Limitations

Uses of an array are limitless Limited only by the programmers imagination

Sometimes we require better-defined structures Abstract data structures

Improve the structure Additional storage and retrieval rules Additional limitations

1/20/2014 Jim Littleton - COP3538 2

Page 3: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Abstract Data Structures

Abstract data structures Commonly implemented using arrays Can be implemented using other structures

Abstract data structure examples Stacks Queues Priority queues

Each abstract data structure Solves a particular set of problems Has a unique set of advantages and disadvantages

1/20/2014 Jim Littleton - COP3538 3

Page 4: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Access (interface) Arrays

Directly accessed via an index (immediate) Search through values sequentially or logically Only one item can be accessed at a time

Abstract data structures An interface controls access to the values

Direct access is NOT possible by user (programmer) Implementation of the interface controls

Access to values How values are stored, retrieved and deleted

1/20/2014 Jim Littleton - COP3538 4

Page 5: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Stacks Stack properties

Access to only one item at a time Follows the Last In First Out (LIFO) logical process

Think of any natural “stack” of items Stack of dishes Stack of coins Stack of papers

Four basic operations (know these!) Push – place an item on top of the stack Pop – remove an item off the top of the stack Overflow – procedure to follow when the stack is full Underflow – procedure to follow when the stack is

empty1/20/2014 Jim Littleton - COP3538 5

Page 6: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Stacks Code examplepubic class Stack { private int maxSize; private int top = -1; private int[] array;

public Stack(int size) { maxSize = size; array = new int[maxSize]; }

public int pop() { return array[top--]; // Returns the topmost item }

public void push(int item) { array[++top] = item; // Places item on top of the stack }

public boolean isEmpty() { // Underflow operation return top == -1; // Test if stack is empty }

public boolean isFull() { // Overflow operation return top == maxSize – 1; // Test if stack is full }}

1/20/2014 Jim Littleton - COP3538 6

Page 7: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Stacks

Important notes Always call isEmpty() before calling pop()

Avoids ArrayIndexOutOfBounds exception Always call isFull() before calling push()

Avoids ArrayIndexOutOfBounds exception

Extremely useful in programming Following a sequence that requires backtracking Evaluating parenthesized expressions

Arithmetical expressions

1/20/2014 Jim Littleton - COP3538 7

Page 8: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Stacks Example use of a stack

Reverse the letters of a word Non-stack example

private class NonStackApp { public static void main(String[] args) { String s = “COP3538”; char[] c = new char[s.length()]; for(int x = 0; x < c.length; x++) { c[x] = s.charAt(x); } for(int x = c.length – 1; x >= 0; x--) { System.out.print(c[x]); } }}

1/20/2014 Jim Littleton - COP3538 8

Page 9: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Stacks Example use of a stack

Reverse the letters of a word Stack example

private class StackApp { public static void main(String[] args) { Stack stack = new Stack(); String s = “COP3538”; for(int x = 0; x < s.length(); x++) { stack.push(s.charAt(x)); } while(!stack.isEmpty()) { System.out.print(stack.pop()); } }}

1/20/2014 Jim Littleton - COP3538 9

Page 10: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Stacks

A stack is a very useful data structure Don’t have to keep track of array indices Limited access to the elements in the array

Elements are accessible only via the push and pop methods

Stack efficiency Push and pop take O(1) time

A constant value. Why?? Not dependent on the number of items in the stack No comparisons or swaps necessary

1/20/2014 Jim Littleton - COP3538 10

Page 11: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Queues A First In First Out (FIFO) data Structure

Remember: A Stack is a LIFO data structure Can be implemented using several structures

Array Linked-list Etc.

Very useful for many different applications Print queues Job queues Ready queues Keyboard queues

Jim Littleton - COP3538 111/20/2014

Page 12: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Queues Queue Terminology (Know the terms!!)

Front Points to the first element in the queue

Rear Points to the last element in the queue

Linear queue Front pointer always exists before the rear pointer

Circular queue The front and rear pointers can wrap around the queue

Deques (double-ended queue) Inserts and removals can occur on either end

Overflow Attempting to add an element to a full queue

Underflow Attempting to remove an element from an empty queue

Jim Littleton - COP3538 121/20/2014

Page 13: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Queues Queue methods

Insert() Method used to store data at the rear of the queue

Remove() Method used to retrieve data from the front of the queue

IsEmpty() Tests whether the queue is empty

The queue is considered empty if the front and rear are together

IsFull() Tests whether the queue is full

The queue is considered full if the rear equals the size of the queue Not necessarily true for Circular queues

Jim Littleton - COP3538 131/20/2014

Page 14: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Linear Queuepublic class LinearQueue { private int maxSize, front, rear, numElems; private int[] array;

public Queue(int size) { maxSize = size; array = new int[maxSize]; front = numElems = 0; rear = -1; }

public void insert(int value) { array[++rear] = value; numElems++; }

public boolean isEmpty() { return numElems == 0; }

Jim Littleton - COP3538 141/20/2014

public boolean isFull() { return numElems == maxSize; }

public int remove() { numElems--; return array[front++]; }

public int size() { return numElems; }}

Page 15: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Circular Queuepublic class CircularQueue { private int maxSize, front, rear, numElems; private int[] array;

public Queue(int size) { maxSize = size; array = new int[maxSize]; front = numElems = 0; rear = -1; }

public void insert(int value) { if(rear == maxSize - 1) { rear = -1; // Wrap around } array[++rear] = value; numElems++; }

public boolean isEmpty() { return numElems == 0; }

Jim Littleton - COP3538 151/20/2014

public boolean isFull() { return numElems == maxSize; }

public int remove() { if(front == maxSize) { front = 0; // Wrap around } numElems--; return array[front++]; }

public int size() { return numElems; }}

Page 16: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Queues Queue efficiency

Insert() and remove() occur in O(1) time Simply insert or remove at front and rear pointers

Minor adjustment of front and rear values Even in the case when the pointers wrap the queue (circular queue)

Not dependent on the number of items in the array No comparisons or swaps necessary

Jim Littleton - COP3538 161/20/2014

Page 17: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Priority Queue A priority queue (pQueue)

Similar to a regular queue Insert in rear (becomes inconsequential) Remove from front

Significant differences Items are organized by a key field

i.e., Last name, N#, etc. New items are inserted in their correct positions

The position of existing items in the queue may change Insertion time is slower for a pQueue compared to a regular queue Remove time is the same for both queue types

If the queue is full An existing item must be removed before a new item is added

Jim Littleton - COP3538 171/20/2014

Page 18: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Priority Queue Several useful applications

Scheduling queues Shortest job first Fewest resources first

Print queues Fewest pages first Jobs sent to faster printers first

A pQueue is no longer a FIFO queue!The FO item is based on the specified priority

Jim Littleton - COP3538 181/20/2014

Page 19: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Priority Queue pQueue Code

Jim Littleton - COP3538 191/20/2014

public class PriorityQueue { private int maxSize; private int numElems = 0; private int[] array;

public PriorityQueue(int size) { maxSize = size; array = new int[maxSize]; }

public void insert(int item) { int x; if(numElems == 0) { array[numElems++] = item; } else { x = numElems; while(x > 0 && item > array[x - 1]) { array[x] = array[x - 1]; x--; } array[x] = item; numElems++; } }

public boolean isEmpty() { return numElems == 0; } public boolean isFull() { return numElems == maxSize; }

public int remove() { return array[--numElems]; }}

This block of code is identical to the code that performs the copies in the Insertion Sort!

Page 20: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Priority Queue pQueue Efficiency

Remove occurs at O(1) time (immediate) Insert occurs at O(n) time

Items have to be copied to new locations To make room for the item being inserted

Jim Littleton - COP3538 201/20/2014

Page 21: COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues

Questions

Jim Littleton - COP3538 211/20/2014