Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Data Structures & Algorithm Lab Manual

Data Structures and Algorithms

Uploaded by

Shoaib Rajper
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data Structures & Algorithm Lab Manual

Data Structures and Algorithms

Uploaded by

Shoaib Rajper
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Data Structures and Algorithms Practical Lab Manual

University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

UNIVERSITY OF SINDH
KBSAS CAMPUS NAUSHAHRO FEOZE
DEPARTMENT OF INFORMATION TECHNOLOGY

PRACTICAL NOTEBOOK

Data Structures and Algorithms

Name of Student: Shoaib Ali

Roll Number: 2k23/NFIT/19

Name of Instructor: Sir Muhammad


Khalid

Roll Number: 2K23-NFIT-19 P a g e 1 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

CERTIF
ICATE

This is to certify that Mr. SHOAIB ALI of 2k23 BS (IT) class bearing Roll No. 19 has

carried out the necessary Project work as per course of studies Data Structures and

Algorithms for the 2nd Year (3rd Semester) as above in practical schedule of his

journal.

Date: _____________ Course Teacher: Sir Muhammad Khalid

Roll Number: 2K23-NFIT-19 P a g e 2 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Roll Number: 2K23-NFIT-19 P a g e 3 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Table of Contents

Lab No. 1 ……………………………………………………………………………………………………………………………….


Objective: To become familiar with the downloading and installation of JDK and Net
Beans IDE 04

Lab No.2 ……………………………………………………………………………………………………………………………….


Objective: Create 0

Lab No.3 ……………………………………………………………………………………………………………………………….


Objective: Make 0

Lab No.4 ……………………………………………………………………………………………………………………………….


Objective: Create 1

Lab No.5……………………………………………………………………………………………………………………………….
Objective: Build 1

Roll Number: 2K23-NFIT-19 P a g e 4 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Lab No. 1

Objective: To become familiar with the downloading and installation of JDK and Net beans IDE.

Step 1: Open Google and type Java jdk in the search bar. Click the very first link then the new tab will
open.

Note: Internet connection is necessary to download and install java jdk.

Figure 1: Google Search Result

Step 2: Click on the highlighted option and setup will start downloading.

Figure 2: Website is opened

Roll Number: 2K23-NFIT-19 P a g e 5 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Step 3: Backup file is downloading.

Figure 4: Downloading Process

Figure 3: Downloading Process

Step 4: Here you can see that the backup of Java JDK has been downloaded, now you just have to
click on open and move to the next step.

Figure 4: Opening backup file

Roll Number: 2K23-NFIT-19 P a g e 6 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Procedure of Installation of JDK

Step 1: After opening backup, Click On Next. Step 3: The installation process begins.

Step 2: Set the destination where you want to Step 4: JDK has been installed. Now click on Close.
install the program. Click Next to continue the
process.

Roll Number: 2K23-NFIT-19 P a g e 7 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Roll Number: 2K23-NFIT-19 P a g e 8 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Procedure to make changes for running software

Step 1: Open the destination where JDK was Step 4: Go to Path Click on Edit. A dialogue
installed. box appears.

Step 2: Open bin folder. Then copy its


Step 5: Click on New to add a path.
destination location.

Step 3: Now Open System Environment


Step 6: Enter the location copied. Set a
Variables. Click on Environment Variables
name also. Click on Ok.
Option.

Roll Number: 2K23-NFIT-19 P a g e 9 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Step 7: Click on Ok.

Step 8: Check it using Command


Prompt. Check version and java
compiler.

Roll Number: 2K23-NFIT-19 P a g e 10 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Procedure of Installation of Apache Net Beans

Step 1: Open Google and type Netbeans IDE in the search bar. Click the very first link then the new
tab will open.

Figure 1: Google Search Result

Step 2: Click on the highlighted option and setup will start downloading.

Figure 2: Website is opened

Roll Number: 2K23-NFIT-19 P a g e 11 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Step 3: Click on the highlighted area.

Step 4: Click on the highlighted link the backup will start downloading.

Figure 4: Downloading Process

Roll Number: 2K23-NFIT-19 P a g e 12 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Procedure of Installation of JDK

Step 3: Set the destination where you want


Step 1: After opening backup, Click On Next. to install the program. Click on Next.

Step 2: Agree to the terms and conditions of the Step 4: Click on Next to continue the process.
software.

Roll Number: 2K23-NFIT-19 P a g e 13 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Step 5: The installation process begins.

Step 6: NetBeans has been installed. Now click


on Finish.

Roll Number: 2K23-NFIT-19 P a g e 14 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Lab No.2

Objective: Array

Definition:

Normally, an array is a collection of similar type of elements which has contiguous memory location.

Java array is an object which contains elements of a similar data type. Additionally, the elements of
an array are stored in a contiguous memory location. It is a data structure where we store similar
elements. We can store only a fixed set of elements in a Java array.

Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is
stored on 1st index and so on.

Figure 1: Array

How an Array works?

An array in Java works by allocating a contiguous block of memory to hold a fixed number of
elements of the same data type. Here is a brief overview of how it works:

i. Array Declaration: You declare an array by specifying its type and name, along with the size
in square brackets.
Syntax:

Here, data is an array that can hold values of type double.

Here, the array can store 10 elements. We can also say that the size or length of the array is
10.
In Java, we can declare and allocate the memory of an array in one single statement. For
example,

Roll Number: 2K23-NFIT-19 P a g e 15 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

ii. Array Initialization: In Java, we can initialize arrays during declaration. For example,
Syntax: at the time of declaration,
DataType[ ] arrayName = {value1, value2, value3, …};

Figure 1: Syntax

Here, we have created an array named age and initialized it with the values inside the curly
brackets.
In the Java array, each memory location is associated with a number. The number is known
as an array index. We can also initialize arrays in Java, using the index number.
Syntax (after declaration):
arrayName [index] = value;

For example,

Figure 2: Initilization using index number

Figure 3: Array Initialization

Roll Number: 2K23-NFIT-19 P a g e 16 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
iii. Accessing Elements of an Array: We can access the element of an array using the index
number. Here is the syntax for accessing elements of an array,
Example: Access Array Elements

Figure 1: Code for Accessing Elements

Output:

Figure 2: Output Result

In the above example, notice that we are using the index number to access each element of
the array.

We can use loops to access all the elements of the array at once.

iv. Array Length: You can find the length of an array using the length property:
Syntax:

int length =
numbers.length;

Roll Number: 2K23-NFIT-19 P a g e 17 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

v. Iterating Through Array: In Java, we can also loop through each element of the array. For
example,
Example: Using For-Loop

Figure
Output:

Using length function:

Figure 1: Syntax for Using Loop

Roll Number: 2K23-NFIT-19 P a g e 18 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Output:

Figure 2: Output Result

In the above example, we are using the for Loop in Java to iterate through each element of
the array. Notice the expression inside the loop,

age.length

Here, we are using the length property of the array to get the size of the array.

We can also use the for-each loop to iterate through the elements of an array. For example,

Roll Number: 2K23-NFIT-19 P a g e 19 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Lab No.3

Objective: Coding in Array, perform tasks using for loop

Definition:

Example: Using the for-each Loop

Task 1:

Output:

Task 2:

Output:

Roll Number: 2K23-NFIT-19 P a g e 20 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Task 3:

Output:

Task 4:

Output:

Roll Number: 2K23-NFIT-19 P a g e 21 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Lab No.4

Objective: What is a 2D Array? How it Works? Exemplify through coding.

Definition:

A two-dimensional array, also known as 2D array, is a collection of data elements arranged in a grid-
like structure with rows and columns. Each element in the array is referred to as a cell and can be
accessed by its row and column indices/indexes. A multidimensional array is an array of arrays. Each
element of a multidimensional array is an array itself.

For Example,
int [ ] [ ] a = new int [3] [4];

Here we have a created a multidimensional array named a. It is a 2D array that can hold a maximum
of 12 elements,

Roll Number: 2K23-NFIT-19 P a g e 22 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
 Declaration:

Syntax:

Data_type array_name [ ] [ ]; OR Data_type [ ] [ ] array_name;

 Initialization:

A 2D array can be initialized during declaration.

Syntax:

dataType [ ] [ ] arrayName = { {value1, value2, value3, …},


{value1, value2, value3, …} …….};

Example:

Roll Number: 2K23-NFIT-19 P a g e 23 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

In Java, we can initialize 2d arrays as following.

Roll Number: 2K23-NFIT-19 P a g e 24 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

 Assign Value to an Empty 2D Array:


To assign values to an empty 2D array, we can use nested loops to iterate
over the rows and columns of the array, and then assign values to each
element individually.

Syntax:
// Declare an empty 2D array
int[][] array2D = new int[rows][cols]; // rows and cols represent the number
of rows and columns in the array
// Assign values to each element using nested loops
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array2D[i][j] =value ;
}
}
Example in Java:

Figure 1:

Output:

Figure 2: Output

Roll Number: 2K23-NFIT-19 P a g e 25 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
 Access elements of a 2D Array:

In Java, we can access elements of a 2D array using row and column indices.

Example in Java:

(By String Data Type)

Figure 1

Output:

Figure 2

Roll Number: 2K23-NFIT-19 P a g e 26 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Example in Java:

(By using Int Data Type)

Figure 1:

Output:

Figure 2:

Example in Java:

(By using Boolean Data Type)

Figure 1

Roll Number: 2K23-NFIT-19 P a g e 27 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Output:

Figure 2

 Create a simple java program that represents a 2D Array

Here is a simple java program that represents a 2D Array and accesses all its elements using
nested loops:

Example:

Figure 1:

Output:

Figure 2:

Roll Number: 2K23-NFIT-19 P a g e 28 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Java Program to Initialize a 2D Array

Roll Number: 2K23-NFIT-19 P a g e 29 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Print all elements of 2d array using loop:

Output:

Roll Number: 2K23-NFIT-19 P a g e 30 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Program to add two 2D arrays:

Output:

Roll Number: 2K23-NFIT-19 P a g e 31 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Lab No.5

Objective1: Insertion Operation in an array

Insertion: Adding a new element into an array at a specific position.

Example of Array Insertion in Java

(Int Data type:)

Figure 1:

Output:

Figure 2:

Roll Number: 2K23-NFIT-19 P a g e 32 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Using String Data Type

Example:

Figure 1:

Output:

Figure 2:

Roll Number: 2K23-NFIT-19 P a g e 33 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech
Objective 2: Deletion Operation in Java

Array Deletion Operation:

- An array deletion operation involves removing an element from an array at a specified


position.
- This operation typically requires shifting elements to fill the gap left by the deleted
element and updating the size of the array if necessary.

Example:

Int Data Type

Figure 1:

Output:

Figure 2:

Roll Number: 2K23-NFIT-19 P a g e 34 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Roll Number: 2K23-NFIT-19 P a g e 35 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Roll Number: 2K23-NFIT-19 P a g e 36 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Roll Number: 2K23-NFIT-19 P a g e 37 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Roll Number: 2K23-NFIT-19 P a g e 38 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Roll Number: 2K23-NFIT-19 P a g e 39 | 40


Data Structures and Algorithms Practical Lab Manual
University of Sindh KBSAS Campus Naushahro Feroze Department of Information Tech

Roll Number: 2K23-NFIT-19 P a g e 40 | 40

You might also like