elementary data structures stacks, queues, and linked lists

32
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Upload: annice-lawson

Post on 03-Jan-2016

266 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

ELEMENTARY DATA STRUCTURES

ELEMENTARY DATA STRUCTURES

Stacks, Queues, and Linked Lists

Page 2: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Elementary Data Structures

Used as programming tools (stacks & queues)- software (i.e. compilers)- used in the operations of a certain task

Implemented in CPU instructions- hardware

Page 3: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

StacksStacks

Definition container of objects uses LIFO principle for object

operations operations are push and pop

Sample uses : Internet browsers Undo / Redo feature

Page 4: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Other Applications

Parsing- check for matching parenthesis or brackets- aid for algorithms applied to complex data structures (traverse nodes of a tree and searching vertices of a graph)

Page 5: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Basic Stack OperationsBasic Stack Operations

push(o)

pop()

Inserts object o onto top of stack

Input: object Output: none

Removes the top object of stack and returns it to calling method Input: none Output: object

Page 6: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Related Stack Operations

isEmpty()

isFull()

Returns a boolean indicating if stack is empty.

Input: noneOutput: boolean

Returns a boolean indicating if stack is full

Input: noneOutput: boolean

Page 7: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Other Stack OperationsOther Stack Operations

size() :

top()

Returns the number of objects in stack.

Input: noneOutput: integer

Returns the top object of the stack. Input: none

Output: object

Page 8: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

An Array-Based Stack

Create an array A of size NStore elements of stack S in array ALet t (integer) - index of the top

element of stack S S 0 1 2 3 t ... N-1

Page 9: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Array Implementation

Algorithm size()return t + 1

Algorithm isempty()return (t<0)

Algorithm push(o)if size()=N then throw a STACKEMPTYEXCEPTIONt=t+1S[t]=o

Page 10: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Array Implementation

Algorithm pop()if isempty() then throw a STACKEMPTYEXCEPTIONe=S[t]S[t]=nullt=t-1return e

Algorithm top()if isempty() then throw a STACKEMPTYEXCEPTIONreturn S[t]

Page 11: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Class Problem

Using the stack functions, make an algorithm that will determine that given a string x : a) has matching operands (i.e. (),{},[]) b) report an error if an operand is missing

Example :x = “(12xxs3e)”, correctx = “(sdfsdfs}”, error

Page 12: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Class Problem - Summary

1. Make an empty stack2. Read characters until end of file3. If the character is an open anything, push it in the

stack4. If it’s a close anything,then if stack is empty,

report an error, otherwise, pop the stack5. If popped symbol is not the corresponding opening

symbol, then report an error6. At end of file, if stack is not empty report an error

Page 13: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Stacks- other applications

Recursion- i.e. computing for N factorial

Operand parsing- evaluating an expression

- i.e. ((4*5)+(6+2)/5))

- postfix notation A*B+C = AB*C+

function calls- variables and routine position are saved

Page 14: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Stacks - Query

How can the undo/redo function be implemented using stacks ?

Page 15: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

QUEUESQUEUES

A queue is a container of objects that are inserted and removed according to the FIFO principle.

Operations: Enqueue - insert an item at the rear of queue

Dequeue - remove an item from the front of the queue

Sample uses : movie line printing queue

Page 16: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

The Queue Abstract Data Type

The Queue Abstract Data Type

enqueue(o)

dequeue()

Insert object o at the rear of the queue Input: object Output: none

Remove the object from the front of the queue and return it. Input: none Output: object

Page 17: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

The Queue Abstract Data Type

The Queue Abstract Data Type

size() : Returns the number of objects in the queue. Input: none Output: object

isEmpty(): Return a boolean indicating if the queue is empty. Input: none Output:boolean

front(): Return the front object of the queue.

Input: none Output: object

Page 18: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Array ImplementationArray Implementation

Create a queue using an array in a circular fashion

Queue consists of an N-element array Q and two integer variables:

f : index of the front elementr: index of the element after the rear one

Configurations : “normal” “wrapped around”

Page 19: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Queue ImplementationQueue Implementation

ff rrArray QArray Q

f is an index to a cell Q storing the first f is an index to a cell Q storing the first element, r is an index to the next element, r is an index to the next available cell in Qavailable cell in Q

Page 20: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Queues- Example

Print Jobs- All requests from workstations are enqueued to the print queue on a first come first served basis- The current (first) printjob is dequeued and sent to the printer for processing

Page 21: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Queue - Applications

I/O Request Handling File server - Workstation (print, data access,etc.)

Telephone Call Handling History functions in applications

Page 22: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Limitations - Using Arrays

Maximum size needs to be predetermined

Potential wastage of allocated memory

Need to handle overflow

Page 23: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Linked List ADTHead

next next next null

Each node stores a reference to an element Each node stores a reference to an element and a reference, called and a reference, called nextnext to another node to another node

Page 24: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Singly Linked List Queue Implementation

Singly Linked List Queue Implementation

Nodes connected in a chain by links

head tail

head of the list - front of the queue tail of the list - rear of the queue

Page 25: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Removing at the HeadRemoving at the Head

head tail

advance head reference

head tail

Page 26: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Inserting at the TailInserting at the Tail

create a new nodehead tail

chain it and move the tail reference

head tail

Page 27: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Other Advantages

Insertions and deletions are easier compared to an array implementation when dealing with “lists”

Page 28: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Linked Lists - Query

If a stack will be implemented using a linked list, is it better to assign the head or the tail as the top of the stack ? Why ?

Page 29: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Double-Ended QueuesDouble-Ended Queues

Data structure that allows insertion and deletion at the front and the rear end of the queue (pronounced as “deck”)

Page 30: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Doubly linked lists

A node in a doubly linked list is like a node in a singly linked list except that, in addition to the next link, it also has a prev link to the previous node in the list.

Advantage over singly linked lists : deletions at the tail of the list can be done in constant time

Page 31: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Doubly-Linked Lists

Page 32: ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

Linked Lists - Query

What is the running time to search for an entry in a linked list ? Deleting from the tail ? Inserting from the head ?