- Home
- Documents
*Algorithms and Data Structures - STAR - ece250/materials/notes/Lecture7-Stack-Queue.pdf¢ ...*

prev

next

out of 23

View

0Download

0

Embed Size (px)

ECE250: Algorithms and Data Structures

Elementary Data Structures

Materials from CLRS: Chapter 10.1, 10.2

Ladan Tahvildari, PEng, SMIEEE Professor

Software Technologies Applied Research (STAR) Group

Dept. of Elect. & Comp. Eng.

University of Waterloo

Acknowledgements

v The following resources have been used to prepare materials for this course: Ø MIT OpenCourseWare Ø Introduction To Algorithms (CLRS Book) Ø Data Structures and Algorithm Analysis in C++ (M. Wiess) Ø Data Structures and Algorithms in C++ (M. Goodrich)

v Thanks to many people for pointing out mistakes, providing suggestions, or helping to improve the quality of this course over the last ten years: Ø http://www.stargroup.uwaterloo.ca/~ece250/acknowledgment/

Lecture 7 ECE250 2

Lecture 7 ECE250 3

Definition

v Data Type Ø The set of allowed values for a variable

v Data Structure Ø Systematic way of organizing and accessing data

v Principle of Abstraction Ø Focus on what not how when solving a problem

v Abstract Data Type Ø Set of elements together with a well-defined set of operations

on these elements

Lecture 7 ECE250 4

Abstract Data Types (ADTs)

v Allow to break work into pieces that can be worked on independently without compromising correctness

v Serve as specifications of requirements for the building blocks of solutions to algorithmic problems

v Encapsulate data structures and algorithms that implement them

v Provide a language to talk on a higher level of abstraction

Lecture 7 ECE250 5

Dictionary

v An element has a key part and a data part v Dictionary ADT – a dynamic set with methods:

Ø Search(S, k) – an access operation that returns a pointer x to an element where x.key = k

Ø Insert(S, x) – a manipulation operation that adds the element pointed to by x to S

Ø Delete(S, x) – a manipulation operation that removes the element pointed to by x from S

Lecture 7 ECE250 6

Dictionary (cont.)

v Dictionary ADT – a dynamic set with methods: Ø Minimum (S) – an access operation that returns a pointer to

the element of S with the smallest key

Ø Maximum (S) – an access operation that returns a pointer to the element of S with the largest key

Ø Successor (S, x) – an access operation that returns a pointer to the next larger element in S for a given x, or NIL if x is the maximum element

Ø Predecessor (S, x) – an access operation that returns a pointer to the next smaller element in S for a given x, or NIL if x is the minimum element

Lecture 7 ECE250 7

Dictionaries v Dictionaries store elements so that they can be

located quickly using keys v A dictionary may hold bank accounts

Ø each account is an object that is identified by an account number

Ø each account stores a wealth of additional information

Ø an application wishing to operate on an account would have to provide the account number as a search key

Lecture 7 ECE250 8

Dictionaries (cont.)

v Different data structures to realize dictionaries

v Arrays v Binary Trees

v Linked List v AVL Trees

v Queues v B-Trees v Stacks v Heaps v Hash Tables

Data Storage for ADT

v Data storage can be classified as either: – Contiguous storage – Node-based storage

v Examples:

– Contiguous storage is the array – Node-based storage is the linked list

Lecture 7 ECE250 9

Contiguous Storage

v An array stores n objects in a single contiguous space of memory

v Unfortunately, if more memory is required, a request for new memory usually requires copying all information into the new memory – In general, you cannot request for

the operating system to allocate to you the next n memory locations

Lecture 7 ECE250 10

Node-Based Storage

v Node-based storage such as a linked list associates two pieces of data with each item being stored: – The object itself, and – A reference to the next item

• In C++ that reference is the address of the next node

Lecture 7 ECE250 11

Lecture 7 ECE250 12

v A data structure in which the objects are arranged in a linear order with dynamic allocation

v Singly linked list – nodes (data, pointer) connected in a chain by links

Linked List ADT

Lecture 7 ECE250 13

ADT Operation

Time Complexity

isEmpty()

O(1)

first(), last(), insertFirst(), insertLast()

O(1)

after(p), insertAfter()

O(1)

before(p), insertBefore()

O(n)

replace(p,e)

O(1)

remove(d)

O(n)

Listed List – Running Time

Lecture 7 ECE250 14

Doubly Linked Lists

v To implement all of the linked list operations in constant time we use doubly linked lists

v A node of a doubly linked list has a next and a prev link

Lecture 7 ECE250 15

Lecture 7 ECE250 16

Stack ADT

v A stack is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle.

Ø Objects can be inserted at any time, but only the last (the most-recently inserted) object can be removed.

v Inserting an item is known as “pushing” onto the stack. “Popping” off the stack is synonymous with removing an item.

§ Push (S:Stack, x:element) - Inserts o onto top of S § Pop (S:Stack) - Removes the top object of stack S

Lecture 7 ECE250 17

Stack - An Array Implementation

v Create a stack using an array by specifying a maximum size N for our stack.

v The stack consists of an N-element array S and an integer variable t, the index of the top element in array S.

v Array indices start at 0, so we initialize t to -1

Lecture 7 ECE250 18

Stack – Pseudo Code

Algorithm push(o) if size()==N then return Error t=t+1 S[t]=o

Algorithm pop() if isEmpty() then return Error S[t]=null t=t-1

Algorithm size() return t+1

Algorithm isEmpty() return (t

Lecture 7 ECE250 19

Stack - Running Time

v The array implementation is simple and efficient (methods performed in O(1)).

v There is an upper bound, N, on the size of the stack. The arbitrary value N may be too small for a given application, or a waste of memory.

Lecture 7 ECE250 20

Queue ADT v A queue differs from a stack in that its insertion and removal

routines follows the first-in-first-out (FIFO) principle.

v Elements may be inserted at any time, but only the element which has been in the queue the longest may be removed.

v Elements are inserted at the rear (enqueued) and removed from the front (dequeued)

Front Rear Queue

Lecture 7 ECE250 21

Queue - An Array Implementation

v Create a queue using an array in a circular fashion

v A maximum size N is specified

v The queue consists of an N-element array Q and two integer variables:

Ø f, index of the front element (head – for dequeue) Ø r, index of the element after the rear one (tail – for enqueue)

Lecture 7 ECE250 22

Queue – Pseudo Code

Algorithm size() return (N-f+r) mod N

Algorithm isEmpty() return size()=0

Algorithm front() if isEmpty() then return Error return Q[f]

Algorithm dequeue() if isEmpty() then return Error Q[f]=null f=(f+1) mod N

Algorithm enqueue(o) if size = N - 1 then return Error Q[r]=o r=(r+1) mod N

Methods performed in O(1)

Stack and Queue Example

v Consider we have one stack (LIFO principle) data structure and one circular queue (FIFO principle) data structure; both in array implementations. Draw the content of the stack and the circular queue for “each step” in the following given order:

add(A), add(B), add(C), remove, add(D), add(E), remove, add(F), and add(G)

v Assume an initial size of 5 for the array in each data structure. v Remember to show “the top of the stack” and “the front and the back

of the circular queue” in each step. v In circular queue, assume we sacrifice one storage space to make a

difference between FULL and EMPTY.

Lecture 7 ECE250 23