+
Elementary Data Structures
Data Structures
+
TOPICS
ARRAYS
STRUCTURES
POINTERS
+
TOPICS
ARRAYS
STRUCTURES
POINTERS
+
ARRAYS
Arrays
Is a collection of objects with the same type.
Is conceptually defined as a collection of <index, value>
pair.
Usually implemented using indices starting at 0.
+
ARRAYS
Arrays
A data structure for storing more than one data item that
has a similar data type.
Is the most convenient method to store a collection of
objects.
+
ARRAYS
Arrays
The items of an array are allocated at adjacent memory
locations – elements of that array.
The total number of elements in an array is called
length(size).
Elements can be accessed thru index or subscript.
+
ARRAYS
1 ARRAY
2 ELEMENTS
A B C D
0 1 2 3
Array Length = 4
3 INDEX First Index =0
Last Index =3
+
ARRAYS
+
ARRAYS
Java C++
int[] anArray; int anArray[5];
anArray = new int[5];
+
ARRAYS
Java
int[] anArray = {1, 2, 3, 4, 5};
C++
int anArray[] = {1, 2, 3, 4, 5};
+
ARRAYS
Operation on an Array
Traversing – visiting elements per index.
Searching – determine whether an element is present.
Update – update an element at the given index.
Insertion – adds an element at the given index.
Deletion – deletes an element at the given index.
+
ARRAYS
Traversing
A B C D
0 1 2 3
+
ARRAYS
Traversing
A B C D
0 1 2 3
+
ARRAYS
Traversing
A B C D
0 1 2 3
+
ARRAYS
Traversing
A B C D
0 1 2 3
+
ARRAYS
Traversing
A B C D
0 1 2 3
+
ARRAYS
Searching
A B C D
0 1 2 3
Find n-value from array list. Suppose n-value is ”C.”
+
ARRAYS
Searching
A B C D
0 1 2 3
+
ARRAYS
Searching
A B C D
0 1 2 3
+
ARRAYS
Searching
A B C D
0 1 2 3
+
ARRAYS
Searching
A B C D
0 1 2 3
N-value is ”C” is at array index 2.
+
ARRAYS
Update
A B C D
0 1 2 3
Update value at index 2 with value “D”.
+
ARRAYS
Update
A B C D
0 1 2 3
Index 2 value equal to “C”
+
ARRAYS
Update
A B D D
0 1 2 3
Replace index 2 value with “D”
+
ARRAYS
Insert
A B C D
0 1 2 3
Insert value “E” at index 2.
+
ARRAYS
Insert
A B C D
0 1 2 3
+
ARRAYS
Insert
A B E D
0 1 2 3
“C” value is then dropped to a temporary storage.
+
ARRAYS
Insert
A B E C
0 1 2 3
“D” value is then dropped to a temporary storage.
+
ARRAYS
Insert
A B E C D
0 1 2 3 4
“D” value is then added at the end of the array.
+
ARRAYS
Insert
A B C D
0 1 2 3
+
ARRAYS
Insert
A B C D
0 1 2 3 4
+
ARRAYS
Insert
A B C D D
0 1 2 3 4
+
ARRAYS
Insert
A B C D D
0 1 2 3 4
+
ARRAYS
Insert
A B C C D
0 1 2 3 4
+
ARRAYS
Insert
A B C C D
0 1 2 3 4
+
ARRAYS
Insert
A B E C D
0 1 2 3 4
+
ARRAYS
Delete
A B E C D
0 1 2 3 4
Delete index 2.
+
ARRAYS
Delete
A B E C D
0 1 2 3 4
+
ARRAYS
Delete
A B C D
0 1 2 3 4
+
ARRAYS
Insert
A B C C D
0 1 2 3 4
+
ARRAYS
Insert
A B C D D
0 1 2 3 4
+
ARRAYS
Delete
A B C D
0 1 2 3
+
ARRAYS
Two-Dimension Arrays
int myArray[row][col]
int myArray[2][4]
+
ARRAYS
Two-Dimension Arrays
0 1 2 3
0
A B C D
1
A B C D
+
TOPICS
ARRAYS
STRUCTURES
POINTERS
+
STRUCTURES
Structures/ Struct
Is a collection of variables under a single name.
A composite data type declaration that defines a grouped
list of variables under one name.
Aggregating associated data into a single variable.
+
STRUCTURES
BOX STUDENT PROFILE
Width Student Name
Length Student Course
Height Student Year
Student’s Enrolled
Subjects
+
STRUCTURES
struct nameOfStruct
{
type member;
type member;
};
+
STRUCTURES
struct Box
{
int width;
int length;
int height;
};
+
STRUCTURES
To declare a variable:
struct nameofStruct variable_name;
+
STRUCTURES
int main()
{
struct Box b;
};
+
STRUCTURES
int main()
We use a period “.” to get to the
{
elements of a struct.
struct Box b;
b.width = 10;
b.length = 30;
b.height = 10;
};
+
TOPICS
ARRAYS
STRUCTURES
POINTERS
+
POINTERS
Pointer
is a variable that stores the address of another
variable.
is a variable that stores memory addresses
+
POINTERS
Pointer
num
10
0x7fff569adc58
+
POINTERS
Pointer
num Variable name
10 Value of num
0x7fff569adc58 Address of num
+
POINTERS
#include <stdio.h>
int main()
{
//Variable declaration
int num = 10;
//Pointer declaration
int *p;
//Assigning address of num to the pointer p
p=#
printf("Address of variable num is: %p", p);
return 0;
}
Output: Address of variable num is: 0x7fff5694dc58
+
POINTERS
Operators that are used with Pointers:
“Address of” (&) Operator
“Value at Address” (*) Operator
+
POINTERS
#include <stdio.h>
int main()
{
/* Pointer of integer type, this can hold the * address of a integer type variable. */ int *p;
int var = 10;
/* Assigning the address of variable var to the pointer * p. The p can hold the address of var because var is * an integer
type variable. */
p= &var;
printf("Value of variable var is: %d", var);
printf("\nValue of variable var is: %d", *p);
printf("\nAddress of variable var is: %p", &var);
printf("\nAddress of variable var is: %p", p);
printf("\nAddress of pointer p is: %p", &p);
return 0;
}
+
POINTERS
Output:
Value of variable var is: 10
Value of variable var is: 10
Address of variable var is: 0x7fff5ed98c4c
Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50
+
POINTERS
int var = 10;
int *p;
p = &var;
p var
0x7fff5ed98c4c 10
0x7fff5ed98c50 0x7fff5ed98c4c
END