0% found this document useful (0 votes)
20 views

Data Structure 2

Uploaded by

aliyevakhanim386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Data Structure 2

Uploaded by

aliyevakhanim386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Data Structure

Why to Learn Data Structures & Algorithms (DSA)?

• Data Search − Consider an inventory of 1 million(106) items of a


store. If the application is to search an item, it has to search an
item in 1 million(106) items every time slowing down the search.
As data grows, search will become slower.

• Processor speed − Processor speed although being very high,


falls limited if the data grows to billion records.

• Multiple requests − As thousands of users can search data


simultaneously on a web server, even the fast server fails while
searching the data.
public class ArrayDemo {
public static void main(String []args) {
int LA[] = new int[3];
for(int i = 0; i < 3; i++) {
LA[i] = i+2;
System.out.println("LA[" + i + "] = " + LA[i]);}}}
Output
LA [0] = 2
LA [1] = 3
LA [2] = 4
Online Editor
• https://www.tutorialspoint.com/
data_structures_algorithms/index.htm
DSA Jobs and Opportunities(imkan)
• Google
• Amazon
• Microsoft
• Apple
• Adobe
• JPMorgan Chase
• Goldman Sachs
• Walmart
• Johnson & Johnson
• Airbnb
• Tesla
Built-in Data Type
• Integers
• Boolean (true, false)
• Floating (Decimal numbers)
• Character and Strings
Data Type
• Built-in Data Type
• Derived Data Type
Derived Data Type
• List
• Array
• Stack
• Queue
Linear Data Structures && Non-Linear Data
Structures

•Graphs
•Arrays •Trees
•Linked Lists •Tries
•Stacks •Maps
•Queues
What is an Array?

• An array is a type of linear data structure that is defined


as a collection of elements with same or different data
types. They exist in both single dimension and
multiple dimensions. These data structures come into
picture when there is a necessity to store multiple
elements of similar nature together at one place.
D
e
D
f
a
a
t
u
a
l
t
T
V
y
a
Data Type Default Value
p
e
l
u
e

f
bool false
b
o
o
a
l
s
l
e

char 0
c
h
0
a
r

int 0
i
n 0
t

float 0.0
f
l 0
o .
a 0
t

double 0.0f
d
o 0
u .
b 0
l f
void e

v
o
i
wchar_t 0
d

w
c
h
a 0
r
_
t
Basic Operations in Arrays
• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or
by the value.
• Update − Updates an element at the given index.
• Display − Displays the contents of the array.
Array - Insertion Operation

• public class ArrayDemo {


• public static void main(String []args) {
• int LA[] = new int[3];
• System.out.println("Array Before Insertion:");
• for(int i = 0; i < 3; i++)
• System.out.println("LA[" + i + "] = " + LA[i]); //prints empty array
• System.out.println("Inserting Elements..");

• // Printing Array after Insertion
• System.out.println("Array After Insertion:");
• for(int i = 0; i < 3; i++) {
• LA[i] = i+3;
• System.out.println("LA[" + i + "] = " + LA[i]);}}}
Output
• Array Before Insertion:
• LA[0] = 0
• LA[1] = 0
• LA[2] = 0
• Inserting elements..
• Array After Insertion:
• LA[0] = 3
• LA[1] = 6
• LA[2] =9
Array - Deletion Operation
• #include <iostream>
• using namespace std;
• int main(){
• int LA[] = {1,3,5};
• int i, n = 3;
• cout << "The original array elements are :"<<endl;
• for(i = 0; i<n; i++) {
• cout << "LA[" << i << "] = " << LA[i] << endl; }
• for(i = 1; i<n; i++) {
• LA[i] = LA[i+1];
• n = n - 1; }
• cout << "The array elements after deletion :"<<endl;
• for(i = 0; i<n; i++) {
• cout << "LA[" << i << "] = " << LA[i] <<endl;}}
Output

• The original array elements are :


• LA[0] = 1
• LA[1] = 3
• LA[2] = 5
• The array elements after deletion :
• LA[0] = 1
• LA[1] = 5
Array - Search Operation
• #include <iostream>
• using namespace std;
• int main(){
• int LA[] = {1,3,5,7,8};
• int item = 5, n = 5;
• int i = 0;
• cout << "The original array elements are : " <<endl;
• for(i = 0; i<n; i++) {
• cout << "LA[" << i << "] = " << LA[i] << endl; }
• for(i = 0; i<n; i++) {
• if( LA[i] == item ) {
• cout << "Found element " << item << " at position " << i+1 <<endl;}}
• return 0;}
Output
• The original array elements are :
• LA[0] = 1
• LA[1] = 3
• LA[2] = 5
• LA[3] = 7
• LA[4] = 8
• Found element 5 at position 3
Array - Traversal Operation
• #include <iostream>
• using namespace std;
• int main(){
• int LA[] = {1,3,5,7,8};
• int item = 10, k = 3, n = 5;
• int i = 0, j = n;
• cout << "The original array elements are:\n";
• for(i = 0; i<n; i++)
• cout << "LA[" << i << "] = " << LA[i] << endl;
• return 0;}
Output

• The original array elements are :


• LA[0] = 1
• LA[1] = 3
• LA[2] = 5
• LA[3] = 7
• LA[4] = 8
Array - Update Operation
• #include <iostream>
• using namespace std;
• int main(){
• int LA[] = {1,3,5,7,8};
• int item = 10, k = 3, n = 5;
• int i = 0, j = n;
• cout << "The original array elements are :\n";
• for(i = 0; i<n; i++)
• cout << "LA[" << i << "] = " << LA[i] << endl;
• LA[2] = item;
• cout << "The array elements after updation are :\n";
• for(i = 0; i<n; i++)
• cout << "LA[" << i << "] = " << LA[i] << endl;
• return 0;}
Output

• The original array elements are :


• LA[0] = 1
• LA[1] = 3
• LA[2] = 5
• LA[3] = 7
• LA[4] = 8
• The array elements after updation :
• LA[0] = 1
• LA[1] = 3
• LA[2] = 10
• LA[3] = 7
• LA[4] = 8
Array - Display Operation
• #include <iostream>
• using namespace std;
• int main(){
• int LA[] = {1,3,5,7,8};
• int n = 5;
• int i;
• cout << "The original array elements are :\n";
• for(i = 0; i<n; i++)
• cout << "LA[" << i << "] = " << LA[i] << endl;
• return 0;}
Output

• The original array elements are :


• LA[0] = 1
• LA[1] = 3
• LA[2] = 5
• LA[3] = 7
• LA[4] = 8
• https://www.tutorialspoint.com/
data_structures_algorithms/array_data_structure.htm

You might also like