DS Practicals SE
DS Practicals SE
List of
Experiments
Subject:- Data Structures Laboratory – CSL303 Class: SE SEM: III
Subject In-Charges:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust Atma
Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 1
Theory:
Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Stack is an
abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows
adding and removing elements in a particular order. Every time an element is added, it goes on
the top of the stack and the only element that can be removed is the element that is at the top of
the stack, just like a pile of objects.
Program:
#include<conio.h {
1;void push(); {
void peek();
void }
display();int else
main() {
clrscr(); top++;
printf("\n\t1.PUSH\n\t2.POP\n\t }
3.DISPLAY\n\t4.PEEK \n\t5.EXIT\n"); }
do void pop()
{ {
scanf("%d", &choice); {
{ }
case 1: else
{
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
push() {
break; }
} void display()
case 4: {
{ int i;
break; {
{ printf("%d\n", stack[i]);
printf("Exit }
Point");break; else
} {
{ }
} }
} while (choice !=
5);getch();
return 0;
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 2
Theory:
Expression conversion is the most important application of stack. Given an infix expression, it
can be converted to both prefix and postfix expression. Postfix notation is said to be harder to
learn.
Infix expression:The expression of the form a op b. When an operator is in-between every pair
of operands.
Postfix expression:The expression of the form a b op. When an operator is followed for every
pair of operands.
Algorithm:
Program:
#include<conio.h> }
#include<string.h> {
else {
{ return(1);
top = top+1; }
stack[top] = item; else
} {
}char pop() return(0);
{ }
char item ;
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
exit(1); char x;
top = top-1;
} i=0
return(item); ;
{ ;
postfix_exp[j] =
else if(is_operator(item) == 1) item; /* add operand symbol to
/* means symbol is operator */ postfix expr */
{ j++;
x=pop()
;while(is_operator(x) == 1 }
&&
illegeal symbol */
precedence(x)>= precedence(item))
{
}
postfix_exp[j] = x; /* so pop all if(top>0)
higher precendence operator and */
{
j++;
printf("\nInvalid infix
x = pop(); Expression.\n"); /* the it is illegeal
/* add them to postfix expresion */ symbol */
}
x = pop(); /* pop
and keep popping until */ postfix_exp[j] = '\0';
while(x != '(') /* '(' }
encounterd */
{
int main()
postfix_exp[j] =
{
x;j++;
clrscr();
x = pop();
char infix[SIZE], postfix[SIZE];
} /* declare infix string and postfix string */
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
gets(infix);
InfixToPostfix(infix,postfix);
/* call to convert */
getch();
return 0;
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 3
Theory: Postfix evaluation is an important concept in computer science that allows us to perform
arithmetic operations on postfix expressions. In this article, we will discuss postfix evaluation in the
context of C programming language.
Postfix evaluation algorithm is a simple algorithm that allows us to evaluate postfix expressions. The
algorithm uses a stack to keep track of operands and performs arithmetic operations when an operator
is encountered.
Algorithm:
The algorithm can be summarized in the following steps:
4. If an operator is encountered, pop the top two operands from the stack, perform the operation,
and push the result back onto the stack.
5. After that, it Continue scanning the expression until all tokens have been processed.
6. When the expression has been fully scanned, the result will be the top element of the stack.
Program:
#include <stdio.h> }
#include <stdlib.h> int pop() {
#define MAX_SIZE 100 if (top < 0) {
// Stack implementation printf("Stack Underflow\n");
int stack[MAX_SIZE]; return -1;
int top = -1; }
void push(int item) { int item = stack[top];
if (top >= MAX_SIZE - 1) { top--;
printf("Stack Overflow\n"); return item;
return; }
} int is_operator(char symbol) {
top++; if (symbol == '+' || symbol == '-' ||
stack[top] = item; symbol == '*' || symbol == '/') {
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
return 1;
}
return 0; }
} push(result);
int evaluate(char* expression) { }
int i = 0; i++;
char symbol = expression[i]; symbol = expression[i];
int operand1, operand2, result; }
result = pop();
while (symbol != '\0') { return result;
if (symbol >= '0' && symbol <= }
'9') {
int num = symbol - '0'; int main() {
push(num); char expression[] = "5 6 7 + * 8 -";
} int result = evaluate(expression);
else if (is_operator(symbol)) { printf("Result= %d\n", result);
operand2 = pop(); return 0;
operand1 = pop(); }
switch(symbol) {
case '+': result = operand1 +
operand2; break;
case '-': result = operand1 -
operand2; break;
case '*': result = operand1 *
operand2; break;
case '/': result = operand1 /
operand2; break;
Outcome:
Result= 57
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 4
Theory:
A queue is an abstract data structure that contains a collection of elements. Queue implementsthe
FIFO mechanism i.e. the element that is inserted first is also deleted first. In other words, the
least recently added element is removed first in a queue.
In this, the function Insert() inserts an element into the queue. If the rear is equal to n-1, then the
queue is full and overflow is displayed. If front is -1, it is incremented by 1. Then rear is
incremented by 1 and the element is inserted in index of rear.
In the function Delete(), if there are no elements in the queue then it is underflow condition.
Otherwise the element at front is displayed and front is incremented by one.
In the function display(), if front is -1 then queue is empty. Otherwise all the queue elements
are displayed using a for loop.
Algorithm:
If.
by 1][End of If]
4. Exit
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Program:
else printf("4.Quit\n");
rear = rear+1;
} printf("Enter your choice : ");
queue[rear] = i ; scanf("%d",&choice);
}
void del() switch(choice)
{ {
if (front == -1) case 1 :
{ printf("Input the
printf("Queue Underflow\n"); element for insertion in queue : ");
return ; scanf("%d", &i);
}
printf("Element deleted from queue is : insert(i);
%d\n",queue[front]); break;
if(front == rear) case 2 :
{ del();
front = -1; break;
rear=-1; case 3:
} display();
else break;
{ case 4:
if(front == MAX-1) break;
front = 0; default:
else printf("Wrong
front = front+1; choice\n");
} }
} }while(choice!=4);
return 0;
}
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 5
Theory:
Circular Queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make
a circle. It is also called ‘Ring Buffer’.
Program:
: %d\n",queue[front]); insert(i);
if(front == rear) break;
{ case 2 :
front = -1; del();
rear=-1; break;
} case 3:
else display()
{ ; break;
if(front == MAX-1) case 4:
front = 0;
break;
else
default:
front = front+1;
printf("Wrong
}
choice\n");
}
}
void display()
}while(choice!=4);
{
int x = front,y =
return 0;
rear;if(front == -1)
}
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( x<= y )
while(x <= y)
{
printf("%d
",queue[x]);x++;
}
else
{
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 6
Theory:
A linked list is a linear collection of data elements. These data elements are called nodes. Linked list is a
data structure which in turn can be used to implement other data structures. Thus, it acts as a building
block to implement data structures such as stacks, queues, and their variations. A linked list can be
perceived as a train or a sequence of nodes in which each node contains one or more data fields and a
pointer to the next node.
Algorithms:
To insert a new node at the beginning To insert a new node at the end
Step 1: IF AVAIL = NULL Step 1: IF AVAIL = NULL
Write Write
OVERFLOWGo
OVERFLOWGo
to Step 1
to Step 7 [END [END OF IF]
OF IF] Step 2: SET = AVAIL Step
Step 2: SET NEW_NODE = 3: SET AVAIL = AVAIL
AVAILStep 3: SET AVAIL = NEXTStep 4: SET DATA
AVAIL NEXT Step 4: SET = VAL
DATA = VAL Step 5: SET NEW_NODE =
NULLStep 6: SET PTR = START
Step 5: SET NEW_NODE NEXT =
Step 7: Repeat Step 8 while PTR NEXT !=
STARTStep 6: SET START = NULLStep 8: SET PTR = PTR NEXT
NEW_NODE [END OF LOOP]
Step 7: EXIT Step 9: SET PTR
NEXT =Step 10: EXIT
To insert a new node after a node that has value To insert a new node before a node that has
NUM value NUM
Step 1: IF AVAIL = NULL Step 1: IF AVAIL = NULL
Write Write
OVERFLOWGo OVERFLOWGo
to Step 12 [END to Step 12 [END
OF IF] OF IF]
Step 2: SET = AVAIL Step 2: SET = AVAIL
Step 3: SET AVAIL = AVAIL Step 3: SET AVAIL = AVAIL NEXT
NEXTStep 4: SET DATA = Step 4: SET NEW_NODE->DATA =
VAL VALStep 5: SET PTR = START
Step 5: SET PTR = Step 6: SET PREPTR = PTR
START Step 6: SET Step 7: Repeat Steps 8 and 9 while PTR DATA !=
PREPTR = PTR NUM
Step 7: Repeat Steps 8 and 9 while PTR NEXT !=
NUM
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Program:
{ else
case 1:create();break; {r=start;
case 2:Delete();break; while(q->next!=q)
case 3:display();break; {r=r->next;
case 4:exit(0); break; r->next=q-
default : printf("wrong choice"); >next;q-
} >next=NULL;
}} free(q);
void create() }}}}}
{p=malloc(sizeof(struct void display()
Node));printf("\nenter data"); {
scanf("%d",&p->info); if (start==NULL)
p- printf ("\nlinked list is empty");
>next=NULL; else
if(start==NUL {
L)start=p; q=start;
else while (q!=NULL)
{q=start; {printf("\t %d",q-
while(q->next!=NULL) >info);q=q->next;
{q=q- }}}
>next;}q-
>next= p;
}
}
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 7
Theory:
If the array size cannot be determined in advance, then the other alternative, i.e., linked representation, is
used. The storage requirement of linked representation of the stack with n elements is O(n), and the
typical time requirement for the operations is O(1). In a linked stack, every node has two parts—one that
stores data and another that stores the address of the next node. The START pointer of the linked list is
used as TOP. All insertions and deletions are done at the node pointed by TOP. If TOP = NULL, then it
indicates that the stack is empty.
Algorithm:
Program:
clrscr(); }
top=NULL; }
void pop()
printf("\n\n1.PUSH\n2.POP\n3.DISPLA {
Y\n4.EXIT"); q=top;
while(1) if(q==NULL)
{ printf("\nStack is empty");else{
printf("\nEnter yourchoice"); printf("Element
scanf("%d",&ch); popped=%d",top->info);
switch(ch) top=top->next;q-
{ >next=NULL;
case 1: push();break; free(q);
case 2:pop();break; }
case 3:Display();break; }
case 4:exit(1); void Display()
default:printf("\nWrong {
choice"); q=top;
} if(q==NULL)
} printf("\nStack is empty");Else{
getch(); printf(“\n Stack elementsare: \t”);
} while(q!=NULL)
{
printf("\t%d",q->info);q=q->next;
}}}
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 8
Theory:
A binary search tree, also known as an ordered binary tree, is a variant of binary tree in which the nodes
are arranged in an order. In a binary search tree, all the nodes in the left sub-tree have a value less than
that of the root node. Correspondingly, all the nodes in the right sub-tree have a value either equal to or
greater than the root node.
Algorithms:
to insert a given value in a binary search tree to search for a given value in a binary
searchtree
InsertElement(TREE,VAL)
Step 1: Allocate memory for new variable ptr SearchElement (TREE, VAL)
SET ptr–>data = val Step 1: IF TREE DATA = VAL OR TREE =
SET ptr–>left = NULL
NULL SET ptr– Return
>right = NULLStep TREEELSE
2: IF VAL < TREE DATA
IF Return searchElement(TREE LEFT,
TREE=NUL VAL)ELSE
LSET Return searchElement(TREE RIGHT,
TREE=ptr VAL)[END OF IF]
else [END OF IF]
SET parentptr=NULL Step 2: END
SET nodeptr=TREE
While to delete a binary search tree
nodeptr!=NULLdo
SET parentptr=nodeptr deleteTree(TREE)
IF val<nodeptr–>data Step 1: IF TREE != NULL
SET nodeptr=nodeptr–>left deleteTree (TREE
else LEFT) deleteTree
SET nodeptr = nodeptr–>right (TREE RIGHT)Free
END OF IF (TREE)
IF val<parentptr–>data [END OF IF]
SET parentptr–>left = ptr; Step 2: END
else
SET parentptr–>right = ptr;
END OF IF
END OF
WHILEEND
OF IF
Step 3: END
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
totalNodes(TREE) totalExternalNodes(TRE
Step 1: IF TREE = E)Step 1: IF TREE =
NULL NULL
Return o Return 0
ELSE ELSE IF TREE LEFT = NULL AND TREE
Return totalNodes(TREE LEFT) RIGHT = NULL
+ totalNodes(TREE RIGHT) Return 1
+ 1[END OF IF] ELSE
Step 2: END Return totalExternalNodes(TREE LEFT) +
totalExternalNodes(TREE RIGHT)
[END OF IF]
Step 2: END
to find the largest node in a binary search tree to find the smallest node in a binary search tree
findLargestElement(TREE) findSmallestElement(TREE)
Step 1: IF TREE = NULL OR TREE RIGHT = Step 1: IF TREE = NULL OR TREE LEFT =
NULL NULL
Return Return
TREEELSE TREEELSE
Return findLargestElement(TREE Return findSmallestElement(TREE
RIGHT)[END OF IF] LEFT)[END OF IF]
Step 2: END Step 2: END
Program:
#include<stdio.h> }
#include<conio.h> if(val<parentptr->data)
#include<malloc.h> parentptr->left=ptr;
struct node else
{ parentptr->right=ptr;
int data; }
struct node *left; return tree;
struct node *right; }
}; struct node *smallest_element(struct node *tree)
struct node *tree=NULL; {
struct node *ins_element(struct node *,int); if((tree==NULL)||(tree->left==NULL))
struct node *search(struct node *,int); return tree;
struct node *smallest_element(struct node *); else
struct node *largest_element(struct node *); return(smallest_element(tree-
struct node *delete_element(struct node *); >left));
int height(struct node *); }
int internal_nodes(struct node *);
int external_node(struct node *); struct node *largest_element(struct node *tree)
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
break; if((tree==NULL)||((tree-
case 7: >left==NULL)&&(tree->right==NULL)))
printf("\nTotal number of return 0;
internal nodes is %d",internal_node(tree)); else
break; return(internal_node(tree-
case 8: >left)+internal_node(tree->right)+1);
printf("\nTotal number of }
external nodes is %d",external_node(tree));
break; int total_node(struct node *tree)
case 9: {
printf("\nTotal number of nodes if(tree==NULL)
is %d",total_node(tree)); return 0;
break; else
} return(total_node(tree-
}while(ch!=10); >left)+total_node(tree->right)+1);
getch(); }
return 0;
}
struct node *search(struct node *tree,int val)
struct node *ins_element(struct node *tree,int {
val) if((tree->data==val)||(tree==NULL))
{ return tree;
struct node *ptr,*nodeptr,*parentptr; if(val<tree->data)
ptr=(struct node *)malloc(sizeof(struct search(tree->left,val);
node)); else
ptr->data=val; search(tree->right,val);
ptr->left=NULL; return 0;
ptr->right=NULL; }
if(tree==NULL)
{ tree=ptr;
tree->left=NULL;
tree->right=NULL;
}
else
{parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{parentptr=nodeptr;
if(val<nodeptr->data)
nodeptr=nodeptr->left;
else
nodeptr=nodeptr->right;
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 9
Theory:
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if
they are in the wrong order. Bubble sort, sometimes referred to as sinking sort, is a simple sorting
algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and
swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed,
which indicates that the list is sorted.
Algorithm:
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Program:
#include <stdio.h> {
#include if (array[i] > array[j])
<conio.h> int {temp
main() = array[i];
{ array[i] = array[j];
int array[100], n, i, j, temp; array[j] = temp;
clrscr(); }
printf("Enter number of elements\n"); }
scanf("%d", &n); }
printf("Enter %d integers\n", n); printf("Sorted list:\n");
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
scanf("%d", printf("%d\t", array[i]);
&array[i]);for (i = 0; i < getch();
n; i++) return 0;
{ }
for (j = 0 ; j < n - 1; j++)
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 10
Theory:
QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array
around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array
as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x,
and put all greater elements (greater than x) after x. All this should be done in linear time.
Algorithm:
Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, PIVOT = BEG, FLAG =0
Step 2: Repeat Steps 3 to 6 while FLAG =1
Step 3: Repeat while ARR[PIVOT] <= ARR[RIGHT] AND PIVOT !=
RIGHTSET RIGHT = RIGHT - 1
[END OF LOOP]
Step 4: IF PIVOT =
RIGHTSET FLAG = 1
ELSE IF ARR[PIVOT] >
ARR[RIGHT] SWAP ARR[PIVOT]
with ARR[RIGHT]SET PIVOT =
RIGHT
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
[END OF IF]
Step 5: IF FLAG = 0
Repeat while ARR[PIVOT] >= ARR[LEFT] AND PIVOT !=
LEFTSET LEFT = LEFT + 1
[END OF LOOP]
Step 6: IF PIVOT =
LEFTSET FLAG = 1
ELSE IF ARR[PIVOT] <
ARR[LEFT] SWAP ARR[PIVOT]
with ARR[LEFT]
SET PIVOT =
LEFT[END OF
IF] [END OF IF]
Step 7: [END OF LOOP]
Step 8: END
QUICK_SORT (ARR, BEG, END)
Program:
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 11
Theory:
Binary search works on a principle of searching an element from sorted array by repeatedly dividing the
search interval in half. Begin with an interval covering the whole array. If the value of the search key is
less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it
to the upper half. Repeatedly check until the value is found or the interval is empty.
Algorithm:
Step 6: EXIT
Program:
#include<stdio.h> if(a[mid]==val)
#include<conio.h {
>int main() pos=mid+1;
{ printf("\nElement found at Position %d", pos);
int i,beg,end,mid,pos=-1,a[100],n,val; break;
clrscr(); }
printf("Enter number of elements "); else if(a[mid]>val)
scanf("%d",&n); end=mid-1;
printf("\nEnter sorted array"); else
for(i=0;i<n;i++) beg=mid+1;
scanf("%d",&a[i]); }
printf("\nEnter number to be searched"); if(pos==-1)
scanf("%d",&val); {
beg=0; printf("\n%d does not exist in
end=n-1; array",val);
while(beg<=end) }
{ getch();
mid=(beg+end)/2; return 0;
}
Outcome:
!!Sab Ka Malik Atma!!
Vishwatmak Jangli Maharaj Ashram Trust
Atma Malik Institute of Technology and Research
At: Mohili, Post: Aghai, Via Kalyan, Dist.: Thane, 421301
Experiment No 12
Aim: Write a program to Implement Depth First Search and Breadth First Search.
Theory:
BFS:
Breadth First Traversal (or Search) for a graph is similar to Breadth First Traversal of a tree. The only catch
here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid
processing a node more than once, we use a boolean visited array. For simplicity, it is assumed that all
vertices are reachable from the starting vertex.
DFS:
Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch
here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid
processing a node more than once, we use a boolean visited array.
Algorithm:
Step 6: EXIT
Program:
Outcome: