Data Structures
Data Structures
Data structure
Primitive DS Non-Primitive DS
Non-Primitive DS
• Lists
• Stacks 2
1
• Queues 3 4
7 8
• Trees 5 6
F R
19
Examples
• Basic Types
– integer, real (floating point), boolean (0,1),
character
• Arrays
– A[0..99] : integer array
0 1 2 3 4 5 6 7 99
A 2 1 3 3 2 9 9 6
… 10
0 1 2 99
20
Abstract Data Type
21
ADT: Array
22
Description of various
Data Structures : Arrays
• An array is defined as a set of finite
number of homogeneous elements or
same data items.
• It means an array can contain one type of
data only, either all integer, all float-point
number or all character.
Arrays
Pop Pop
Push
What is this good for ?
• Page-visited history in a Web browser
What is this good for ?
• Page-visited history in a Web browser
• Undo sequence in a text editor
What is this good for ?
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Saving local variables when one function
calls another, and this one calls another
How should we represent it ?
• Write code in python ?
How should we represent it ?
• Write code in python ?
• Write code in C ?
How should we represent it ?
• Write code in python ?
• Write code in C ?
• Write code in Java ?
35
Exercise: Stacks
• Describe the output of the following series of stack
operations
– Push(8)
– Push(3)
– Pop()
– Push(2)
– Push(5)
– Pop()
– Pop()
– Push(9)
– Push(1)
36
C++ Run-time Stack
main() {
• The C++ run-time system keeps int i;
track of the chain of active i = 5; bar
functions with a stack foo(i); PC = 1
• When a function is called, the } m=6
run-time system pushes on the foo(int j)
stack a frame containing { foo
– Local variables and return value int k; PC = 3
k = j+1; j=5
– Program counter, keeping track of bar(k); k=6
the statement being executed }
• When a function returns, its bar(int m) main
frame is popped from the stack { PC = 2
and control is passed to the …
i=5
method on top of the stack }
37
Parentheses Matching
• Each “(”, “{”, or “[” must be paired with a
matching “)”, “}”, or “[”
– correct: ( )(( )){([( )])}
– correct: ((( )(( )){([( )])}
– incorrect: )(( )){([( )])}
– incorrect: ({[ ])}
– incorrect: (
Stacks 38
Parentheses Matching Algorithm
Algorithm ParenMatch(X,n):
Input: An array X of n tokens, each of which is either a grouping symbol, a
variable, an arithmetic operator, or a number
Output: true if and only if all the grouping symbols in X match
Let S be an empty stack
for i=1 to n do
if X[i] is an opening grouping symbol then
S.push(X[i])
else if X[i] is a closing grouping symbol then
if S.isEmpty() then
return false {nothing to match with}
if S.pop() does not match the type of X[i] then
return false {wrong type}
if S.isEmpty() then
return true {every symbol matched}
else
return false {some symbols were never matched}
Stacks 39
Array-based Stack
Algorithm size()
• A simple way of return t + 1
implementing the
Stack ADT uses an Algorithm pop()
array if empty() then
throw EmptyStackException
• We add elements else
from left to right t = t - 1
• A variable keeps track return S[t + 1]
of the index of the
top element
…
S
0 1 2 t
Array-based Stack (cont.)
• The array storing the
stack elements may
become full Algorithm push(o)
if t = S.length - 1 then
• A push operation will throw FullStackException
then throw a else
FullStackException t = t + 1
– Limitation of the S[t] = o
array-based
implementation
– An array which is
formed will be
homogeneous
…
S
0 1 2 t
41
Performance and Limitations
- array-based implementation of stack ADT
• Performance
– Let n be the number of elements in the stack
– The space used is O(n)
– Each operation runs in time O(1)
• Limitations
– The maximum size of the stack must be defined a
priori , and cannot be changed
– Trying to push a new element into a full stack causes
an implementation-specific exception
42
Singly Linked List
• A singly linked list is a
data structure next
consisting of a
sequence of nodes
• Each node stores
– element elem node
– link to the next node
A B C D
43
Stack with a Singly Linked List
• We can implement a stack with a singly linked list
• The top element is stored at the first node of the list
• The space used is O(n) and each operation of the
Stack ADT takes O(1) time
nodes
top t Æ
elements
10/9/23 1:22 PM
Vectors 48
A Queue
Queues
50
Array-based Queue
• Use an array of size N in a circular fashion
• Two variables keep track of the front and rear
– f index of the front element
– r index immediately past the rear element
• Array location r is kept empty
normal configuration
Q
0 1 2 f r
wrapped-around configuration
Q
0 1 2 r f
52
Queue Operations
• We use the modulo Algorithm size()
return (N + r – f) mod N
operator
(remainder of Algorithm isEmpty()
return (f = r)
division)
Q
0 1 2 f r
Q
0 1 2 r f
53
Queue Operations (cont.)
• Operation enqueue throws an Algorithm enqueue(o)
exception if the array is full if size() = N - 1 then
throw FullQueueException
• This exception is
else
implementation-dependent
Q[r] = o
r = (r + 1) mod N
Q
0 1 2 f r
Q
0 1 2 r f
54
Queue Operations (cont.)
• Operation dequeue Algorithm dequeue()
throws an exception if isEmpty() then
if the queue is throw EmptyQueueException
empty else
o = Q[f]
• This exception is f = (f + 1) mod N
specified in the return o
queue ADT
Q
0 1 2 f r
Q
0 1 2 r f
55
Performance and Limitations
- array-based implementation of queue ADT
• Performance
– Let n be the number of elements in the queue
– The space used is O(n)
– Each operation runs in time O(1)
• Limitations
– The maximum size of the queue must be defined a
priori , and cannot be changed
– Trying to enqueue an element into a full queue causes
an implementation-specific exception
Queue with a Singly Linked List
• We can implement a queue with a singly linked list
– The front element is stored at the head of the list
– The rear element is stored at the tail of the list
• The space used is O(n) and each operation of the Queue ADT takes O(1)
time
• NOTE: we do not have the limitation of the array based implementation
on the size of the stack b/c the size of the linked list is not fixed, I.e., the
queue is NEVER full.
r
nodes
rear
front
f Æ
elements
57