0% found this document useful (0 votes)
96 views43 pages

Lab Report

The document contains details about a student's data structures laboratory file. It includes 5 programs completed by the student - implementation of stacks using arrays, queues using arrays, conversion of infix to postfix notation, evaluation of postfix expressions using stacks, and linked list implementation. For each program, it provides the algorithm, code sample, and output. The file contains the student's name, enrollment number, and was submitted to their professor for the data structures course.

Uploaded by

Sakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views43 pages

Lab Report

The document contains details about a student's data structures laboratory file. It includes 5 programs completed by the student - implementation of stacks using arrays, queues using arrays, conversion of infix to postfix notation, evaluation of postfix expressions using stacks, and linked list implementation. For each program, it provides the algorithm, code sample, and output. The file contains the student's name, enrollment number, and was submitted to their professor for the data structures course.

Uploaded by

Sakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

B.

Tech Laboratory File


of
Data Structure Laboratory(CSL206)
Submitted By->
NAME :Manish Jataw
Enroll No: 2020BECE086
Submitted to -> Dr. K. Veningston
(DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING)

DEPARTMENT ELECTRONICS AND COMMUNICATION


NATIONAL INSTITUTE OF TECHNOLOGY SRINAGAR - J&K
AUTUMN 2021
Date-23/08/2021
P1:- Static implementation of stacks.

 Algorithm: -
 Firstly, we create a stack with c capacity.
 Initializing the array to –1.
 For push operation we just increase the top by 1 and put the value in top
position like arr[top]=x;
 For pop operation we just decrease the top position by 1 and then return
the arr[top].
 Actually, stack follows the last in the first out rule so the last element is first
which out.

 Code:-

#include <bits/stdc++.h>
using namespace std;
struct MyStack{
int *arr;
int cap;
int top;

MyStack(int c){
cap=c;
arr=new int [cap];
top=-1;
}

void push(int x){


if(top==cap-1){cout<<"Stack is full"<<endl;return;}
top++;
arr[top]=x;
}

int pop(){
if(top==-1){cout<<"Stack is Empty"<<endl;return INT_MIN;}
int res=arr[top];
top--;
return res;
}

int peek(){
if(top==-1){cout<<"Stack is Empty"<<endl;return INT_MIN;}
return arr[top];
}

int size(){
return (top+1);
}

bool isEmpty(){
return top==-1;
}

};

int main()
{
MyStack s(5);
s.push(80);
s.push(90);
s.push(3);
cout<<s.pop()<<endl;
cout<<s.size()<<endl;
cout<<s.peek()<<endl;
cout<<s.isEmpty()<<endl;
return 0;
}

 Output:-
Date-06/09/2021

 P2:- Static implementation of queues using arrays.

 Algorithm:-
 As we know queues is follows first in first out or last in last out rule.
 A queue maintains two pointers front and rear.
 For enqueue operation -
 If the queue is full, produce overflow error and exit.
 If the queue is not full, increment rear pointer to point the next empty space,
Add data element to the queue location, where the rear is pointing.
 For dequeue operation-
 If the queue is full, produce overflow error and exit.
 if queue is not full ,access the data front is pointing,increament the
front pointer to next available data element.

 Code:-
#include <bits/stdc++.h>
using namespace std;

class Queue {
public:
int front, rear, size;
unsigned capacity;
int* array;
};

Queue* createQueue(unsigned capacity)


{
Queue* queue = new Queue();
queue->capacity = capacity;
queue->front = queue->size = 0;

queue->rear = capacity - 1;
queue->array = new int[(
queue->capacity * sizeof(int))];
return queue;
}

int isFull(Queue* queue)


{
return (queue->size == queue->capacity);
}

int isEmpty(Queue* queue)


{
return (queue->size == 0);
}

void enqueue(Queue* queue, int item)


{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1)
% queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
cout << item << " enqueued to queue\n";
}

int dequeue(Queue* queue)


{
if (isEmpty(queue))
return INT_MIN;
int item = queue->array[queue->front];
queue->front = (queue->front + 1)
% queue->capacity;
queue->size = queue->size - 1;
return item;
}

int front(Queue* queue)


{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}

int rear(Queue* queue)


{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
}

int main()
{
Queue* queue = createQueue(1000);

enqueue(queue, 11);
enqueue(queue, 25);
enqueue(queue, 36);
enqueue(queue, 49);

cout << dequeue(queue)


<< " dequeued from queue\n";

cout << "Front item is "


<< front(queue) << endl;
cout << "Rear item is "
<< rear(queue) << endl;

return 0;
}

 Output:-
Date-13/09/2021

 P3:- Implementation of infix to postfix expression evaluation.


 Algorithm:-
 To convert infix expression to postfix expression, we will use the stack data
structure.
 By scanning the infix expression from left to right,
 when we will get any operand, simply add them to the postfix form, and for
the operator and parenthesis, add them in the stack maintaining the
precedence of them.

 Code:-

#include<iostream>
#include<stack>
#include<locale>
using namespace std;

int precedence(char ch) {


if(ch == '+' || ch == '-') {
return 1; //Precedence of + or - is 1
}else if(ch == '*' || ch == '/') {
return 2; //Precedence of * or / is 2
}else if(ch == '^') {
return 3; //Precedence of ^ is 3
}else {
return 0;
}
}

string infixtopostfix(string infix ) {


stack<char> stk;
stk.push('#');
string postfix = "";
string::iterator it;

for(it = infix.begin(); it!=infix.end(); it++) {


if(isalnum(char(*it)))
postfix += *it;
else if(*it == '(')
stk.push('(');
else if(*it == '^')
stk.push('^');
else if(*it == ')') {
while(stk.top() != '#' && stk.top() != '(') {
postfix += stk.top();
stk.pop();
}
stk.pop();
}else {
if(precedence(*it) > precedence(stk.top()))
stk.push(*it);
else {
while(stk.top() != '#' && precedence(*it) <= precedence(stk.top())) {
postfix += stk.top();
stk.pop();
}
stk.push(*it);
}
}
}

while(stk.top() != '#') {
postfix += stk.top();
stk.pop();
}

return postfix;
}

int main() {
string infix = "3^4/(4*6)+1";
cout << "Postfix Form Is: " << infixtopostfix(infix) << endl;
}

 Output:-
Date-13/09/2021
 P4:- implementation of Postfix expression evaluation using stack.
 Algorithm:-
 Read the element from left to right and push into the stack.
 Then if the current expression is operator than pop the two operands from
the stack and then evaluate it.
 Then push back the result of evaluation.
 Repeat it till the end of the expression.

 Code:-
#include <iostream>
#include <stack>
using namespace std;

int evalPostfix(string exp)


{
stack<int> stack;

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


{
if (exp[i] >= '0' && exp[i] <= '9') {
stack.push(exp[i] - '0');
}
else {
int x = stack.top();
stack.pop();

int y = stack.top();
stack.pop();

if (exp[i] == '+') {
stack.push(y + x);
}
else if (exp[i] == '-') {
stack.push(y - x);
}
else if (exp[i] == '*') {
stack.push(y * x);
}
else if (exp[i] == '/') {
stack.push(y / x);
}
}
}
return stack.top();
}

int main()
{
string exp = "56+67*";//1. 46+2/5*7+"=32
//2. "21 + 3*"+9

cout << evalPostfix(exp);

return 0;
}

 Output:-
Date-20/09/2021

P5:- Linked list implementation.


 Algorithm:-
Single linked list-
 For traverse a linked list-
 Create linked list using nodes.
 Head point to the first node.
 Point the temp variable to head .
 When temp is null we know we reach the end of linked list so
we get out of the while loop.
 Insert at certain position-
 Create new node allocate the memory and stored the data.
 Traverse the node just before the required position to new
node.
 Change the link pointers to include new node between them.
 Delete at certain position-
 Traverse the list just before the element to be deleted.
 Change the link to exclude the node from the chain.

doubly linked list-


 insertion between two nodes-
 create a new node.(prev,next,data)
 Set the next pointer of new node and previous node.
 Set the prev pointer of new node and the next node.

 Code:-

#include<stdio.h>
#include<stdlib.h>
struct node {
int i;
struct node *link;
};
struct node* head=NULL;

void print(){
struct node* t;
t=head;
printf("\n single linked list");
while (t)
{
printf("%d\n", t->i);
t=t->link;
}
printf("Null");

void add_at_bigging(){
struct node* new=(struct node*)malloc(sizeof(struct node));
new->i=5;
new->link=NULL;
new->link=head;
head=new;
}

void add_at_end(){
struct node* new=(struct node*)malloc(sizeof(struct node));
new->i=6;
new->link=NULL;
struct node* t=head;
while(t->link){
t=t->link;
}
t->link=new;
new->link=NULL;

}
void add_at_intermediant(int pos){
int counter=1;
struct node* new=(struct node*)malloc(sizeof(struct node));
new->i=97;
new->link=NULL;
struct node* t=head;
while ( t->link != NULL && counter!=pos )
{
t=t->link;
counter++;
}
new->link=t->link;
t->link=new;
}
int main(){
struct node* n1=(struct node*)malloc(sizeof(struct node));
struct node* n2=(struct node*)malloc(sizeof(struct node));
struct node* n3=(struct node*)malloc(sizeof(struct node));
struct node* n4=(struct node*)malloc(sizeof(struct node));
n1->i=1;n1->link=n2;
n2->i=2;n2->link=n3;
n3->i=3;n3->link=n4;
n4->i=4;n4->link=NULL;
head=n1;
print();
add_at_bigging();
print();
add_at_end();
print();
add_at_intermediant(2);
print();
add_at_intermediant(5);
print();
return 0;

 Output:-
Date-27/09/2021
P6:- To represent the polynomial as a linked list and write a function
to polynomial addition.

 Algorithm:-
 loop around all values of linked list .
 if the value of a node’s exponent. is greater copy this node to result node
and head towards the next node.
 if the values of both node’s exponent is same add the coefficients and then
copy the added value with node to the result.
 Print the resultant node.

 Code:-
#include<bits/stdc++.h>
using namespace std;
struct Node{
int coeff;
int pow;
struct Node *next;
};
void create_node(int x, int y, struct Node **temp){
struct Node *r, *z;
z = *temp;
if(z == NULL){
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
} else {
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL;
}
}
void polyadd(struct Node *p1, struct Node *p2, struct Node *result){
while(p1->next && p2->next){
if(p1->pow > p2->pow){
result->pow = p1->pow;
result->coeff = p1->coeff;
p1 = p1->next;
}
else if(p1->pow < p2->pow){
result->pow = p2->pow;
result->coeff = p2->coeff;
p2 = p2->next;
} else {
result->pow = p1->pow;
result->coeff = p1->coeff+p2->coeff;
p1 = p1->next;
p2 = p2->next;
}
result->next = (struct Node *)malloc(sizeof(struct Node));
result = result->next;
result->next = NULL;
}
while(p1->next || p2->next){
if(p1->next){
result->pow = p1->pow;
result->coeff = p1->coeff;
p1 = p1->next;
}
if(p2->next){
result->pow = p2->pow;
result->coeff = p2->coeff;
p2 = p2->next;
}
result->next = (struct Node *)malloc(sizeof(struct Node));
result = result->next;
result->next = NULL;
}
}
void printpoly(struct Node *node){
while(node->next != NULL){
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}
int main(){
struct Node *p1 = NULL, *p2 = NULL, *result = NULL;
create_node(11,2,&p1);
create_node(29,3,&p1);
create_node(52,0,&p1);
create_node(2,1,&p2);
create_node(5,3,&p2);
printf("polynomial 1: ");
printpoly(p1);
printf("\npolynomial 2: ");
printpoly(p2);
result = (struct Node *)malloc(sizeof(struct Node));
polyadd(p1, p2, result);
printf("\npolynomial after adding p1 and p2 : ");
printpoly(result);
return 0;
}

 Output:-
Date-11/10/2021

P7:- Implementation of in order, preorder and postorder.


 Algorithm:-
 Create a tree.
 Inorder-
 If root is not null then recursive call left then print root then recursive
right.
 Preorder-
 If root is not null then print root recursive call left then recursive right.
 Postorder-
 If root is not null then recursive call left then right then print the root.

 Code:-
#include"bits/stdc++.h"
using namespace std;
struct node{
int data;
struct node*left;
struct node* right;
node(int val)
{
data=val;
left =NULL;
right =NULL;
}
};
void preorder(node*root){
if(root==NULL){
return;
}
cout<<root->data<<" ";
cout<<"\n";
preorder(root->left);
preorder(root->right);

}
void inorder(node*root){
if(root==NULL){
return;
}
inorder(root->left);
cout<<root->data<<" ";
cout<<"\n";
inorder(root->right);

}
void postorder(node*root){
if(root==NULL){
return;
}
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
cout<<"\n";
}

int main()
{
struct node* root = new node(4);
root->left=new node(2);
root->right=new node(6);
root->left->left=new node(1);
root->left->right=new node(3);
root->right->left=new node(5);
root->right->right=new node(7);
cout<<"preorder traversal"<<"\n";
preorder(root);
cout<<"inorder traversal"<<"\n";
inorder(root);
cout<<"postorder traversal"<<"\n";
postorder(root);
return 0;
}
 Output:-
Date-18/10/2021

P8:-Implementation of circular linked list.

 Algorithm:-
o The last link's next points to the first link of the list in both cases of
singly as well as doubly linked list.
o The first link's previous points to the last of the list in case of doubly
linked list.

 Code:-
#include <bits/stdc++.h>
using namespace std;

struct Node{
int data;
Node* next;
Node(int d){
data=d;
next=NULL;
}
};

void printlist(Node *head){


if(head==NULL)return;
cout<<head->data<<" ";
for(Node *p=head->next;p!=head;p=p->next)
cout<<p->data<<" ";
}

int main()
{
Node *head=new Node(342);
head->next=new Node(93);
head->next->next=new Node(90);
head->next->next->next=new Node(85);
head->next->next->next->next=head;
printlist(head);
return 0;
}
 Output:-
Date-25/10/2021

 P9:-implementation of binary search tree operation –


o Insert
o Search
o Delete

 Algorithm:-
o Insert-
 For inserting we call recursive function.
 For this return new node if root is null.
 Otherwise call recursive function for left and right sub tree.
o Search-
 For search operation we call bool recursive function.
 If root is null then return false.
 If root->key is same as enter key then return true.
 Otherwise call recursive function for right and left sub tree.
o Delete-
 For delete operation we defined a recursive delete function and a
successor function that used to find closed value.
 Similar to insert function but if left leaf node is null then point a
temp node to right and delete root and same goes for right node
is null.
 If both node is not empty then find successor then swap with
that and delete the node through recursive delete function

 Code:-
#include<iostream>
using namespace std;
struct node{
int key;
node *left, *right;
node(int x){
key=x;
left=right=NULL;
}
};

node *insert(node *root, int x){


if(root==NULL)
return new node(x);
else if(root->key>x)
root->left=insert(root->left,x);
else if(root->key<x)
root->right=insert(root->right,x);
return root;
}
bool search(node* root,int x){
if(root==NULL)
return false;
else if(root->key==x)
return true;
else if(root->key>x)
return search(root->left,x);
else
return search(root->right,x);
}
void inorder(node *root){
if (root!=NULL)
{
inorder(root->left);
cout<<root->key<<" ";
inorder(root->right);
}

}
node *getsuccer(node *curr){
curr=curr->right;
while(curr!=NULL && curr->left!=NULL)
curr=curr->left;
return curr;
}

node *deletenode(node *root, int x)


{
if(root==NULL)
return root;
else if (root->key>x)
root->left=deletenode(root->left,x);
else if (root->key<x)
root->right=deletenode(root->right, x);
else{
if(root->left==NULL){
node *temp=root->right;
delete root;
return temp;
}
if(root->right==NULL){
node *temp=root->left;
delete root;
return temp;
}
else{
node *succ=getsuccer(root);
root->key=succ->key;
root->right=deletenode(root->right,succ->key);
}
return root;
}
}

int main(){
node *root=new node(10);
root->left=new node(5);
root->right=new node(15);
root->right->right=new node(18);
root->right->left=new node(12);
cout<<"binary tree is : \n "<<endl;
inorder(root);

insert(root,20);

cout<<"\n after inserting : \n"<<endl;


inorder(root);
cout<<"\n searching :\n "<<endl;
if(search(root,20))
cout<<"true"<<endl;
else
cout<<"false"<<endl;

deletenode(root,15);
cout<<"afer deleting : \n"<< endl;
inorder(root);
return 0;
}
 Output:-
Date-01/11/2021

 P10:-implementation of priority queues using heap.


 Algorithm:-
o By default max heap built.
o For insert we use push function.
o For print we use a loop with pop function.

 Code:-
#include <iostream>
#include<queue>
using namespace std;

int main(){
priority_queue <int> pq;
pq.push(10);
pq.push(15);
pq.push(5);
cout<<pq.size()<<" ";
cout<<pq.top()<<" ";
while(pq.empty()==false){
cout<<pq.top()<<" ";
pq.pop();
}

return 0;
}

 Output:-
Date-08/11/2021

 P11:-implementation of hashing techniques.


o Chaining
o Linear probing

 Algorithm:-
o Chaining-
hashIndex = key % noOfBucket
 Now, we will use this hash function and calculate the
hashindex of every inserted value to the hashmap.
 Insert element and calculate the hashIndex of given key
value and then insert the new node to the end of the list.
 To delete a node, we will calculate the hash index and in the
bucket corresponding to hash Index we will search for the
element in the bucket and remove it.
o Linear probing-
 Calculate the hash key. key = data % size;
 If hashTable[key] is empty, store the value directly.
hashTable[key] = data.
 If the hash index already has some value, check for
next index.
 key = (key+1) % size;
 If the next index is available hashTable[key], store the
value. Otherwise try for next index.
 Do the above process till we find the space.

 Code:-
o Chaining-
#include<bits/stdc++.h>
using namespace std;

struct myhash{
int bucket;
list<int>*table;

myhash(int b){
bucket=b;
table=new list<int>[b];
}
void insert(int key){
int i=key%bucket;
table[i].push_back(key);
}
void remove(int key){
int i=key%bucket;
table[i].remove(key);
}
bool search(int key){
int i=key%bucket;
for(auto x : table[i])
if(x==key) return 1;
return 0;
}
void displayHash(){
for (int i = 0; i <bucket; i++){
cout << i;
for (auto x:table[i])
cout << " --> " << x;
cout << endl;
}
}
};

int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, };
int n = 5;
myhash h (7);
for (int i = 0; i < n; i++)
h.insert(a[i]);
h.remove (12);
h.search(67);
h.displayHash();
return 0;
}
o Linear probing-
#include<bits/stdc++.h>
using namespace std;
#define TABLE_SIZE 10

class linearprobing {
int* hashTable;
int curr_size;
public:
bool isFull()
{
return (curr_size == TABLE_SIZE);
}
int hash1(int key)
{
return (key % TABLE_SIZE);
}

linearprobing()
{
hashTable = new int[TABLE_SIZE];
curr_size = 0;
for (int i = 0; i < TABLE_SIZE; i++)
hashTable[i] = -1;
}

void insertHash(int key)


{
if (isFull())
return;
int index = hash1(key);
if (hashTable[index] != -1) {

int i = 1;
while (1) {
int newIndex = (index + i) % TABLE_SIZE;
if (hashTable[newIndex] == -1) {
hashTable[newIndex] = key;
break;
}
i++;
}
}
else
hashTable[index] = key;
curr_size++;
}
void search(int key)
{
int index1 = hash1(key);

int i = 0;
while (hashTable[(index1+ i) % TABLE_SIZE] != key) {
if (hashTable[(index1 + i) % TABLE_SIZE] == -1) {
cout << key << " does not exist" << endl;
return;
}
i++;
}
cout << key << " found" << endl;
}
void displayHash()
{
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable[i] != -1)
cout << i << " --> "
<< hashTable[i] << endl;
else
cout << i << endl;
}
}
};
int main()
{
int a[] = { 12, 34, 56, 78, 89, 90 };
int n = sizeof(a) / sizeof(a[0]);
linearprobing h;
for (int i = 0; i < n; i++) {
h.insertHash(a[i]);
}

h.search(37);
h.search(34);
h.displayHash();
return 0;
}

 Output:-
o Chaining-
o Linear probing-
Date-15/11/2021

 P12:- implementation of various sorting techniques.


 Algorithm-
o Merge sort-

o Quick sort-
 Code:-
o Merge sort-
#include <iostream>
#include <algorithm>
using namespace std;

void merge(int arr[], int l, int m, int h){

int n1=m-l+1, n2=h-m;


int left[n1],right[n2];
for(int i=0;i<n1;i++)
left[i]=arr[i+l];
for(int j=0;j<n2;j++)
right[j]=arr[m+1+j];
int i=0,j=0,k=l;
while(i<n1 && j<n2){
if(left[i]<=right[j])
arr[k++]=left[i++];
else
arr[k++]=right[j++];
}
while(i<n1)
arr[k++]=left[i++];
while(j<n2)
arr[k++]=right[j++];
}

void mergeSort(int arr[],int l,int r){


if(r>l){
int m=l+(r-l)/2;
mergeSort(arr,l,m);
mergeSort(arr,m+1,r);
merge(arr,l,m,r);
}
}

int main() {

int a[]={10,5,30,15,7};
int l=0,r=4;

mergeSort(a,l,r);
for(int x: a)
cout<<x<<" ";
}
o Quick sort-
#include <bits/stdc++.h>
using namespace std;

int iPartition(int arr[], int l, int h)


{
int pivot=arr[h];
int i=l-1;
for(int j=l;j<=h-1;j++){
if(arr[j]<pivot){
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[h]);
return i+1;
}

void qSort(int arr[],int l,int h){


if(l<h){
int p=iPartition(arr,l,h);
qSort(arr,l,p-1);
qSort(arr,p+1,h);
}
}

int main() {

int arr[]={54,45,74,85,11,25,21,12};

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

qSort(arr,0,n-1);

for(int x: arr)
cout<<x<<" ";
}
 Output:-
o Merge sort-

o Quick sort-
Date-15/11/2021

 P15:-implementation of various searching techniques.


o Linear searching
o Binary searching
 Algorithm:-
 Code:-
o Linear searching-
#include<iostream>
using namespace std;

int binerysearch(int a[],int n,int key)


{
int l=0;
int h=n-1;
while(l<=h)
{
int mid=(l+h)/2;
if(key==a[mid])
return mid;
else if(key<a[mid])
h=mid-1;
else
l=mid+1;
}
return -1;
}
int main()
{
int x, n,a[100],key;
cout<<"enter size of array"<<endl;
cin>>n;
cout<<"enter array elements"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"enter key"<<endl;
cin>>key;
cout<<endl;
x=binerysearch(a,n,key);
if(x!=-1)
{
cout<<"key found";
}
else{
cout<<"key doesent found";
}
return 0;
}

o Binary searching-
#include<iostream>
using namespace std;

int linearsearch(int *a,int n,int key)


{
for(int i=0;i<n;i++)
{
if(a[i]==key)
return i;
}
return -1;
}

int main()
{
int x, n,a[100],key;
cout<<"enter size of array"<<endl;
cin>>n;
cout<<"enter array elements"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"enter key"<<endl;
cin>>key;
cout<<endl;
x=linearsearch(a,n,key);
if(x!=-1)
{
cout<<"key found";
}
else{
cout<<"key doesent found";
}
return 0;
}

 Output:-
o Linear searching-

o Binary searching-
Date-22/11/2021

 P14:- implementation of breadth first search(BFS) and depth first


search(DFS).

 Algorithm:-
 Code :-
o DFS-
#include<bits/stdc++.h>
using namespace std;

void DFSRec(vector<int> adj[], int s, bool visited[])


{
visited[s]=true;
cout<< s <<" ";

for(int u:adj[s]){
if(visited[u]==false)
DFSRec(adj,u,visited);
}
}

void DFS(vector<int> adj[], int V, int s){


bool visited[V];
for(int i = 0;i<V; i++)
visited[i] = false;

DFSRec(adj,s,visited);
}

void addEdge(vector<int> adj[], int u, int v){


adj[u].push_back(v);
adj[v].push_back(u);
}

int main()
{
int V=5;
vector<int> adj[V];
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,2,3);
addEdge(adj,1,3);
addEdge(adj,1,4);
addEdge(adj,3,4);

cout << "Following is Depth First Traversal: "<< endl;


DFS(adj,V,0);

return 0;
}

o BFS-
BFS-#include<bits/stdc++.h>
using namespace std;

void BFS(vector<int> adj[], int V, int s)


{
bool visited[V];
for(int i = 0; i < V; i++)
visited[i] = false;

queue<int> q;
visited[s] = true;
q.push(s);

while(q.empty()==false)
{
int u = q.front();
q.pop();
cout << u << " ";

for(int v:adj[u]){
if(visited[v]==false){
visited[v]=true;
q.push(v);
}
}
}
}

void addEdge(vector<int> adj[], int u, int v){


adj[u].push_back(v);
adj[v].push_back(u);
}

int main()
{
int V=5;
vector<int> adj[V];
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,1,2);
addEdge(adj,2,3);
addEdge(adj,1,3);
addEdge(adj,3,4);
addEdge(adj,2,4);

cout << "Following is Breadth First Traversal: "<< endl;


BFS(adj,V,0);

return 0;
}

 Output:-
o BFS-
o DFS-

thank
you

You might also like