Lecture 1: Introduction Adts Stacks/Queues: Cse3 373 Edition 2014
Lecture 1: Introduction Adts Stacks/Queues: Cse3 373 Edition 2014
CSE3 373
Edition 2014
Data structures
(Often highly non-obvious) ways to organize information to enable
efficient computation over that information
A data structure supports certain operations, each with a:
Meaning: what does the operation do/return
Performance: how efficient is the operation
Examples:
List with operations insert and delete
Stack with operations push and pop
Edition 2014 2 CSE373: Data Structures and Algorithms
Trade-offs
A data structure strives to provide many useful, efficient operations
But there are unavoidable trade-offs:
Time vs. space
One operation more efficient if another less efficient
Generality vs. simplicity vs. performance
We ask ourselves questions like:
Does this support the operations I need efficiently?
Will it be easy to use (and reuse), implement, and debug?
What assumptions am I making about how my software will
be used? (E.g., more lookups or more inserts?)
Edition 2014 3 CSE373: Data Structures and Algorithms
Terminology
Abstract Data Type (ADT)
Mathematical description of a thing with set of operations
Algorithm
A high level, language-independent description of a step-by-
step process
Data structure
A specific organization of data and family of algorithms for
implementing an ADT
Implementation of a data structure
A specific implementation in a specific language
Edition 2014 4 CSE373: Data Structures and Algorithms
Example: Stacks
The Stack ADT supports operations:
isEmpty: have there been same number of pops as pushes
push: takes an item
pop: raises an error if empty, else returns most-recently
pushed item not yet returned by a pop
(possibly more operations)
A Stack data structure could use a linked-list or an array or
something else, and associated algorithms for the operations
One implementation is in the library java.util.Stack
Edition 2014 5 CSE373: Data Structures and Algorithms
Why useful
The Stack ADT is a useful abstraction because:
It arises all the time in programming (e.g., see Weiss 3.6.3)
Recursive function calls
Balancing symbols (parentheses)
Evaluating postfix notation: 3 4 + 5 *
Clever: Infix ((3+4) * 5) to postfix conversion (see text)
We can code up a reusable library
We can communicate in high-level terms
Use a stack and push numbers, popping for operators
Rather than, create an array and keep indices to the
Edition 2014 6 CSE373: Data Structures and Algorithms
The Queue ADT
Operations
create
destroy
enqueue
dequeue
is_empty
Just like a stack except:
Stack: LIFO (last-in-first-out)
Queue: FIFO (first-in-first-out)
Just as useful and ubiquitous
Edition 2014 7 CSE373: Data Structures and Algorithms
F E D C B
enqueue dequeue
G A
Back Front
Circular Array Queue Data Structure
Edition 2014 8 CSE373: Data Structures and Algorithms
// Basic idea only!
enqueue(x) {
Q[back] = x;
back = (back + 1) % size
}
// Basic idea only!
dequeue() {
x = Q[front];
front = (front + 1) % size;
return x;
}
b c d e f
Q:
0 size - 1
front
back
What if queue is empty?
Enqueue?
Dequeue?
What if array is full?
How to test for empty?
What is the complexity of
the operations?
Can you find the k
th
element in the queue?
Linked List Queue Data Structure
Edition 2014 9 CSE373: Data Structures and Algorithms
b c d e f
front back
// Basic idea only!
enqueue(x) {
back.next = new Node(x);
back = back.next;
}
// Basic idea only!
dequeue() {
x = front.item;
front = front.next;
return x;
}
What if queue is empty?
Enqueue?
Dequeue?
Can list be full?
How to test for empty?
What is the complexity of
the operations?
Can you find the k
th
element in the queue?
Circular Array vs. Linked List
Array:
May waste unneeded space or
run out of space
Space per element excellent
Operations very simple / fast
Constant-time access to k
th
element
For operation insertAtPosition,
must shift all later elements
Not in Queue ADT
List:
Always just enough space
But more space per element
Operations very simple / fast
No constant-time access to k
th
element
For operation insertAtPosition
must traverse all earlier elements
Not in Queue ADT
Edition 2014 10 CSE373: Data Structures and Algorithms
The Stack ADT
Operations:
create
destroy
push
pop
top
is_empty
Can also be implemented with an array or a linked list
This was homework #5 of CSE143!
Like queues, type of elements is irrelevant
Edition 2014 11 CSE373: Data Structures and Algorithms
A
B
C
D
E
F
E D C B A
F