Data structure and algorithm complete lab file, Lab Reports of Data Structures and Algorithms

It includes these topics coding: An overview of Data Structures and their Operations: Brief Revision of Array Implementation of Array operations One Dimensional Array Multidimensional Array Abstract data types. Pointers: Implementation of Null pointer, Void Pointer, Invalid Pointer, Dangling Pointer Reference Variable Sorting and Searching Techniques: Implementation for elementary sorting algorithms such as Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort Implementation of searching algorithms such as Linear Search and Binary Search Linked Lists: Implementation of linked list with the help of algorithms for Insertion, Deletion and Search an element Implementation of Doubly Link List Implementation of Circular Link List. Stacks: An array and link list based implementation of STACK Queues: An array and link list based implementation of QUEUES Trees: Implementation of Binary Trees, Array/Link List representation of binary trees in memory,BST and graphs.

Typology: Lab Reports

2023/2024

Available from 03/08/2024

shiza-alam-khan
shiza-alam-khan 🇵🇰

5 documents

1 / 76

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name : Syed Affan Hussain
Roll No. : 2020-CS-180
Sec : D
Semester : 3rd
Submitted to Miss Javeria
COMPUTER SCIENCE DEPARTMENT
SIR SYED UNIVERSITY OF ENGINEERING
AND TECHNOLOGY, KARACHI
LAB MANUAL
OF
DATA STRUCTURES
AND ALGORITHMS (DSA)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c

Partial preview of the text

Download Data structure and algorithm complete lab file and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Name : Syed Affan Hussain

Roll No. : 2020 - CS- 180

Sec : D

Semester : 3

rd

Submitted to Miss Javeria

COMPUTER SCIENCE DEPARTMENT SIR SYED UNIVERSITY OF ENGINEERING AND TECHNOLOGY, KARACHI

LAB MANUAL

OF

DATA STRUCTURES

AND ALGORITHMS (DSA)

Data Structures and Algorithms (CS-212) Lab Manual Data Structures and Algorithms (CS-212) Spring Semester 2021 I. Lab Objectives In the laboratory, Visual Studio 2017 will be used to implement the concepts of Data Structures (including arrays, stacks, queues and linked lists), advanced data structures (including trees and graphs). Simple searching and sorting algorithms (linear and binary search, selection and insertion sort). II. Lab Outcomes Upon the completion of Data Structures practical, Student will be able to describe the usage of various data structures and operations for maintaining data structures. Design and implement various data structures and algorithms. III. TEXT BOOK:

  • Drozdek, Adam. Data Structures and Algorithms in C++, 4th edition, USA, Published by Cengage Learning 2013. IV. REFERENCE BOOKS:
  • Weiss, Mark. Data Structures and Algorithm Analysis in C++, 4th^ edition, USA, Published by Pearson 2014.

Lab # 1 Getting Started (Revision of C++)

I. Introduction to:

  1. Recall all programming techniques
  2. Use of Array, Structures, Functions & Pointers Basic If-Else Statement There are three types of control statements used in C language
  • If else statements
  • Switch statements
  • Goto statements if ( ) { // Execute these statements if is TRUE } else if ( ) { // Execute these statements if is TRUE and // is FALSE } Example: include using namespace std; int main() // Most important part of the program! { int age; // Need a variable... cout<<"Please input your age: "; // Asks for age cin>> age; // The input is put in age cin.ignore(); // Throw away enter if ( age < 100 ) { // If the age is less than 100 cout<<"You are pretty young!\n"; // Just to show you it works. } else if (age == 100) { // I use else just to show an example cout<<"You are old\n"; // Just to show you it works... } else {

cout<<"You are really old\n"; // Executed if no other statement is } cin.get(); }

II. Array

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World!" application. Example #include using namespace std; void printarray (int arg[], int length) { for (int n=0; n<length; ++n) cout<<arg[n] << ' '; cout<< '\n'; } int main () { Int firstarray[] = {5, 10, 15}; Int secondarray[] = {2, 4, 6, 8, 10}; printarray(firstarray,3); printarray(secondarray,5); }

III. Structures

A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths Definition Of Structure struct<struct-type> { <identifier_list>; <identifier_list>; ….. }; Example #include using namespace std;

IV. Pointer to Structures

A pointer variable can be created not only for native types like (int, float, double etc.) but they can also be created for user defined types like structure. Pointer to Structure Example: #include using namespace std; struct Distance { int feet; float inch; }; int main() { Distance ptr, d; ptr = &d; cout<< "Enter feet: "; cin>> (ptr).feet; cout<< "Enter inch: "; cin>> (ptr).inch; cout<< "Displaying information." <<endl; cout<< "Distance = " << (ptr).feet << " feet " << (ptr).inch << " inches"; return 0; } OUTPUT Enter feet: 4 Enter inch: 3. Displaying information. Distance = 4 feet 3.5 inches In this program, a pointer variable ptr and normal variable d of type structure Distance is defined. The address of variable d is stored to pointer variable, that is, ptr is pointing to variable d. Then the member function of variable d is accessed using pointer. Note: Since pointer ptr is pointing to variable d in this program, (ptr).inch and d.inch is exact same cell. Similarly, (*ptr).feet and d.feet is exact same cell. The syntax to access member function using pointer is ugly and there is alternative notation -

which is more common. ptr->feet is same as (ptr).feet ptr->inch is same as (ptr).inch

Lab Tasks

  1. Write a Program to enter three integers and output the smallest integer using IF.

PROGRAM

#include #include<conio.h> using namespace std; int main() { int a, b, c; cout << "\n \t Enter First Number : "; cin >> a; cout << "\n \t Enter Second Number : "; cin >> b; cout << "\n \t Enter Third Number : "; cin >> c; cout << "\n \t_____________________________ " << endl; if (a < b && a < c) { cout << " \t " << a << " is the smallest element " << endl; } else if (b < a && b < c) { cout << " \t " << b << " is the smallest element " << endl; } else { cout << " \t " << c << " is the smallest element " << endl; } cout << " \t_____________________________ " << endl; system("pause"); return 0; }

OUTPUT

OUTPUT

  1. Write a program to create structure named student. Take information of student from user as input (StdID, StdName, StdAge etc.) Display the output.

PROGRAM

int main() { cout << "\n \t Enter First student Detail : " << endl; student s1; cout << " \t Enter His Id : "; cin >> s1.student_id; cout << " \t Enter His Name : "; cin >> s1.student_name; cout << " \t Enter His Age : "; cin >> s1.age; cout << "\n \t Enter Second student Detail : " << endl; student s2; cout << " \t Enter His Id : "; cin >> s2.student_id; cout << " \t Enter His Name : "; cin >> s2.student_name; cout << " \t Enter His Age : "; cin >> s2.age; cout << "\n _______________________________________ " << endl; cout << "\t First Student Details :) " << endl; cout << "\t Age : " << s1.age << endl; cout << "\t Name : " << s1.student_name << endl; cout << "\t ID : " << s1.student_id << endl;

cout << "\n _______________________________________ " << endl; cout << "\t Second Student Details :) " << endl; cout << "\t Age : " << s2.age << endl; cout << "\t Name : " << s2.student_name << endl; cout << "\t ID : " << s2.student_id << endl; cout << " _______________________________________ " << endl; system("pause"); return 0; }

OUTPUT

  1. Perform above task using pointers to structure

PROGRAM

struct student { int student_id; string student_name; int age; }; int main() { cout << "\n \t Enter student Detail : " << endl; student* ptr, s; ptr = &s; cout << " \t Enter His Id : "; cin >> (ptr).student_id; cout << " \t Enter His Name : "; cin >> (ptr).student_name; cout << " \t Enter His Age : ";

Lab # 2 Measurement of Time Complexity of an algorithm Sometimes, there are more than one way to solve a problem. We need to learn how to compare the performance different algorithms and choose the best one to solve a particular problem. While analyzing an algorithm, we mostly consider time complexity and space complexity. Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. Similarly, Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input. Time and space complexity depends on lots of things like hardware, operating system, processors, etc. However, we don’t consider any of these factors while analyzing the algorithm. We will only consider the execution time of an algorithm. Let’s start with a simple example. Suppose you are given an array A and an integer x and you have to find if x exists in array A. Simple solution to this problem is traverse the whole array A and check if the any element is equal to x. For i : 1 to length of A if A[i] is equal to x return TRUE return FALSE Each of the operation in computer take approximately constant time. Let each operation takes c time. The number of lines of code executed is actually depends on the value of x. During analyses of algorithm, mostly we will consider worst case scenario, i.e., when x is not present in the array A. In the worst case, the if condition will run N times where N is the length of the array A. So, in the worst case, total execution time will be (N c+c). N c for the if condition and c for the return statement (ignoring some operations like assignment of i). As we can see that the total time depends on the length of the array A. If the length of the array will increase the time of execution will also increase.

I. Order of growth is how the time of execution depends on the length of the input. In the

above example, we can clearly see that the time of execution is linearly depends on the length of the array. Order of growth will help us to compute the running time with ease. We will ignore the lower order terms, since the lower order terms are relatively insignificant for large input. We use different notation to describe limiting behavior of a function. a) O-notation: To denote asymptotic upper bound, we use O-notation. For a given function g(n), we denote by O(g(n)) (pronounced “big-oh of g of n”) the set of functions: O(g(n)) = { f(n) : there exist positive constants c and n0 such that 0≤f(n)≤c g(n) for all n≥n0 }

b) Ω-notation: To denote asymptotic lower bound, we use Ω-notation. For a given function g(n), we denote by Ω(g(n)) (pronounced “big-omega of g of n”) the set of functions: Ω(g(n)) = { f(n) : there exist positive constants c and n0 such that 0≤c g(n)≤f(n) for all n≥n } c) Θ-notation: To denote asymptotic tight bound, we use Θ-notation. For a given function g(n), we denote by Θ(g(n)) (pronounced “big-theta of g of n”) the set of functions: Θ(g(n)) = {f(n) : there exist positive constants c1,c2 and n0 such that 0≤c1 g(n)≤f(n)≤c2 g(n) for all n>n0 }

II. Time complexity notations

While analyzing an algorithm, we mostly consider O-notation because it will give us an upper limit of the execution time i.e. the execution time in the worst case. To compute O-notation we will ignore the lower order terms, since the lower order terms are relatively insignificant for large input. Let f(N)=2N2 + 3N + 5 O(f(N)) = O(2N2 + 3N + 5) = O(N2) Let’s consider some example:

int count = 0; for (int i = 0; i < N; i++) for (int j = 0; j < i; j++) count++; Lets see how many times count++ will run. When i=0, it will run 0 times. When i=1, it will run 1 times. When i=2, it will run 2 times and so on. Total number of times count++ will run is 0+1+2+...+(N−1)=N (N−1)2. So the time complexity will be O(N2).

int count = 0; for (int i = N; i > 0; i /= 2) for (int j = 0; j < i; j++) count++; This is a tricky case. In the first look, it seems like the complexity is O(N∗logN). N for the j′s loop and logN for i′s loop. But its wrong. Lets see why.

Data Structures and Algorithms (CS-212) Lab # 3 Lab # 3 Pointers: Implementation of Null pointer, Void Pointer, Invalid Pointer, Dangling Pointer Reference Variable A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is − type *var-name; Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration − int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

I. Null Pointers

The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Consider the following program #include using namespace std; int main () { int *ptr = NULL; cout << "The value of ptr is " << ptr ; return 0 ; } When the above code is compiled and executed, it produces the following result – The value of ptr is 0 On most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has

Data Structures and Algorithms (CS-212) Lab # 3 special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing. To check for a null pointer you can use an if statement as follows – if(ptr) // succeeds if p is not null if(!ptr) // succeeds if p is null Thus, if all unused pointers are given the null value and you avoid the use of a null pointer, you can avoid the accidental misuse of an uninitialized pointer. Many times, uninitialized variables hold some junk values and it becomes difficult to debug the program.

II. Pointers vs Arrays

Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing. Consider the following program – #include using namespace std; const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr; // let us have array address in pointer. ptr = var; for (int i = 0; i < MAX; i++) { cout << "Address of var[" << i << "] = "; cout << ptr << endl; cout << "Value of var[" << i << "] = "; cout << *ptr << endl; // point to the next location ptr++; } return 0; } When the above code is compiled and executed, it produces result something as follows – Address of var[0] = 0xbfa088b Value of var[0] = 10 Address of var[1] = 0xbfa088b Value of var[1] = 100 Address of var[2] = 0xbfa088b Value of var[2] = 200

Data Structures and Algorithms (CS-212) Lab # 3 TASK Write a function that returns a pointer to the maximum value of an array, if array is empty return NULL. Coding : #include #include<conio.h> using namespace std; int *Maximum(int *a, int size) { if (size == 0) { cout << "\n \t Array is Empty !" << endl; return NULL; } else { int max_val = a[0]; int *max_pos=0; for (int i = 0; i < size; i++) { if (a[i] > max_val) { max_val = a[i]; max_pos = &a[i]; } } return max_pos; } } int main() { int size, *n; int num[10]; cout << "\n \t Enter the size of array : "; cin >> size; for (int i = 0; i < size; i++) { cout << "\n \t Enter " << i + 1 << " Element : "; cin >> num[i]; } n = Maximum(num, size); cout << "\n \t________________________________________ " << endl; cout << " \t The maximum value of an array is : " << *n << endl; cout << " \t________________________________________ " << endl; system("pause"); return 0; }

Data Structures and Algorithms (CS-212) Lab # 3 Output :