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

DSA 1 (1)

A data structure is an organized way to store and manage data for efficient operations, with examples including arrays, lists, and trees. Arrays are linear data structures that store elements in contiguous memory locations and support operations like insertion, deletion, traversal, searching, and updating. The document provides examples of inserting and deleting elements in arrays, demonstrating how to manipulate array contents through code snippets.

Uploaded by

ranaibrahim453
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)
5 views

DSA 1 (1)

A data structure is an organized way to store and manage data for efficient operations, with examples including arrays, lists, and trees. Arrays are linear data structures that store elements in contiguous memory locations and support operations like insertion, deletion, traversal, searching, and updating. The document provides examples of inserting and deleting elements in arrays, demonstrating how to manipulate array contents through code snippets.

Uploaded by

ranaibrahim453
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/ 14

What is a Data Structure?

•Data: Raw facts or numbers with no meaning


on their own.: Test scores: 85, 90, 78, 88
•Information: Organized data that makes
sense. Average test score: 85.3
•Definition: A data structure is a way of
organizing and storing data to perform
operations efficiently.
Examples: Arrays, Lists, Trees, Graphs, Stacks,
Queues
Algorithms:
An algorithm is a step-by-step process
to solve a problem.
Example:
Step 1: Input: A number n
Step 2: If n is perfectly divisible by 2, -
Then print "The number is even"
Step 3: Else - Print "The number is
odd" Step 4: End
Linear Data Structures:
Definition: A data structure where elements are stored
sequentially.
What is an Array:
A collection of elements stored in
contiguous memory locations.
Example: arr = [10, 20, 30, 40, 50]
Properties:
 Fixed size (in static arrays).
 Index-based access.
 Homogeneous data type (all elements of the same type).
Array Operations:

1.Insertion – Adding elements.


2.Deletion – Removing elements.
3.Traversal – Accessing elements.
4.Searching – Finding an element.
5.Updating – Modifying an element.
Insertion in an Array:
When inserting an element into an array, we
need to consider three possible cases:
• At the beginning → Shift all elements right.
• At a specific position → Shift elements from
that index onward.
• At the end → Simply insert if space is
available.
Inserting at the Beginning:
Initial Array: [10, 20, 30, 40]
Insert 5 at the beginning → Shift all elements
right
Final Array: [5, 10, 20, 30, 40]
Explanation of Inserting 5 at the Beginning

Initial Array:
[10, 20, 30, 40,50]

1.Make Space for the New Element


•Shift all elements one position to the right to create
space at the beginning.
2.Shift Elements Right:
I. Move 40 to the next position → [10, 20, 30, 40, _ ]
II. Move 30 to the next position → [10, 20, 30, 30, 40]
III. Move 20 to the next position → [10, 20, 20, 30, 40]
IV. Move 10 to the next position → [10, 10, 20, 30, 40]
3.Insert 5 in the First Position:
i. Place 5 in the first spot → [5, 10, 20, 30, 40,50]
Final Array:
[5, 10, 20, 30, 40,50]
Example :Insert at the Beginning of an Array
#include <iostream>
using namespace std;
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int size = 5;
int element = 25;
for (int i = size; i > 0; i--) {
arr[i] = arr[i - 1]; }
arr[0] = element;
size++;
After Insertion: 25 10 20 30 40 50
cout << "After Insertion: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " "; }
return 0; }
Example :Insert at the Specific Position of an Array
#include <iostream>
using namespace std;
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int size = 5;
int element = 25;
int position = 2;
for (int i = size; i > position; i--) {
arr[i] = arr[i - 1]; }
Array after insertion: 10 20 25 30 40 50
arr[position] = element;
size++;
cout << "Array after insertion: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
return 0;
Example :Insert at the End of an Array

#include <iostream>
using namespace std;
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int size = 5;
int element = 60;
arr[size] = element;
size++;
cout << "Array after insertion: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
} Array after insertion: 10 20 30 40 50
return 0; 60
}
Deleting an Element from the Beginning
#include <iostream>
using namespace std;

int main() {
int arr[10] = {10, 20, 30, 40, 50};
int size = 5;
// Shift elements to the left
for (int i = 0; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
cout << "Array after deleting first element: ";
for (int i = 0; i < size; i++) { Array after deleting first element: 20
cout << arr[i] << " "; 30 40 50
}
return 0;}
Deleting an Element from a Specific Position
#include <iostream>
using namespace std;

int main() {
int arr[10] = {10, 20, 30, 40, 50};
int size = 5;
int position = 2; // Deleting element at index 2 (30)
for (int i = position; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
cout << "Array after deleting element at index " << position << ": ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " "; Array after deleting element at index 2:
10 20 40 50
}
return 0; }
Deleting an Element from the End

#include <iostream>
using namespace std;
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int size = 5;
size--;
cout << "Array after deleting last element: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
return 0; Array after deleting last element: 10 20 30 40
}

You might also like