Professional Documents
Culture Documents
DSA - Manual22-23 Swarup Devde
DSA - Manual22-23 Swarup Devde
Lab Manual
TRUST’S
SE COMPUTER
MEUA21207
Practical In charge,
Mrs. Pradnya S. Mehta
Dr. Dattatray G. Takale
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Programme Outcomes:
2. To analyse the problem by finding its domain and applying domain specific skills
3. To understand the design issues of the product/software and develop effective solutions with
appropriate consideration for public health and safety, cultural, societal, and environmental
considerations.
11. To apply the knowledge of Computer Engineering for development of projects, and its finance
and management.
12. To keep in touch with current technologies and inculcate the practice of lifelong learning.
Course Objectives:
1) To study data structures and their implementations and applications.
2) To learn different searching and sorting techniques.
3) To study some advanced data structures such as trees, graphs and tables.
4) To learn algorithm development and analysis of algorithms.
Course Outcomes:
On completion of the course, learners will be able to
1. Perform basic analysis of algorithms with respect to time and space complexity.
2. Select appropriate searching and/or sorting techniques and Implement data structures for
given application.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
INDEX
Assignment. Title of Assignment Page
No. No.
7 Represent graph as adjacency matrix or list and perform Depth first Traversal 28
and Breadth First Traversal
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
CO-PO-PSO Mapping
CO-PO Mapping
PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO
Perform 3 3 3 3
basic
analysis of
algorithms
with
respect to
time and
space
complexity.
Select 3 3 3 3
appropriate
searching
and/or
sorting
techniques
and
Implement
data
structures
for given
application.
CO-PSO Mapping
PO PSO1 PSO2
CO
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
ASSIGNMENT 1
AIM/ Problem Statement: Write a program to perform operations on matrices like addition,
multiplication, transpose etc using functions & Pointers.
Theory:
Basic operations
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Display C[i,j];
Multiplication of 2 matrices
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
11. 6 C[i, j] =0
12. 7 for k = 1 to columns [A]
13. 8 C[i, j]=C[i, j]+A[i, k]*B[k, j]
14. 9 return C
Transpose of 2 matrices
1. transpose(A, B);
2. cout << "Result matrix is \n";
3. for (i = 0; i < N; i++) {
4. for (j = 0; j < N; j++)
5. cout << " " << B[i][j];
6. cout << "\n";
7. }
Return
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Source Code:
#include <iostream>
do
{
cout <<" ***menu***";
// cout << "1) Transpose of the Matrix" <<endl;
cout<< "1)Add two matrices" <<endl;
cout<<" 2)Multiply two matrices"<<endl;
cout<<"3) Tanspose of matrix"<<endl;
cout<<"\nEnter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
case 3:
cout << "Enter rows and columns for first matrix: ";
createm1(a,r1,c1);
}
cout<<"\n Do u want to continue\n";
cin>>ch1;
}while(ch1=='Y' || ch1=='y');
return 0;
}
{
int i,j;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
cout << c[i][j] << " ";
if(j == c1 - 1)
cout << endl;
}
}
Conclusion:
Thus we have studied various matrix operations.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
ASSIGNMENT 2
TITLE: Write a menu driven Program in C++ for the following operations on Singly Linked List
(SLL) of Student Data with the fields: PRN, Name, Branch, Semester, Cell Number
a. Create a SLL of N Students
b. Display the SLL and count the number of nodes in it
c. Perform Insertion
d. Perform Deletion
Aim/ Problem Statement: Write a menu driven program to perform following operations on SLL:
Create, Insert – Start, end, between, Search & delete, Display etc.
Relevant Course Objectives: To learn the concept of link list and perform various operations on it.
Theory:
A linked list is a data structure consisting of a group of nodes which together represent a sequence.
Under the simplest form, each node is composed of a data and a reference (in other words, a link) to
the next node in the sequence; more complex variants add additional links. This structure allows for
efficient insertion or removal of elements from any position in the sequence.
The principal benefit of a linked list over a conventional array is that the list elements can easily be
inserted or removed without reallocation or reorganization of the entire structure because the data
items need not be stored contiguously in memory or on disk, while an array has to be declared in the
source code, before compiling and running the program.
Singly linked list
Singly linked lists contain nodes which have a data field as well as a next field, which points to the
next node in line of nodes. Operations that can be performed on singly linked lists include insertion,
deletion and traversal.
A singly linked list whose nodes contain two fields: an integer value and a link to the next node
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
In a doubly linked list, each node contains, besides the next-node link, a second link field pointing
to the previous node in the sequence. The two links may be called forward(s) and backwards,
or next and prev(previous).
A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next
node, and the link backward to the previous node
A technique known as XOR-linking allows a doubly linked list to be implemented using a single
link field in each node. However, this technique requires the ability to do bit operations on
addresses, and therefore may not be available in some high-level languages.
Input:
Insert a new node at the head of the list is straightforward. The main idea is that we create a new
node, set its next link to refer to the current head, and then set head to point to the new node.
If we keep a reference to the tail node, then it would be easy to insert an element at the tail of the list.
Assume we keep a tail node in the class of SLinkedList, the idea is to create a new node, assign its
next reference to point to a null object, set the next reference of the tail to point to this new object,
and then assign the tail reference itself to this new node. Initially both head and tail point to null
object.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
v.setNext(null)
if (head == null) { // list is empty
head = v
} else { // list is not empty
tail.setNext(v)
}
tail = v
size = size + 1
Removal of an element at the head of a singly linked list is relatively easy. However removing a tail
node is not easy.
Algorithm removeFirst()
if (head = = null) then
Indicate an error: the list is empty
tmp = head
head = head.getNext()
tmp.setNext(null)
size = size - 1
Algorithm traverseList()
curNode = head
while (curNode != null) {
// print out the contents of the current node
curNode = curNode.getNext()
}
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Source Code:
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int roll;
string Name;
string Dept;
int Marks;
Node* next;
};
Node* head = new Node();
bool check(int x)
{
if (head == NULL)
return false;
while (t != NULL) {
if (t->roll == x)
return true;
t = t->next;
}
return false;
}
if (head == NULL
|| (head->roll >= t->roll)) {
t->next = head;
head = t;
}
else {
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Node* c = head;
while (c->next != NULL
&& c->next->roll < t->roll) {
c = c->next;
}
t->next = c->next;
c->next = t;
}
else {
Node* p = head;
while (p) {
if (p->roll == roll) {
cout << "Roll Number\t"
<< p->roll << endl;
cout << "Name\t\t"
<< p->Name << endl;
cout << "Department\t"
<< p->Dept << endl;
cout << "Marks\t\t"
<< p->Marks << endl;
return;
}
p = p->next;
}
if (p == NULL)
cout << "No such Record "
<< "Available\n";
}
}
if (t != NULL
&& t->roll == roll) {
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
head = t->next;
delete t;
delete t;
cout << "Record Deleted "
<< "Successfully\n";
return 0;
}
}
void Show_Record()
{
Node* p = head;
if (p == NULL) {
cout << "No Record "
<< "Available\n";
}
else {
cout << "Index\tName\tCourse"
<< "\tMarks\n";
while (p != NULL) {
cout << p->roll << " \t"
<< p->Name << "\t"
<< p->Dept << "\t"
<< p->Marks << endl;
p = p->next;
}
}
}
int main()
{
head = NULL;
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
string Name, Course;
int Roll, Marks;
while (true) {
cout << "\n\t\tWelcome to Student Record "
"Management System\n\n\tPress\n\t1 to "
"create a new Record\n\t2 to delete a "
"student record\n\t3 to Search a Student "
"Record\n\t4 to view all students "
"record\n\t5 to Exit\n";
cout << "\nEnter your Choice\n";
int Choice;
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Conclusion:
Thus we ha
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
ASSIGNMENT NO: 3
Relevant Course Objectives: To learn the concept of Stack using Array and perform various
operations on it.
Theory:
A stack data structure can be implemented using a one-dimensional array. But stack implemented
using array stores only a fixed number of data values. This implementation is very simple. Just define
a one dimensional array of specific size and insert or delete the values into that array byusing
LIFO principle with the help of a variable called 'top'. Initially, the top is set to -1. Whenever we want
to insert a value into the stack, increment the top value by one and then insert. Whenever we want to
delete a value from the stack, then delete the top value and decrement the top value by one.
Input:
A stack can be implemented using array as follows. Before implementing actual operations, first
follow the below steps to create an empty stack.
Step 1: Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
Step 2: Declare all the functions used in stack implementation.
Step 3: Create a one dimensional array with fixed size (int stack[SIZE])
Step 4: Define a integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5: In main method, display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
In a stack, push () is a function used to insert an element into the stack. In a stack, the new element
is always inserted at top position. Push function takes one integer value as parameter and inserts that
value into the stack. We can use the following steps to push an element on to the stack...
Step 1: Check whether stack is FULL. (top == SIZE-1)
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the
function.
Step 3: If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is
always deleted from top position. Pop function does not take any value as parameter. We can use the
following steps to pop an element from the stack...
Steps 1: Check whether stack is EMPTY. (top == -1)
Step 2: If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate
the function.
Step 3: If it is NOT EMPTY, then delete stack [top] and decrement top value by one (top--).
Source Code:
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Conclusion:
Thus we have studied various Stack using Array operations.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
ASSIGNMENT NO: 4
Title: Implement FCFS (Queue) algorithm of job scheduling in operating system with the help of suitable
data structure.
Theory:
When we run a program, we create a particular instance of the program called process. There might
be a condition where more than one process is created at a given time and the CPU has to serve all
the processes. There is a various process scheduling algorithm that decides which process has to be
executed at a given time by considering various factors. FCFS or First come first serve is one such
algorithm that schedules the process
Scope
This article will explain the FCFS algorithm along with the Gantt chart.
The first come first serve scheduling algorithm is non-preemptive in nature i.e, if a process is already
running, then it is not interrupted by another process until the currently running process is executed
completely.
Buying a movie ticket from the ticket counter is a perfect real-life example of a first come first
serve (fcfs) algorithm. The person who comes first and stands in the queue gets to buy the ticket first.
Similarly in the fcfs scheduling algorithm, the process which arrives first gets executed first.
Input:
Let us consider an example where 4 processes with different burst times arrive at different times.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
P1 6 2
P2 8 1
P3 3 0
P4 4 4
Let us see how the above processes are handled using the First come first serve scheduling
algorithm.
At time t=0
P1 6 2
P2 8 1
P4 4 4
From the above chart, we can see that at time 0, the process P3 arrives. Once the process P3 arrives,
the process is served as there is no other process that is being executed.
At time t=1
P1 6 2
1 (please add this row in different color but same color as the one used
P2 8
in previous image)
P3 3 0
P4 4 4
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
The process P2 arrives at time t=1, but the program P3 is still executing. The process P2 will be
kept in a queue which will be executed after the existing process finishes its execution. Queue
Gantt Chart
At time t=2
2 (please add this row in different color but same color as the one used
P1 6
in previous image)
P2 8 1
P3 3 0
P4 4 4
At time t=2, Process P1 arrives and waits in the queue as there is another process being executed.
Queue
At time t=3
At time t=3, Process P3 finished execution after 3 seconds. In the queue, the process P2 is waiting
for the execution Therefore process P2 begins its execution.
At time t=5
P1 6 2
P2 8 1
P3 3 0
4 (please add this row in different color but same color as the one used in
P4 4
previous image)
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
At time t=21
The process which arrives first would be served first based on a first come first serve basis.
Source Code:
#include<iostream>
using namespace std;
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
cout<<"Enter total number of processes(maximum 20):";
cin>>n;
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
avwt/=i;
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
avtat/=i;
cout<<"\n\nAverage Waiting Time:"<<avwt;
cout<<"\nAverage Turnaround Time:"<<avtat;
return 0;
}
Conclusion:
Thus we have studied FCFS (Queue) algorithm of job scheduling.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
ASSIGNMENT NO: 5
Title: Write C++ program to maintain club members, sort on roll numbers in ascending order. Write
function for Binary Search and Linear Search to search whether particular student is member of club or
not.
AIM/ PROBLEM STATEMENT: Write C++ program to maintain club members, sort on roll numbers
in ascending order. Write function for Binary Search and Linear Search to search whether particular
student is member of club or not.
Relevant Course Objectives: To learn the concept unction for Binary Search and Linear Search to
search whether particular student is member of club or not.
Theory:
A search algorithm is an algorithm for finding an item with specified properties among
a collection of items.
Sequential search: is a method for finding a particular value in a list that consists of checking
every one of its elements, one at a time and in sequence, until the desired one is found.
Binary search or half-interval search algorithm finds the position of a specified input value (the
search "key") within an array sorted by key value.
.
Indexed sequential Search: An index file can be used to effectively overcome the above mentioned
problem, and to speed up the key search as well. The simplest indexing structure is the single-level
one: a file whose records are pair’s key-pointer, where the pointer is the position in the data file of
the record with the given key. Only a subset of data records, evenly spaced along thedata file, are
indexed, so to mark intervals of data records.
A key search then proceeds as follows: the search key is compared with the index ones to find the
highest index key preceding the search one, and a linear search is performed from the record the index
key points onward, until the search key is matched or until the record pointed by the next index entry
is reached. In spite of the double file access (index + data) needed by this kind of search, the decrease
in access time with respect to a sequential file is significant.
Input:
Algorithmic Steps/Pseudo code/Program:
Sequential search
For each item in the list:
if that item has the desired value,
stop the search and return the item's location.
Return Λ.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Binary Search
int binary_search(int A[], int key, int imin, int imax)
{
// continue searching while [imin,imax] is not empty
while (imax >= imin)
{
// calculate the midpoint for roughly equal partition
int imid = midpoint(imin, imax);
if(A[imid] == key)
// key found at index imid
return imid;
// determine which subarray to search
else if (A[imid] < key)
// change min index to search upper subarray
imin = imid + 1;
else
// change max index to search lower subarray
imax = imid - 1;
}
// key was not found
return KEY_NOT_FOUND;
}
Source Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int arr[100], st, mid, end, i, n, tgt,j,temp;
bool swapped;
cout << " Define the size of the array: " << endl;
cin >> n;
cout << " Enter the values of array " << endl;
for (i = 0; i < n; i++)
{
cout << " arr [" << i << "] = ";
cin >> arr[i];
}
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
if (swapped == false)
break;
}
cout<< "sorted array is ";
for (i = 0; i < n; i++)
cout <<" "<< arr[i]<<endl;
st = 0;
end = n - 1;
cout << " Define a value to be searched from sorted array: " << endl;
cin >> tgt;
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
ASSIGNMENT NO: 6
Title: Department maintains student’s database. The file contains roll number, name, division and
address. Write a program to create a sequential file to store and maintain student data. It should allowthe
user to add, delete information of student. Display information of particular student. If record of student
does not exist an appropriate message is displayed. If student record is found it should display the student
details.
Theory:
The file represents a sequence of bytes, does not matter if it is a text file or binary file. Cprogramming
language provides access on high level functions as well as low level (OS level) calls to handle file
on your storage devices.
Opening Files
You can use the fopen( ) function to create a new file or to open an existing file, this call will initialize
an object of the type FILE, which contains all the information necessary to control the stream.
Following is the prototype of this function call:
Mode Description
Opens a text file for writing, if it does not exist then a new file is created.
w
Here your program will start writing content from the beginning of the file.
Opens a text file for writing in appending mode, if it does not exist then a new
a file is created. Here your program will start appending content in the existing
file content.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
Opens a text file for reading and writing both. It first truncate the file to zero
w+
length if it exists otherwise create the file if it does not exist.
Opens a text file for reading and writing both. It creates the file if it does not
a+ exist. The reading will start from the beginning but writing can only be
appended.
Closing a File
To close a file, use the fclose( ) function. The prototype of this function is:
The fclose( ) function returns zero on success, or EOF if there is an error in closing the file. This
function actually, flushes any data still pending in the buffer to the file, closes the file, and releases
any memory used for the file. The EOF is a constant defined in the header file stdio.h.
Writing a File
The function fputc() writes the character value of the argument c to the output stream referenced by
fp. It returns the written character written on success otherwise EOF if there is an error. You can use
the following functions to write a null-terminated string to a stream:
The function fputs() writes the string s to the output stream referenced by fp. It returns a non- negative
value on success, otherwise EOF is returned in case of any error. You can use int fprintf(FILE
*fp,const char *format, ...) function as well to write a string into a file.
Reading a File
The fgetc() function reads a character from the input file referenced by fp. The return value is the
character read, or in case of any error it returns EOF. The following functions allow you to read a
string from a stream:
The functions fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the
read string into the buffer buf, appending a null character to terminate the string.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
If this function encounters a newline character '\n' or the end of the file EOF before they have read
the maximum number of characters, then it returns only the characters read up to that pointincluding
new line character. You can also use int fscanf(FILE *fp, const char *format, ...) function to read
strings from a file but it stops reading after the first space character encounters.
Writing a File
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fclose(fp);
Reading a File
FILE *fp;
char buff[255];
fp = fopen("/tmp/test.txt", "r");
fclose(fp);
Conclusion:
Thus we have studied a program to perform various operations on any database: Add, Delete, Modify,
Display, Search & Sort etc.
Source Code:
#include<bits/stdc++.h>
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
using namespace std;
class tel
{
public:
int rollNo,roll1;
char name[10];
char div;
char address[20];
void accept()
{
cout<<"\n\tEnter Roll Number : ";
cin>>rollNo;
cout<<"\n\tEnter the Name : ";
cin>>name;
cout<<"\n\tEnter the Division:";
cin>>div;
cout<<"\n\tEnter the Address:";
cin>>address;
}
void accept2()
{
cout<<"\n\tEnter the Roll No. to modify : ";
cin>>rollNo;
}
void accept3()
{
cout<<"\n\tEnter the name to modify : ";
cin>>name;
}
int getRollNo()
{
return rollNo;
}
void show()
{
cout<<"\n\t"<<rollNo<<"\t\t"<<name<<"\t\t"<<div<<"\t\t"<<address;
}
};
int main()
{
int i,n,ch,ch1,rec,start,count,add,n1,add2,start2,n2,y,a,b,on,oname,add3,start3,n3,y1,add4,start4,n4;
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
char name[20],name2[20];
tel t1;
count=0;
fstream g,f;
do
{
cout<<"\n>>>>>>>>>>>>>>>>>>>>>>MENU<<<<<<<<<<<<<<<<<<<<";
cout<<"\n1.Insert and overwrite\n2.Show\n 3.Delete a Student Record\n 4.Exit\n\tEnter the
Choice\t:";
cin>>ch;
switch(ch)
{
case 1:
f.open("StuRecord.txt",ios::out);
x:t1.accept();
f.write((char*) &t1,(sizeof(t1)));
cout<<"\nRecord inserted";
cin>>ch1;
if(ch1==1)
goto x;
else
{
f.close();
break;
}
case 2:
f.open("StuRecord.txt",ios::in);
f.read((char*) &t1,(sizeof(t1)));
while(f)
{
t1.show();
f.read((char*) &t1,(sizeof(t1)));
}
f.close();
break;
case 3:
int roll;
cout<<"Please Enter the Roll No. of Student Whose Info You Want to Delete: ";
cin>>roll;
f.open("StuRecord.txt",ios::in);
g.open("temp.txt",ios::out);
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
f.read((char *)&t1,sizeof(t1));
while(!f.eof())
{
if (t1.getRollNo() != roll)
g.write((char *)&t1,sizeof(t1));
f.read((char *)&t1,sizeof(t1));
}
cout << "The record with the roll no. " << roll << " has been deleted " << endl;
f.close();
g.close();
remove("StuRecord.txt");
rename("temp.txt","StuRecord.txt");
break;
case 4:
cout<<"\n\tThank you";
break;
}
}while(ch!=4);
}
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
ASSIGNMENT NO: 7
Title: Case study to Represent graph as adjacency matrix or list and perform Depth first Traversal
and Breadth First Traversal
AIM/ PROBLEM STATEMENT: Represent graph as adjacency matrix or list and perform Depth
first Traversal and Breadth First Traversal
Relevant Course Objectives: To learn the concept of adjacency matrix or list and perform Depth
first Traversal and Breadth First Traversal
Theory:
Breadth First Search (BFS) has been discussed in this article which uses adjacency list for the graph
representation. In this article, adjacency matrix will be used to represent the graph.
BFS algorithm
A standard BFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the back of the queue.
VIIT, PUNE
DSA Lab Manual SE MECH, Sem-I, 2022-23
The graph might have two different disconnected parts so to make sure that we cover every vertex,
we can also run the BFS algorithm on every node
A standard DFS implementation puts each vertex of the graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the top of the stack.
VIIT, PUNE