Data Structure
Data Structure
Data Structure
An abstract data type (ADT) is an organized collection of information and a set of operations used to manage that information The set of operations defines the interface to the ADT
As long as the ADT fulfills the promises of the interface, it doesn't really matter how the ADT is implemented Objects are a perfect programming mechanism to create ADTs because their internal details are encapsulated
1
Abstraction
Our data structures should be abstractions
What do we mean by makes it possible to change the implementation without changing the interface?
Why is changing the implementation without changing the interface desirable?
A set of operations defines the interface to the ADT. What are they for a stack?
This meaning is different from the meaning of the static modifier (variable shared among all instances of a class)
Arrays are static; once you define the number of elements it can hold, the number doesnt change A dynamic data structure grows and shrinks at execution time as required by its contents A dynamic data structure is implemented using links
4
Dynamic Implementations
The representation should facilitate the intended operations and should make them easy to implement.
Classic nonlinear data structures include trees, binary trees, graphs, and digraphs
Queues
We can define the operations for a queue
enqueue - add an item to the rear of the queue dequeue (or serve) - remove an item from the front of the queue empty - returns true if the queue is empty
As with our linked list example, by storing generic Object references, any object can be stored in the queue Queues often are helpful in simulations or any situation in which items get backed up while awaiting processing (Jobs waiting their turn to be processed.)
8
Queues
A queue can be represented by a singly-linked list. Operationrs: enqueue add an item to rear dequeue remove an item from front empty returns true if queue is empty Is it more efficient if the references point from front to the rear?
rear info4
null info4
info3
next info3
info2
next info2
front info1
next info1
queue
queue
next rear
next
next
null front
Queues
A queue can be represented by an array What may happen as queue grows via enqueue with no immediately occurring dequeues?
Stacks
A stack ADT is also linear, like a list or a queue
Items are added and removed from only one end of a stack
It is therefore LIFO: Last-In, First-Out Analogies:
a stack of plates in a cupboard a stack of bills to be paid a stack of hay bales in a barn
11
push
pop
12
Stacks
Some stack operations: push - add an item to the top of the stack pop - remove an item from the top of the stack peek (or top) - retrieves the top item without removing it empty - returns true if the stack is empty A stack can be represented by a singly-linked list; it doesnt matter whether the references point from the top toward the bottom or vice versa A stack can be represented by an array, but the new item should be placed in the next available place in the array rather than at the end of the array (What can happen with an array implementation?) 13
Stacks
The java.util package contains a Stack class