Lecture 2 M Allocation (ADT Arrays)
Lecture 2 M Allocation (ADT Arrays)
Lecture : 2
Static and Dynamic Allocation
(ADT & Arrays)
int ar[100];
Page ▪ 2
Arrays Review
• Initializing Arrays
type name [elements];
int x[5];
int x[3]={1,2,3}; must be
constant
int x[10]={1};
Page ▪ 3
Arrays Review
int a[3] = { 3, 4 };
• Accessing Arrays
array[index] ----------> cout<< x[2]; x[1]=100;
Page ▪ 4
Pointers revisions
Point to here,
point to there,
point to that,
point to this,
and point to nothing!
well, they are just memory addresses!!
Page ▪ 5
Pointers
int *s_rate;
s_rate is pointing to rate or is said a pointer
to rate
Page ▪ 6
Pointers
Page ▪ 7
Pointers(operators)
Page ▪ 8
Pointers(operators)
int main() {
int value = 10;
int* ptr = &value; // Pointer variable "ptr" stores the memory address of
"value"
*ptr = 50; // Dereferencing ptr and assigning the value 50 to the memory
location it points to
return 0;
}
Page ▪ 9
Introduction to Data Structures
▪What is Data ?
– Any useful information – that can be stored or
memorized for future reference
• In an organization it can be
– Employee’s name, sex, age, department, address so on
• In Railway reservation office
– Passenger name, traveling date, traveling time, seat number so on
• In Computers
– Set of integers, Structure(s) or any other user defined data type
Page ▪ 10
Static And Dynamic Allocation
Stack allocation Static Array
int intArray[10];
intArray[0] = 6837;
int *intArray;
intArray = new int[10];
intArray[0] = 6837;
...
delete[] intArray;
Page ▪ 11
Static And Dynamic Allocation (cout’d)
Page ▪ 12
Static & Dynamic Arrays
Page ▪ 13
Allocation- Dynamic Array
Page ▪ 17
Types of Data Structures
Page ▪ 18
Classification of data structures
➢Based Existence
❖Physical data structures
– Array
– Linked List
❖Logical data structures
– Can’t be created independently
– Stack, queues, trees, graph
Page ▪ 19
Classification of data structures
Page ▪ 20
Classification of data structures
➢Based on Representation
❖Linear Data structures
▪ Array
▪ Linked list
▪ Stack
▪ queue
❖Non Linear Data structure
▪ Trees
▪ Graph
Page ▪ 21
Operation of data structure
▪Traversing
– Accessing/vesting each data elements exactly once so
the certain elements in the data may be processed
▪Searching
– Finding the location of the given data elements(key)
▪Insertion
– Adding a new element to the structure
Page ▪ 22
Operation of data structure
▪Deletion
– Removing element data from the structure
▪Sorting
– Arrange the data elements in some logical fashion
• Ascending/descending
▪Merging
– Combine data elements from two or more data
structure into one
Page ▪ 23
Algorithms
• The operations defined on the data structures are generally
knows as algorithms
• For each data structure there has to be defined algorithms
• Algorithms are normally used to add, delete, modify, sort
items of a data structure
• All programming languages are generally equipped with
conventional data structures and algorithms.
• New data structures and the algorithms for manipulation can
be defined by the user
▪Design Issue
» The challenge is to select the most appropriate
data structure for the problem
Page ▪ 24
Arrays As ADT
Arrays ADT
▪Data
• Sequential collection of elements of the same type
• A collection of variables of the same type.
▪Operations :
• Create ()
• Fill
• Display()
• Append (int newitem)
• Insert (int index, int newitem)
• Search (int key)
Page ▪ 26
Arrays ADT
▪ Data
• Sequential collection of elements of the same type
• A collection of variables of the same type.
▪ Operations :
• Create ()
• Fill
void fillArray (int value) {
for (int i = 0; i < array.size(); i++) {
array[i] = value;
Page ▪ 27 }
Arrays ADT
▪ Operations :
• Display()
void printArray() {
for (int i = 0; i < array.size(); i++) {
cout << array[i] << " ";
Page ▪ 28
Arrays ADT
▪ Operations :
• Search(int key)
void search(int k) {
for (int i = 0; i < array.size(); i++) {
if ( array[i]== key);
cout << array[i] << " ";
Page ▪ 29
Arrays ADT
1
2
Page ▪ 30
Arrays ADT
Page ▪ 31
Arrays ADT
▪Operations :
• Delete (int index)
• Enlarge (newSize)
• Merge (Array other)
• getSize ()
• getLength ()
Page ▪ 32
Arrays ADT
▪Operations :
• Enlarge (newSize)
• Merge (Array other)
• getSize ()
• getLength ()
Page ▪ 33
Arrays ADT
▪Operations :
• Merge (Array other)
• getSize ()
• getLength ()
Page ▪ 34
Problem with Arrays
Page ▪ 36
Multidimensional Dynamic Arrays
m IntArrayPtr's
IntArrayPtr *
int's
Page ▪ 37
Deleting Multidimensional Arrays
Page ▪ 38