




































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 76
This page cannot be seen from the preview
Don't miss anything!





































































COMPUTER SCIENCE DEPARTMENT SIR SYED UNIVERSITY OF ENGINEERING AND TECHNOLOGY, KARACHI
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:
Lab # 1 Getting Started (Revision of C++)
cout<<"You are really old\n"; // Executed if no other statement is } cin.get(); }
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
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> {
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
which is more common. ptr->feet is same as (ptr).feet ptr->inch is same as (ptr).inch
Lab Tasks
#include
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; }
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.
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 }
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.
The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Consider the following program #include
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.
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
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
Data Structures and Algorithms (CS-212) Lab # 3 Output :