Meeting 2
Meeting 2
3
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the resource constraints a
solution must meet.
2. Determine the basic operations that must be supported. Quantify
the resource constraints for each operation.
3. Select the data structure that best meets these requirements.
4
Abstract Data Types
Abstract Data Type (ADT): a definition for a data type solely in terms of a
set of values and a set of operations on that data type.
5
Data Structure
• A data structure is the physical implementation of an ADT.
• Each operation associated with the ADT is implemented by one or more subroutines in
the implementation.
7
What is Program
• Data structure affects the design of both structural & functional aspects of a program.
• You know that a algorithm is a step by step procedure to solve a particular function.
• A Set of Instructions
• That means, algorithm is a set of instruction written to carry out certain tasks & the data structure is the way of
organizing the data with their logical relationship retained.
• To develop a program of an algorithm, we should select an appropriate data structure for that algorithm.
Address 4
Address 3 Address 3
Browser Push Address 2 Address 2 Address 2
Address 1 Address 1 Address 1
Stack - Operations
• a stack, S, is a container that supports the following two methods:
if (N − f + r) mod N = N − 1 then
return an error condition that the queue is full
Q[r] ← o
r ← (r + 1) mod N
return
Lists
• Stacks and queues store elements according to a linear sequence
determined by update operations that act on the “ends” of the
sequence.
• Lists maintain linear orders while allowing for accesses and updates
in the “middle.”
Lists – Index-Based Lists
• Suppose we are given a linear sequence, S, that contains n elements.
• We can uniquely refer to each element e of S using an integer in the
range [0, n−1].
• We define the index (or rank) of an element e in S to be the number of
elements that are before e in S.
• So, the first element in a sequence is at index 0 and the last element
is at index n − 1.
Index-Based Lists – Operations
• get(r): Return the element of S with index r; an error condition occurs
if r < 0 or r > n− 1.
• set(r, e): Replace with e the element at index r and return it; an error
condition occurs if r < 0 or r > n− 1.
• add(r, e): Insert a new element e into S to have index r; an error
condition occurs if r < 0 or r > n.
• remove(r): Remove from S the element at index r; an error condition
occurs if r < 0 or r > n− 1.
Index-Based Lists – Algorithm add(r, e)
if n = N then
return “Array is full.”
if r < n then
for i ← n − 1, n − 2, . . . , r do
A[i + 1] ← A[i] // make room for the new element
A[r] ← e
n←n+1
Index-Based Lists – Algorithm remove(r)
e ← A[r] // e is a temporary variable
if r < n− 1 then
for i ← r, r + 1, . . . , n − 2 do
A[i] ← A[i + 1] // fill in for the removed element
n←n−1
return e
Lists – Linked Lists
• A linked list as a container of elements that stores each element at a
node position and that keeps these positions arranged in a linear
order relative to one another.
Linked Lists – Operations
• first(): Return the position of the first element of S; an error occurs if S
is empty.
• last(): Return the position of the last element of S; an error occurs if S
is empty.
• before(p): Return the position of the element of S preceding the one
at position p; an error occurs if p is the first position.
• after(p): Return the position of the element of S following the one at
position p; an error occurs if p is the last position.
Linked Lists – Operations
• insertBefore(p, e): Insert a new element e into S before position p in S.
• insertAfter(p, e): Insert a new element e into S after position p in S.
• remove(p): Remove from S the element at position p.
Linked Lists – Algorithm insertAfter(p, e)
Inserting an element e after a position p in a linked list
Create a new node v
v.element ← e
v.prev ← p // link v to its predecessor
v.next ← p.next // link v to its successor
(p.next).prev ← v // link p’s old successor to v
p.next ← v // link p to its new successor, v
return v // the position for the element e
Linked Lists – Algorithm insertBefore(p, e)
Linked Lists – Algorithm remove(p)
Removing an element e stored at a position p in a linked list
t ← p.element // a temporary variable to hold the return value
(p.prev).next ← p.next // linking out p
(p.next).prev ← p.prev
p.prev ← null // invalidating the position p
p.next ← null
return t
•To Be Continue