Download Abstract Data Types Data Structures Example: Stacks and Queues

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Array data structure wikipedia , lookup

Transcript
Data Structures
Abstract Data Types
A data structure realising an ADT consist of:
An abstract data type (ADT) consists of:
a mathematical model of the data
collections of variables for storing the data described by the mathematical model underlying the ADT
methods for accessing and modifying the data
algorithms for the methods of the ADT.
Example
The Dictionary ADT of Practical 5.
Remark
ADT
data structure
JAVA interface
JAVA class
Example: Stacks and Queues
Sequential Data
A Stack is an ADT for storing a collection of elements that supports the
following methods:
push : Insert element .
pop : Remove the most recently inserted element and return it; an
error occurs if the stack is empty.
isEmpty : Return TRUE if the stack is empty and FALSE otherwise.
Mathematical model of the data: a linear sequence of elements.
A Queue is an ADT for storing a collection of elements that supports the
following methods:
enqueue : Insert element .
dequeue : Remove the element inserted the longest time ago and
return it; an error occurs if the queue is empty.
isEmpty : Return TRUE if the queue is empty and FALSE otherwise.
A sequence has well-defined first and last elements.
Every element of a sequence except the first and last has a unique
predecessor and successor.
Both Stack and Queue can easily be realised by data structures based
on arrays or linked lists.
The rank of an element in a sequence
before in .
is the number of elements
Arrays and Linked Lists in Memory
Arrays and Linked Lists Abstractly
An array, a singly linked list, and a doubly linked list storing objects
:
o1
o3
o2
o4
An array, a singly linked list, and a doubly linked list storing
:
o5
o3
o5
o2
o1
o2
o1
o3
o4
o4
o2
o1
o1
o3
o1
o2
o5
o4
o3
o2
o4
o3
o5
o4
o5
Vectors
A Vector is an ADT for storing a sequence of elements that supports
the following methods:
elemAtRank : Return the element of rank ; an error occurs if
or
.
An Array Based Data Structure for Vector
: Replace the element of rank with ; an error
.
replaceAtRank
occurs if
or
(storing the elements)
insertAtRank
: Insert a new element at rank (this increases
or
the rank of all following elements by 1); an error occurs if
.
Variables
Array
size : Return , the number of elements in the sequence.
removeAtRank : Remove the element of rank (this reduces the
or
rank of all following elements by 1); an error occurs if
.
Integer
= number of elements in the sequence
o5
Abstract Lists
An Array Based Data Structure for Vector (cont’d)
Methods
List is an ADT for storing a sequence that supports the following methods:
element : Return the element at position .
first : Return the position of the first element; an error occurs if the
list is empty.
isEmpty : Return TRUE if the list is empty and FALSE otherwise.
next : Return the position of the element following the one at
position ; an error occurs if is the last position.
isLast : Return TRUE if is the last position of the list and FALSE
otherwise.
replace
: Replace the element at position with .
insertFirst : Insert as the first element of the list.
insertAfter
: Insert element after position .
remove : Remove the element at position .
Plus: last , previous , isFirst , insertLast , and
insertBefore
1.
Algorithm replaceAtRank
Algorithm elemAtRank
1. return
Algorithm removeAtRank
1. for
to
do
2.
3.
3.
4.
do
2.
Algorithm insertAtRank
1. for
downto
Algorithm size
1. return
is big enough)
(insertAtRank assumes that array
Realising List with Doubly Linked Lists
Sequences
Variables
Positions of a List are realised by nodes having fields element, previous, next.
Sequence is an ADT for sequences combining the methods of Vector
and List. It can be realised by data structures using arrays or linked list.
The whole list is accessed through two node-variables first and last.
Methods (Two Examples)
doubly linked list
next
previous
array
Algorithm remove
1.
previous next
2.
next previous
3. delete
method
size,isEmpty,element,
first,last,next,previous,
replace,isFirst,isLast,insertLast
elemAtRank,replaceAtRank
insertFirst,insertAfter,insertBefore
insertAtRank,removeAtRank
Algorithm insertAfter
1. create a new node
2.
element
3.
next
next
4.
previous
5.
next
Running times
Dynamic Insertion
Dynamic Arrays
Algorithm insertLast
1. if
length then
5.
6.
Thus the amortised running time of one insertion is
.
Theorem
Inserting elements into an initially empty VeryBasicSequence us.
ing the Algorithm insertLast requires time
.
The worst case running time of insertLast is
Observation
Analysis
9.
10.
8.
size : Return , the number of elements in the sequence.
to the sequence.
: Append element
length , i.e., the array is full
length
Create new array
of length
for
to
do
4.
3. else
7.
insertLast
: Replace the element of rank with ; an error
.
2.
replaceAtRank
or
occurs if
VeryBasicSequence is an ADT for storing a sequence that supports the
following methods:
elemAtRank : Return the element of with rank ; an error occurs
if
or
.