Docsity

Stack and Queue Data Structures: A Comprehensive Guide, Schemes and Mind Maps of Computer Applications

Partial preview of the text

Download Stack and Queue Data Structures: A Comprehensive Guide and more Schemes and Mind Maps Computer Applications in PDF only on Docsity!

Data Structures and

Algorithms

Data Structures and

Algorithms

LECTURE 03: STACK & QUEUE LECTURE 03: STACK & QUEUE

OverviewOverview

Stacks

Array-based stack

Stack implemented by a singly linked list

Stack class in java.util

Queues

Priority Queues

Queue interface in java.util

Reading at home

Stacks

Array-based stack

Stack implemented by a singly linked list

Stack class in java.util

Queues

Priority Queues

Queue interface in java.util

Reading at home

What is a stack?What is a stack?

A linear data structure

Can be accessed only at one of its ends for storing and

retrieving data

A stack is a Last In, First Out (LIFO) data structure

Anything added will go to the top

Anything removed will be taken from the top

Things are removed in the reverse order that they were inserted

A linear data structure

Can be accessed only at one of its ends for storing and

retrieving data

A stack is a Last In, First Out (LIFO) data structure

Anything added will go to the top

Anything removed will be taken from the top

Things are removed in the reverse order that they were inserted

Operations on stackOperations on stack

Applications of stacksApplications of stacks

Any sort of nesting (such as parentheses)

Evaluating arithmetic expressions (and other sorts of expression)

Implementing function or method calls

Keeping track of previous choices (as in backtracking )

Keeping track of choices yet to be made (as in creating a maze)

Undo sequence in a text editor

Auxiliary data structure for algorithms

Component of other data structure

Any sort of nesting (such as parentheses)

Evaluating arithmetic expressions (and other sorts of expression)

Implementing function or method calls

Keeping track of previous choices (as in backtracking )

Keeping track of choices yet to be made (as in creating a maze)

Undo sequence in a text editor

Auxiliary data structure for algorithms

Component of other data structure

Array-based stackArray-based stack

ļ‚š A simple way of implementing the Stack ADT uses an

array

ļ‚š We add elements from left to right

ļ‚š A variable top keeps track of the index of the top

element

ļ‚š A simple way of implementing the Stack ADT uses an

array

ļ‚š We add elements from left to right

ļ‚š A variable top keeps track of the index of the top

element

Array based ImplementationArray based Implementation

FunctionalitiesFunctionalities

ļ‚š Common

characteristics

Object[] a

int top, max

ļ‚š Constructors

ArrayStack(int

max)

ArrayStack() –

default max=

ļ‚š Common

characteristics

Object[] a

int top, max

ļ‚š Constructors

ArrayStack(int

max)

ArrayStack() –

default max=

ļ‚š Common behaviors

isEmpty()

isFull()

clear()

grow()

push()

top()

pop()

ļ‚š Common behaviors

isEmpty()

isFull()

clear()

grow()

push()

top()

pop()

Array implementation of StackArray implementation of Stack

Array implementation of StackArray implementation of Stack

ArrayList Implementation of StackArrayList Implementation of Stack

LinkedList Implementation of

Stack

LinkedList Implementation of

Stack

Use of Stack – Convert 10 to binaryUse of Stack – Convert 10 to binary

10

0

5

/

%

1

2

/

%

0

1

/

%

1

0

/

%

Stop

return return

1

print

0

print

return

1

print

return

0

print

Values 2

0 2

1 2

2 2

3

Example using stackExample using stack