7 | Data structures / graphs Flashcards
Definition: Data Structure
Data structure is a particular way of organizing and storing data in a computer so it can be accessed and modified efficiently.
Data structure
- collection of data values
- relationship among the values
- functions / operations which can be applied to the data
Definition: Array
Array β data structure consisting of a fixed number of items of the same types (items = elements) usually stored in contiguous storage cells.
Access to every element is obtained by specifying a single subscript or index.
Name an advantage of arrays
We can calculate the address of any given element in a
constant time
It takes time in the order of Ξ(1) to
* read a value of a single element
* change a value of a single element
Name a disadvantage of arrays
no dynamic value modification (fixed number of items, of same type)
What is the algorithm for initialising an array? what is the time complexity?
for i in 1 to n do:
tab(i) <- 0
time: cn, is in Theta(n)
What is the algorithm for finding the minimum of an array? what is the time complexity?
min = T(1)
for i in 1 to n do:
if min > T(i)
min = T(i)
time:
c_i + β(2<=i<=n) of c + c = 2c + (n-1)c β Ξ(n)
What is the algorithm for moving elements (when inserting a new element into an array)? what is the time complexity?
algorithm:
for i in n+1 to k+1
T(i) <- T(i-1)
T(k) <- a
time:
[β(k+1<= i<= n+1) of c ] + c
= [ (n+1) - (k+1) + 1)c + c
= (n+1)c + c
β Ξ(n)
Stack
Definition?
Stack β data structure in which items are added to and
subsequently removed from the structure on a last-in-first-out
(LIFO) basis
Stack
Operations on a stack?
push, pop
Stack
Push?
Add an item x to the stack (push)
counter β counter + 1
stack[counter] β x
Stack
Pop?
Remove an item from the stack (pop)
π₯ β stack[counter]
counter β counter - 1
Stack: 10, 3, 5
what happens when you pop, pop, push, pop, pop ?
β> you have an empty stack
Stack
implementation?
Implemented by an array whose index runs from 1 to the
maximum number of elements allowed along with a counter
Queue
Definition?
Queue β data structure in which items are added and removed
on a first-in-first-out (FIFO) basis
Queue
Operations?
Add an item to the queue (enqueue)
Remove an item from the queue (dequeue)
Stacks and Queues
Disadvantages?
- Maximum elements allowed should be known a priori
- Too little, will affect the performance
- Too many, will waste resources
Record
Definition?
Record β data structure comprised a fixed number of items,
called fields, that are possibly of different types
Two-dimensional array
Reading and modifying a value in ____ time
Reading and modifying a value in constant time
since the address of any given element can be
determined in constant time
Record
Access to the fields?
Access to the fields (dot notation) for Jeanne
Jeanne.name, Jeanne.age
Record
pointers
Record are commonly used in conjuction with pointers
type student = β person
says that student is a pointer to a record of type person
such a record can be created dynamically
student β new person
student β means the record that student points to
student β.name, student β.age are the fields
If a pointer has a value of nil, it does not point to any record
List
Definition?
List is a collection of items arranged in a certain order.
Unlike arrays and records, the number of items in a list is
usually not fixed and is not bounded a priori
List
An item of a list is called a ____.
An item of a list is called a node.
List
Two types of implementation?
List can be implemented
as an array
or as with use of pointers and records (for the nodes)
List
implemented as an array?
List can be implemented as an array (of integers)
type list = record
counter: 0 . . πππ₯πππππ‘β
value: array [1. . maxlength] of integers