Docsity

Stacks and Queues - Data Structures | CSCI 3230, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Stacks and Queues - Data Structures | CSCI 3230 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Stacks and Queues

Stacks

Stack

A stack

 Last-in, first-out (LIFO)

property

 The last item placed on
the stack will be the first
item removed

 Analogy

 A stack of dishes in a
cafeteria

Stack of cafeteria dishes

Operations in Stack 

The main primitives of a stack are known as:

 Push  adds a new node
 Pop  removes a node

 Additional primitives can be defined:

 IsEmpty  reports whether the stack is empty
 IsFull  reports whether the stack is full
 Initialize  creates/initializes the stack
 Destroy  deletes the contents of the stack (may be implemented
by re-initializing the stack)

Operations in Stack

Operations in Stack

Stack Implementation  The Java Collections Framework includes a set of ready made data structure classes, including a Stack class.  However, we will create our own stack class order to learn how a stack is implemented.  Our class will be a bit simpler than the Collections Framework.

Stack Implementation

 A stack can be stored in:

 (^) a static data structure

OR

 (^) a dynamic data structure

 Static data structures

 (^) These define collections of data which are fixed in size when the program is compiled. An array is a static data structure.

 Dynamic data structures

 (^) These define collections of data which are variable in size and structure. They are created as the program executes, and grow and shrink to accommodate the data being stored.

Stack Implementation

Stack Implementation

Stack Implementation

Simple Applications of Stack:

Checking for Balanced Braces

A stack can be used to verify whether a program contains balanced braces

 An example of balanced braces

abc{defg{ijk}{l{mn}}op}qr

 An example of unbalanced braces

abc{def}}{ghij{kl}m

Checking for Balanced Braces Traces of the algorithm that checks for balanced braces

Implementations of Stack

Implementation of the stack that use a) an array; b) a linked list;