E N D
What is Data Structure? • Data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. • Data Organization
Primitive and Non-Primitive Data Structure The primitive data structure can be directly controlled by computer commands. That means it is defined by the system and compiler. The Non-primitive data structure cannot be directly controlled by computer commands. The Non-primitive data structure is derived from the primitive data structure.
Linear and Non-Linear Data Structure • Linear Data Structure – arranged in the Sequential Order • Non-Linear Data Structure – arranged in the Hierarchical Order
Data Structure – Array • An array is a collection of homogeneous (same type) data items stored in contiguous memory locations. For example • if an array is of type “int”, it can only store integer elements and cannot allow the elements of other types such as double, float, char etc.
Array Memory representation • The following diagram represents an integer array that has 12 elements. • The index of the array starts with 0, so the array having 12 elements has indexes from 0 to 11.
Why we need an array? • Array is particularly useful when we are dealing with lot of variables of the same type. For example, lets say I need to store the marks in math subject of 100 students. • To solve this particular problem, either I have to create the 100 variables of int type or create an array of int type with the size 100. • Obviously the second option is best, because keeping track of all the 100 different variables is a tedious task. • On the other hand, dealing with array is simple and easy, all 100 values can be stored in the same array at different indexes (0 to 99).
Accessing Array elements • In this example we have an array arr of type “int”. The size of the array is 10 which means it can hold 10 integer values. arr[0] would be first element, arr[1] second and so on. • Here we are assigning values to only few elements of the array. After this program, I have shared the output of this program, which shows that the default value of the elements of an int array is 0. The elements that are not assigned any value shows their value as 0 (default value).
EXAMPLE PROGRAM public class Java Example { public static void main(String args []) { intarr[]; //array declaration //allocating memory to array arr = new int[10]; //Assigning elements arr[1] = 100; arr[5] = 98; arr[3] = 11; //Accessing array elements for(inti=0; i<10 ; i++) { System.out.println(arr[i]); } } }
Characteristics of an Array • The Declaration int a[5] is nothing but the creation of five variable of integer types in memory. Instead of declaring five variables for five values, the programmer can define them in an array. • All the elements of an array share the same name, and they are distinguished from one another with the help of the number of the element. • The elements number in an array plays a major role in calling each element.
Characteristics of an Array • Any particular elements of an array can be modified separately without disturbing the other elements. • Int a [5] = {1,2,3,4,8}; If a programmer needs to replace 8 with 10, then it need not require changing all other numbers expects 8. To carry out this task, the statement a [4]=10 can be used. Here the other four elements are not disturbed. • Any element of an array a [] can be assigned/equated to another ordinary variable or array variable of its type.
Characteristics of an Array • Once the array is declared, its lowest boundary cannot be changed but the upper boundary can be expanded. • We know that an array name itself is a pointer. • All the elements of an array share the same name, and they are distinguished from one another with the help of the number of the element.
Characteristics of an Array • The amount of memory required for an array depends upon the data type and the number of elements. The total size in bytes for a single-dimensional array is computed. • Total bytes = size of (data type) x size of array • when an array is declared and not initialized, it contains garbage values. If we declared an array as static, all elements are initialized to zero.
One dimensional array in C • Arrays are a fundamental concept in programming, and they come in different dimensions. • One-dimensional arrays, also known as single arrays, are arrays with only one dimension or a single row.
Syntax of One-Dimensional Array in C • The syntax of a one-dimensional array in C programming language is as follows: • dataType arrayName[arraySize];
EXPLANATION dataType • specifies the data type of the array. It can be any valid data type in C programming language, such as int, float, char, double, etc. • arrayName • is the name of the array, which is used to refer to the array in the program. • arraySize • specifies the number of elements in the array. It must be a positive integer value.
Example of One-Dimensional Array in C • #include <stdio.h> • int main() { • int numbers[5] = {10, 20, 30, 40, 50}; • for(int i=0; i<5; i++) { • printf("numbers[%d] = %d\n", i, numbers[i]); • } • return 0; • }
Output • numbers[0] = 10 • numbers[1] = 20 • numbers[2] = 30 • numbers[3] = 40 • numbers[4] = 50
Definition • A linked list is a linear data structure where each element (called a node) is made up of two items - the data and a reference (or pointer) which points to the next node. • A linked list is a collection of nodes where each node is connected to the next node through a pointer.
Types of Linked List 1. Single Linked List − Item navigation is forward only. 2.Doubly Linked List − Items can be navigated forward and backward. 3. Circular Linked List − Last item contains link of the first element.
Single Linked List • Item navigation is forward only
Doubly Linked List • Items can be navigated forward and backward
Circular Linked ListLast item contains link of the first element