You are on page 1of 6

Name:OYINLOLA ISRAEL OYINKANSOLA

MATRIC NO:22110591196
COURSE CODE:221

QUESTION: Explain the six basic operations of data structures using the
example of codes written in different programming languages

SOLUTION:

Traversal:

• Traversing a data structure means visiting each element stored within it


systematically. This operation is applicable to various data structures.

PYTHON:
#Traversing a list

my_list = [1, 2, 3, 4, 5]

for element in my_list:

print(element)

JAVA:

// Traversing an array

int[] myArray = {1, 2, 3, 4, 5};

for (int element : myArray) {

System.out.println(element);

C++

// Traversal in an array (C++)

#include <iostream>

using namespace std;

int main() {

int arr[] = {1, 2, 3, 4};

int N = sizeof(arr) / sizeof(arr[0]);


for (int i = 0; i < N; i++) {

cout << arr[i] << ' ';

return 0;

// Output: 1 2 3 4

Searching:

• Searching involves finding a specific element within a data structure (e.g., array,
linked list, tree, graph).

PYTHON

# Searching for an element in a list

my_list = [1, 2, 3, 4, 5]

print(3 in my_list) # Checking if 3 exists in the list

JAVA

// Searching for an element in an array

int[] myArray = {1, 2, 3, 4, 5};

int searchKey = 3;

boolean found = false;

for (int element : myArray) {

if (element == searchKey) {

found = true;

break;

System.out.println(found);

C++

// Searching an element in an array (C++)

#include <iostream>
using namespace std;

void findElement(int arr[], int N, int K) {

for (int i = 0; i < N; i++) {

if (arr[i] == K) {

cout << "Element found!";

return;

cout << "Element Not found!";

int main() {

int arr[] = {1, 2, 3, 4};

int K = 3;

int N = sizeof(arr) / sizeof(arr[0]);

findElement(arr, N, K);

return 0;

// Output: Element found!

Insertion:

• Insertion involves adding an element to a data structure. It’s a common operation


across all data structures.

PYTHON

# Inserting an element into a list

my_list = [1, 2, 3, 4, 5]

my_list.append(6) # Adding 6 to the end of the list

print(my_list)
JAVA

// Inserting an element into an array (assuming a resizable array)

int[] myArray = {1, 2, 3, 4, 5};

int[] newArray = Arrays.copyOf(myArray, myArray.length + 1);

newArray[newArray.length - 1] = 6;

System.out.println(Arrays.toString(newArray));

C++

// Insertion in an array (C++)

#include <iostream>

using namespace std;

int main() {

int arr[] = {1, 2, 3, 4};

int N = sizeof(arr) / sizeof(arr[0]);

int newElement = 5;

arr[N] = newElement; // Adding 5 to the end

N++; // Update the size

for (int i = 0; i < N; i++) {

cout << arr[i] << ' ';

return 0;

// Output: 1 2 3 4 5

Deletion:

• Deletion involves removing an element from a data structure. It can be performed


in various contexts, such as deleting a node from a linked list or removing an
item from an array.

PYTHON

# Deleting an element from a list


my_list = [1, 2, 3, 4, 5]

my_list.remove(3) # Removing the element with value 3

print(my_list)

JAVA

// Deleting an element from an array

int[] myArray = {1, 2, 3, 4, 5};

int[] newArray = new int[myArray.length - 1];

int deleteIndex = 2; // Index of the element to delete

for (int i = 0, j = 0; i < myArray.length; i++) {

if (i != deleteIndex) {

newArray[j++] = myArray[i];

System.out.println(Arrays.toString(newArray));

Updating/Modifying:

• This operation involves changing the value of an existing element within a data
structure. It’s essential for maintaining data integrity.

JAVA

// Updating an array element (Java)

public class Main {

public static void main(String[] args) {

int[] arr = {10, 20, 30, 40};

int indexToUpdate = 2;

int newValue = 35;

arr[indexToUpdate] = newValue;

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}
}

// Output: 10 20 35 40

Sorting:

• Sorting arranges the elements of a data structure in a specific order (e.g.,


ascending or descending). It’s crucial for efficient searching and retrieval.

PYTHON

# Bubble sort in an array (Python)

def bubble_sort(arr):

n = len(arr)

for i in range(n):

for j in range(n - i - 1):

if arr[j] > arr[j + 1]:

arr[j], arr[j + 1] = arr[j + 1], arr[j]

arr = [64, 34, 25, 12, 22, 11, 90]

bubble_sort(arr)

print("Sorted array:", arr)

# Output: Sorted array: [11, 12, 22, 25, 34, 64, 90]

JAVA

// Sorting an array

int[] myArray = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};

Arrays.sort(myArray);

System.out.println(Arrays.toString(myArray));

You might also like