You are on page 1of 73

FORM NO.

- F/ TL / 021

Rev.00 Date 20.03.2020

RECORD NOTEBOOK

BCS18L01 –DATA STRUCTURES LAB

2022–2023 (ODD SEMESTER)


DEPARTMENT
Of
COMPUTER SCIENCE AND ENGINEERING

NAME : B.VENKATA KUSHAL -


211061101070
REGISTER NO : 211061101070
COURSE : B.TECH (CSE)
YEAR/SEM/SEC : II / III/ B
FORM NO.- F/ TL / 021
Rev.00 Date 20.03.2020

BONAFIDE CERTIFICATE
Register No: 211061101070
Name of Lab: DATA STRUCTURES LAB (BCS18L01)
Department: COMPUTER SCIENCE AND ENGINEERING

Certified that this is the bonafide record of work done by


____________________ II Year B. Tech (CSE), Sec-‘B’
in the DATA STRUCTURES LAB during the year 2022-2023

Signature of Lab-in-Charge Signature of Head of Dept

Submitted for the Practical Examination held on --------------------------

Internal Examiner External Examiner


INDEX

Exp Staff
No Date Name Of Experiment Page No
Signature
Operation on Array
4-7
1
Singly Linked List Operation
8-12
2
Implementation of Stack using Arrays &
13-20
3 Linked Lists.

4 Infix to Postfix Conversion Using Stack 21-24

5 Evaluation to Postfix Expression. 25-28

Queue- operation using array and linked


6 lists. 29-32

7 Dequeue , circular-operations 33-42

Binary tree traversals- In order, pre order,


post order using recursion 43-45
8

Binary tree traversals- In order, pre order,


9 post order using non recursion 46-50

10 Linear and binary search 51-54

Sorting -Selection Sort, Quick sort, Heap sort


11 and Merge Sort 55-60

Addition, multiplication of sparse matrices


12 61-63

Polynomial addition and multiplication


13 64-67

14 Depth first search of a graph 68-70

15 Breadth first search of a graph 71-73


Exp No: 1
Date:

OPERATION OF AN ARRAY

AIM

To write a C++ program to implement insertion and deletion of an array.

ALGORITHM

1. Start the program.

2. Declare the size of the Array.

3. Initialise the member function and variable create a class which consists of insert, delete,
display and update.

4. Check the condition and select the choice.

i Insertion in the Array.

a) If the size is not full, then insert an element in to the array.

ii Deletion from the Array.

a) If the array is not empty, enter the index of the element to delete.

iii Display

a) Check the size of the array.

b) If the size of the array is equal to the number of element inserted then display
the values.

iv Update

a) If the array is not empty, enter the index of the element to update.

v Quit

5. End the program.

4
PROGRAM
#include<iostream.h>
#include<conio.h>
int arr[10],limit;
void disp(){
for (int i=0;i<limit;i++){
cout<<arr[i]<<" ";
}
}
void ins(){
int x,q;
cout<<"Enter the position to be inserted: ";
cin>>x;
if(x>=0 && x<=limit){
cout<<"Enter the values to be inserted: ";
cin>>q;
for(int i=x; i<limit; i++){
arr[i]=q;
break;
}
cout<<"Value Inserted...\n";
}
else{
cout<<"Invalid...\n";
}
}
void del(){
int x;
cout<<"Enter the position of the value to be deleted: ";
cin>>x;
for(int i=x;i<limit;i++)
{
arr[x]=0;
}
cout<<"Element deleted...\n";
}
void update(){
int y,z;
cout<<"Enter position to be updated: ";
cin>>y;
cout<<"Enter a new value: ";
cin>>z;
for(int i=0;i<limit;i++)
{
if(i==y){
arr[i]=z;
}

5
}
cout<<"Element Updated...\n";
}
int main(){
clrscr();
int ch;
cout<<"B.VENKATA KUSHAL - 211061101070\n\n"<<endl;

cout<<"Enter array limit: ";


cin>>limit;
cout<<"Enter the values: ";
for(int i=0; i<limit; i++)
cin>>arr[i];
cout<<"\n---ARRAY OPERATIONS---\n\n";
cout<<"1. Insert\n";
cout<<"2. Delete\n";
cout<<"3. Display\n";
cout<<"4. Update\n";
cout<<"5. Quit\n";
do{
cout<<"\nEnter the choice: ";
cin>>ch;
switch(ch){
case 1: ins();
break;
case 2: del();
break;
case 3: disp();
cout<<endl;
break;
case 4: update();
break;
case 5: cout<<"End of program"<<endl;
break;
default: cout<<"Invalid choice..."<<endl;
}
}
while(ch!=5);
getch();
return 0;
}

6
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

7
Exp No: 2
Date: 16/08/2022

SINGLY LINKED LIST

AIM

To write a C++ program to implement singly linked list.

ALGORITHM
1. Start the program.

2. If there is only one node in singly linked list then the deletion takes place.

3. If there is no element in the list, then the new node is added by linked head pointer to the
node.

4. Check the condition whether the list is empty or not.

5. In case of delete operation, check for the list is empty & then print the delete element and
return.

6. End of the program.

8
PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
struct node{
int value;
struct node *next;
};
void insert();
void display();
void delete_node();
int count();
typedef struct node DATA_NODE;
DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
int data;
int main(){
int option = 0;
clrscr();
cout<<" B.VENKATA KUSHAL - 211061101070\n"<<endl;
cout << "Singly Linked List Operations";
cout << "\n\n---Options---\n\n";
cout << "1. Insert\n";
cout << "2. Delete\n";
cout << "3. Display\n";
cout << "4. Exit\n";
do {
cout << "\nEnter your option: ";
cin>>option;
switch (option){
case 1: insert();
break;
case 2: delete_node();
break;
case 3: display();
break;
case 4: cout<<"END of Program";
getch();
break;
default:
cout<<"Invalid Choice"<<endl;
break;
getch();
}
}
while(option!=4);
return 0;
}

9
void insert(){
cout << "Enter Element for Insert Linked List : ";
cin>>data;
temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
temp_node->value = data;
if (first_node == 0){
first_node = temp_node;
}
else{
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete_node() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
cout << "Enter Position for Delete Element : ";
cin>>pos;
if (pos > 0 && pos <= countvalue){
if (pos == 1){
temp_node = temp_node -> next;
first_node = temp_node;
cout <<"Deleted Successfully...\n";
}
else{
while (temp_node != 0) {
if (i == (pos - 1)) {
prev_node->next = temp_node->next;
if (i == (countvalue - 1)) {
head_node = prev_node;
}
cout <<"Deleted Successfully...\n";
break;
}
else{
i++;
prev_node = temp_node;
temp_node = temp_node -> next;
}
}
}
}
else
cout << "Invalid Position\n";
}
void display(){
int count = 0;

10
temp_node = first_node;
cout << "Display Linked List : ";
while (temp_node != 0){
cout << temp_node->value<<" ";
count++;
temp_node = temp_node -> next;
}
cout<<endl;
}
int count(){
int count = 0;
temp_node = first_node;
while (temp_node != 0){
count++;
temp_node = temp_node -> next;
}
cout << "No Of Items In Linked List : " << count<<endl;
return count;
}

11
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

12
Exp No: 3a
Date: 03/09/2022

IMPLEMENTATION OF STACK USING ARRAY

AIM

To write a C++ program to implement push and pop operation in stack using array.

ALGORITHM
1. Start the program.

2. Create a menu which consists of operations Push, Pop, Display & Exit.

3. Push operation

i If choice is push, insert the item in the stack.

ii Else stack is full.

4. Pop operation

i If choice is pop, delete the item from the stack.

ii Else stack is empty.

5. Display the item of the stack.

6. Exit from the Menu

7. End the program.

13
PROGRAM
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int stack[20], n=20, 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){
cout<<"Stack element are: ";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
}
else
cout<<"Stack is empty"<<endl;
}
int main(){
int ch,val;
cout<<" B.VENKATA KUSHAL - 211061101070\n\n";
cout<<"---OPTIONS---\n\n";
cout<<"1. Push in stack"<<endl;
cout<<"2. Pop from stack"<<endl;
cout<<"3. Display stack"<<endl;
cout<<"4. Exit"<<endl;
do{
cout<<"\nEnter choice: ";
cin>>ch;
switch(ch){
case 1:
cout<<"Enter the value to be pushed: ";
cin>>val;
push(val);
break;
case 2:

14
pop();
break;
case 3:
display();
break;
case 4:
cout<<"Exit\n";
break;
default:
cout<<"Invalid choice"<<endl;
}
}
while(ch!=4);
getch();
return 0;
}

15
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

16
Exp No: 3b
Date: 03/09/2022

IMPLEMENTATION OF STACK USING LINKED LIST

AIM

To write a C++ program to implement push and pop operation in stack using linked list.

ALGORITHM
1. Start the program.

2. If there is no element in the list, then the new node is added and the node becomes head as
well as top node.

3. The stack is grow-able by attaching more nodes, the newly created node becomes the top
node and the link of top becomes head node.

4. The above step is repeated by making new node as top and link is attached to previously
created node.

5. In case of delete operation, check for the list is empty & then print the delete element &
return.

6. End the program.

17
PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
typedef struct node data_node;
data_node *top=NULL;
void push(int val){
data_node *newnode=(data_node*) malloc(sizeof(data_node));
newnode->data=val;
newnode->next=top;
top=newnode;
}
void pop(){
if(top==NULL)
cout<<"Stack underflow"<<endl;
else
cout<<"popped element is: "<<top->data<<endl;
top=top->next;
}
void disp(){
data_node *ptr;
if(top==NULL){
cout<<"Stack is empty";
}
else{
ptr=top;
cout<<"Stack element are: ";
while(ptr!=NULL){
cout<<ptr->data;
ptr=ptr->next;
}
}
cout<<endl;
}
int main(){
clrscr();
int ch, val;
cout<<" B.VENKATA KUSHAL - 211061101070\n\n";
cout<<"---OPTIONS---\n\n";
cout<<"1. Push\n";

18
cout<<"2. Pop\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
do{
cout<<"\nEnter choice: ";
cin>>ch;
switch(ch){
case 1: cout<<"Enter the value to be pushed: ";
cin>>val;
push(val);
break;
case 2: pop();
break;
case 3: disp();
break;
case 4: cout<<"Exit\n";
getch();
break;
}
}
while(ch!=4);
return 0;
}

19
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

20
Exp No: 4
Date: 03/09/2022

INFIX TO POSTFIX CONVERSION

AIM

To write a C++ program to implement infix to postfix conversion using stack.

ALGORITHM
1. Start the program.

2. Initialise the empty stack.

3. Scan the operator from left to right in the infix expression.

i. If the leftmost character is an Operand, set it as the current output to the Postfix
string.

ii. If the scanned character is the operator and the Stack is empty or contains the '(' , ')'
symbol, push the operator into the Stack.

iii. If the scanned operator has higher precedence than the existing precedence operator
in the Stack or if the Stack is empty, put it on the Stack.

iv. If the scanned operator has lower precedence than the existing operator in the Stack,
pop all the Stack operators. After that, push the scanned operator into the Stack.

v. If the scanned character is a left bracket '(', push it into the Stack.

vi. If we encountered right bracket ')', pop the Stack and print all output string character
until '(' is encountered and discard both the bracket.

4. Repeat this step as long as stack is not empty.

5. Print the Stack output.

6. Pop and output all characters, including the operator, from the Stack until it is not empty.

7. End the program.

21
PROGRAM
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
char inf[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
int main()
{
cout<<" B.VENKATA KUSHAL - 211061101070\n";
cout<<"\nEnter the infix Expression:";
cin>>inf;
postfix();
return 0;
}
void postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+':
while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':
while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case '/':
while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case '^':
while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case '(':
push(0);

22
break;
case ')':
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
cout<<"Postfix Expression is : "<<post;
getch();
}
void push(int ele)
{
top++;
st[top]=ele;
}
char pop()
{
int el;
char e;
el=st[top];
top--;
switch(el)
{
case 1:
e='+';
break;
case 2:
e='-';
break;
case 3:
e='*';
break;
case 4:
e='/';
break;
case 5:
e='^';
break;
}
return(e);
}

23
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

24
Exp No: 5
Date: 03/09/2022

EVALUATION POSTFIX EXPRESSION

AIM

To write a C++ program to evaluate a postfix expression using stack.

ALGORITHM
1. Start the program.

2. If a character is an operand push it to Stack.

3. If the character is an operator. Pop two elements from the Stack.

a) Operate on these elements according to the operator, and push the result back to
the Stack.

4. Repeat the process until the end has reached.

5. The result is stored at the top of the Stack, return it.

6. End of program.

25
PROGRAM
#include <iostream.h>
#include <ctype.h>
#include<conio.h>
#define MAXSTACK 100
#define POSTFIXSIZE 100
int stack[MAXSTACK];
int top=-1;
void push(int item)
{
if(top>=MAXSTACK-1){
cout<<"stack over flow";
return;
}
else{
top=top+1;
stack[top]=item;
}
}
int pop()
{
int item;
if (top<0)
{
cout<<"stack underflow";
}
else
{
item=stack[top];
top=top-1;
return item;
}
return(item);
}
void EvalPostfix(char postfix[])
{
int i;
char ch;
int val;
int A,B;
for (i=0; postfix[i]!=')'; i++){
ch=postfix[i];
if(isdigit(ch))
{
push(ch-'0');
}
else if (ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
A=pop();
B=pop();
switch(ch)

26
{
case '*':
val=B*A;
break;
case '/':
val=B/A;
break;
case '+':
val=B+A;
break;
case '-':
val=B-A;
break;
}
push(val);
}
}
cout<<"\nResult of expression evaluation : "<<pop();
}
int main()
{
int i;
clrscr();
char postfix[POSTFIXSIZE];
cout<<" B.VENKATA KUSHAL - 211061101070\n";
cout<<"\nEnter postfix expression,\npress right parenthesis ')' for end...\n\n Expression : ";\
for (i= 0; i<=POSTFIXSIZE-1; i++) {
cin>>postfix[i];
if (postfix[i]==')')
{
break;
}
}
EvalPostfix(postfix);
getch();
return(0);
}

27
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

28
Exp No: 6
Date: 03/09/2022

QUEUE-OPERATIOS USING ARRAY

AIM

To write a C++ program to implement queue using array.

ALGORITHM
1. Start the program.

2. Declare and initialise the member function and variable create a class which consists of add,
delete, display and all private variables.

3. Check the condition and select the choice.

i. ADD QUEUE

a) Check whether the queue is full.

b) If queue is full display queue is full otherwise increment rear.

ii. DELETE QUEUE

a) Check whether the queue is empty.

b) If queue is empty, return NULL otherwise make front is -1 or rear is -1 else


increment front.

iii. DISPLAY

a) Check if front is equal to -1, print element is empty or else display the content of
queue.

4. End of the program.

29
PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 4
class queue{
private:
int que[size], front, rear;
public:
queue(){
front=-1;
rear=-1;
}
void enqueue();
void dequeue();
void display();
};
void queue::enqueue(){
if(rear==size-1){
cout<<"Queue is Full - Overflow\n";
return;
}
rear++;
cin>>que[rear];
if(front==-1)
front++;
}
void queue::dequeue(){
if (front==-1){
cout<<"Queue is Empty - Underflow\n";
return;
}
else
cout<<"The Deleted value is: "<<que[front]<<endl;
if(front==rear)
front=rear=-1;
else
front++;
}
void queue::display(){
if(front==-1){
cout<<"Queue is Empty\n";
return;
}
for(int i=front; i<=rear; i++)
cout<<que[i]<<" \t";
cout<<endl;
}

30
int main(){
clrscr();
queue q;
int ch;
cout<<" B.VENKATA KUSHAL - 211061101070\n\n";
cout<<"---OPTIONS---\n\n";
cout<<"1. Insert\n";
cout<<"2. Delete\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";

do{
cout<<"\nEnter the choice: ";
cin>>ch;
switch(ch){
case 1: cout<<"Enter the value: ";
q.enqueue();
break;
case 2: q.dequeue();
break;
case 3: q.display();
break;
case 4: cout<<"END";
break;
default: cout<<"Invalid Choice\n";
break;
}
}
while(ch!=4);
getch();
return 0;
}

31
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

32
Exp No: 7
Date: 03/09/2022

DEQUEUE, CIRCULAR-OPERATIOS

AIM

To write a C++ program to implement queue using array.

ALGORITHM
1. Start the Program

2. Declare a class contain a status to decides node

3. Promote user for choice

i Inqueue
ii Delqueue
iii Display

4. Inqueue

a) Allocate a new node


b) Put value in the field
c) Mark it rear
d) Return

5. Then do step-3 for delqueue

6. Exit the program

33
PROGRAM
#include<iostream.h>
#include<conio.h>
#include<limits.h>
#define MAX 50
class deq{
int arr[MAX];
int front, rear, size;
public:
deq(int size){
front= -1;
rear=0;
this –>size=size;
}
void insertFront(int key);
void insertRear(int key);
void deleteFront();
void deleteRear();
int isFull();
int isEmpty();
int getFront();
int getRear();
void display();
};
int deq::isFull(){
return((front = = 0 && rear = = size-1) || front= = rear + 1);
}
int deq::isEmpty(){
if(front = = -1)
{
return 1;
}
return 0;
}
void deq::insertFront(int key)
{
if(isFull())
{
cout<<"Overflow..."<<endl;
return;
}
if(front = = -1)
{
front=0;
rear=0;
}
else if(front = = 0)
{
front=size-1;
}

34
else{
front=front-1;}
arr[front]=key;
cout<<arr[front]<<" inserted at front of dequeue"<<endl;
}
void deq::insertRear(int key){
if(isFull())
{
cout<<"Overflow...\n";
return;
}
if (front = = -1)
{
front=0;
rear=0;
}
else if(rear = = size-1)
{
rear=0;
}
else{
rear = rear+1;
}
arr[rear]=key;
cout<<arr[rear]<<" inserted at rear of dequeue"<<endl;
}
void deq::deleteFront(){
if(isEmpty())
{
cout<<"Queue Underflow..."<<endl;
return;
}
cout<<getFront()<<" removed from front of dequeue"<<endl;
if(front = = rear)
{
front=-1;
rear=-1;
}
else if (front = = size-1)
{
front=0;
}
else{
front=front+1;
}
}
void deq::deleteRear(){
if(isEmpty()){
cout<<"Queue Underflow..."<<endl;
return;
}

35
cout<<getRear()<<" removed from rear of dequeue"<<endl;
if(front = = rear){
front=-1;
rear=-1;
}
else if(rear = = 0){
rear=size-1;
}
else{
rear=rear-1;
}
}
int deq::getFront(){
if(isEmpty())
{
cout<<"Underflow...\n"<<endl;
return-1;
}
return arr[front];
}
int deq::getRear(){
if(isEmpty()||rear<0)
{
cout<<"Underflow...\n"<<endl;
return -1;
}
return arr[rear];
}
void deq::display(){
if(front = = -1)
{
cout<<"Dequeue is Empty...\n";
return;
}
cout<<"Elements in Dequeue are: ";
if(rear>=front)
{
for(int i=front; i<=rear; i++)
{
cout<<arr[i]<<" ";
}
}
else{
for(int i=front; i<size; i++)
{
cout<<arr[i]<<" ";
}
}
cout<<endl;
}

struct Queue{

36
int rear, front;
int size;
int *arr;
Queue(int s){
front=rear=-1;
size=s;
arr=new int[s];
}
void enq(int value);
int dequ();
void displayQ();
};
void Queue::enq(int value){
if((front = = 0 && rear = = size-1) || (rear = = (front=1)%(size-1))){
cout<<"Queue is Full...\n";
return;
}
else if(front = = -1)
{
front = rear = 0;
arr[rear]=value;
}
else if(rear = = size-1 && front!=0)
{
rear=0;
arr[rear]=value;
}
else{
rear++;
arr[rear]=value;
}
cout<<arr[rear]<<" inserted into circular queue"<<endl;
}
int Queue::dequ(){
if(front = = -1)
{
cout<<"Queue is empty...\n";
return INT_MIN;
}
cout<<arr[front]<<" removed from circular queue"<<endl;
int data=arr[front];
arr[front]=-1;
if(front = = rear){
front = -1;
rear = -1;
}
else if(front = = size-1)
{
front=0;
}
else{
front++;

37
}
return data;
}
void Queue::displayQ(){
if (front = = -1){
cout<<"\nQueue is Empty...\n";
return;
}
cout<<"Elements in circular queue are: ";
if(rear>=front)
{
for (int i=front; i<=rear; i++)
{
cout<<arr[i]<<" " ;
}
}
else{
for(int i=front; i<size; i++)
{
cout<<arr[i]<<" ";
}
for(int j=0; j<=rear; j++)
{
cout<<arr[j]<<" ";
}
}
cout<<endl;
}
int main(){
clrscr();
cout<<" B.VENKATA KUSHAL - 211061101070\n";
deq dq(10);
Queue cq(10);
int ch;
do{
cout<<"\n---Option---\n"<<endl;
cout<<"1. Dequeue"<<endl;
cout<<"2. Circular Queue"<<endl;
cout<<"3. Exit"<<endl;
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch){
case 1: {
int c1;
cout<<"\n---Dequeue---\n";
cout<<"\n1. Insertion at Front of Dequeue";
cout<<"\n2. Insertion at Rear of Dequeue";
cout<<"\n3. Deletion at Front of Dequeue";
cout<<"\n4. Deletion at Rear of Dequeue";
cout<<"\n5. Display Dequeue";
cout<<"\n6. Quit"<<endl;

38
do{
cout<<"\nEnter your Choice: ";
cin>>c1;
switch(c1){
case 1:
{
int d1;
cout<<"Enter an element to insert: ";
cin>>d1;
dq.insertFront(d1);
break;
}
case 2:
{
int d2;
cout<<"Enter an element to insert: ";
cin>>d2;
dq.insertRear(d2);
break;
}
case 3:
{
dq.deleteFront();
break;
}
case 4:
{
dq.deleteRear();
break;
}
case 5:
{
dq.display();
break;
}
case 6:
{
cout<<"End of program on Dequeue Operation"<<endl;
break;
}
default:
cout<<"Invalid choice..."<<endl;
}
}
while(c1!=6);
break;
}
case 2:
{
int c2;
cout<<"\n---Circular Queue---\n";
cout<<"\n1. Enqueue on Circular queue";
cout<<"\n2. Dequeue on Circular queue";

39
cout<<"\n3. Display Circular queue";
cout<<"\n4. Quit"<<endl;
do{
cout<<"\nEnter a choice: ";
cin>>c2;
switch(c2){
case 1:
{
int eq;
cout<<"Enter an element to insert: ";
cin>>eq;
cq.enq(eq);
break;
}
case 2: cq.dequ();
break;
case 3: cq.displayQ();
break;
case 4: cout<<"End of program on Circular Queue"<<endl;
break;
default: cout<<"Invalid choice..."<<endl;
}
}
while(c2!=4);
break;
}
case 3:
{
cout<<"End of program !"<<endl;
break;
}
default: cout<<"Invalid Option"<<endl;
}
}
while(ch!=3);
getch();
return 0;
}

40
OUTPUT

41
RESULT
Thus the program is executed and the output is verified successfully.

42
Exp No: 8
Date: 03/09/2022

BINARY TREE TRAVERSAL – INORDER, PREORDER & POSTORDER


USING RECURSION

AIM

To write a C++ program to implement binary traversals using recursion.

ALGORITHM
1. Start the Program.

2. Enter all nodes into stack.

3. Input the element in the array.

4. For pre-order traversal process from root, left & right sub-tree.

5. For post-order traversal process from left, right & root.

6. For in-order traversal process from left, root & right.

7. Display all the traversal.

8. End the program.

43
PROGRAM
#include<iostream.h>
#include<conio.h>
struct Node{
int data;
struct Node *left, *right;
Node(int data){
this->data=data;
left=right=NULL;
}
};
void inOrder(struct Node *node){
if(node==NULL)
return;
inOrder(node->left);
cout<<node->data<<" ";
inOrder(node->right);
}
void preOrder(struct Node *node){
if(node==NULL)
return;
cout<<node->data<<" ";
preOrder(node->left);
preOrder(node->right);
}
void postOrder(struct Node *node){
if(node==NULL)
return;
postOrder(node->left);
postOrder(node->right);
cout<<node->data<<" ";
}
int main(){
clrscr();
cout<<"B.VENKATA KUSHAL - 211061101070\n\n";
struct Node
*root=new Node(1);
root->left=new Node(2);
root->right=new Node(3);
root->left->left=new Node(4);
root->left->right=new Node(5);
cout<<"\nPre-order traversal to binary tree is ";
preOrder(root);
cout<<"\nIn-order traversal to binary tree is ";
inOrder(root);
cout<<"\nPost-order traversal to binary tree is ";
postOrder(root);
getch();
return 0;
}

44
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

45
Exp No: 9
Date: 03/09/2022

BINARY TREE TRAVERSAL – INORDER, PREORDER & POSTORDER


USING NON-RECURSION

AIM

To write a C++ program to implement binary traversals using recursion.

ALGORITHM
1. Start the Program.

2. Enter all nodes into stack.

3. Input the element in the array.

4. For pre-order traversal process from root, left & right sub-tree.

5. For post-order traversal process from left, right & root.

6. For in-order traversal process from left, root & right.

7. Display all the traversal.

8. End the program.

46
PROGRAM
#include<iostream.h>
#include<conio.h>
struct tNode{
int data;
struct tNode *left,*right;
int visited;
};
void inorder(struct tNode *root){
struct tNode *current,*pre;
if(root==NULL)
return;
current=root;
while(current!=NULL){
if(current->left==NULL){
cout<<current->data<<" ";
current=current->right;
}
else{
pre=current->left;
while(pre->right!=NULL && pre->right!=current)
pre=pre->right;
if(pre->right==NULL){
pre->right=current;
current=current->left;
}
else{
pre->right=NULL;
cout<<current->data<<" ";
current=current->right;
}
}
}
}
void preorder(struct tNode *root){
while(root){
if(root->left==NULL){
cout<<root->data<<" ";
root=root->right;
}
else{
struct tNode *current=root->left;
while(current->right && current->right!=root)
current=current->right;
if(current->right==root)

47
{
current->right=NULL;
root=root->right;
}
else{
cout<<root->data<<" ";
current->right=root;
root=root->left;
}
}
}
}
void postorder(tNode *root)
{
if(root==NULL)
return;
struct tNode *current=new tNode();
tNode *pre=NULL;
tNode *prev=NULL;
tNode *succ=NULL;
tNode *temp=NULL;
current->left=root;
while(current){
if(current->left==NULL){
current=current->right;
}
else{
pre=current->left;
while(pre->right && pre->right!=current)
pre=pre->right;
if(pre->right==NULL)
{
pre->right=current;
current=current->left;
}
else{
pre->right=NULL;
succ=current;
current=current->left;
prev=NULL;
while(current!=NULL){
temp=current->right;
current->right=prev;
prev=current;
current=temp;
}
while(prev!=NULL){
cout<<prev->data<<" ";
temp=prev->right;
prev->right=current;

48
current=prev;
prev=temp;
}
current=succ;
current=current->right;
}
}
}
}
struct tNode*newtNode(int data){
struct tNode *node=new tNode;
node->data=data;
node->left=NULL;
node->right=NULL;
return(node);
}
int main(){
clrscr();
cout<<"B.VENKATA KUSHAL - 211061101070\n\n";
struct tNode *root=newtNode(1);
root=newtNode(1);
root->left=newtNode(2);
root->right=newtNode(3);
root->left->left=newtNode(4);
root->left->right=newtNode(5);
root->right->left=newtNode(6);
root->right->right=newtNode(7);
root->left->left->left=newtNode(8);
root->left->left->right=newtNode(9);
root->left->right->left=newtNode(10);
root->left->right->right=newtNode(11);
cout<<"Inorder: ";
inorder(root);
cout<<endl;
cout<<"Preorder: ";
preorder(root);
cout<<endl;
cout<<"Postorder: ";
postorder(root);
cout<<endl;
getch();
return 0;
}

49
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

50
Exp No: 10
Date: 03/09/2022

LINEAR & BINARY SEARCH

AIM

To write a C++ program to implement linear and binary search.

ALGORITHM
1. Start the Program.

2.
i Linear Search

a. Start from the leftmost element of arr[] and one by one compare x with each element
of arr[].

b. If x matches with an element, return the index.

c. If x doesn't match with any of elements, return -1

ii Binary Search

a. Compare x with the middle element.

b. If x matches with middle element, we return the mid index.

c. Else If x greater than the mid element, then x can only lie in right half sub-array after
the mid element So we recur for right half.

d. Else (x is smaller) recur for the left half.

3. Prompt the user to select between linear and binary search.

4. End the program.

51
PROGRAM
#include<iostream.h>
#include<conio.h>
#define MAX 10
int Is(int arr[], int n, int num)
{
for(int i=0; i<n;i++){
if(arr[i]==num){
return i;
}
}
return -1;
}
int bs(int arr[], int l, int r, int x)
{
if(r>=1){
int mid=1+(r-1)/2;
if(arr[mid]==x)
return mid;
if(arr[mid]>x)
return bs(arr, l, mid-1, x);
return bs(arr, r, mid+1, x);
}
return -1;
}
int main(){
int arr[MAX];
clrscr();
cout<<"B.VENKATA KUSHAL - 211061101070\n\n";
cout<<"Enter the size of the array: ";
int n, ch, x, result;
cin>>n;
cout<<"Enter the elements: ";
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"\n---MENU---\n";
cout<<"\n1. Linear Search"<<endl;
cout<<"2. Binary Search"<<endl;
cout<<"3. Exit";
do{
cout<<"\n\nEnter your choice: ";
cin>>ch;

52
switch(ch){
case 1:
cout<<"\n---Linear Search---\n"<<endl;
cout<<"Enter element to search: ";
cin>>x;
result=Is(arr, n, x);
(result==-1)?
cout<<"Not present\n":
cout<<"Present at index "<<result;
break;
case 2:
cout<<"\n---Binary Search---\n"<<endl;
cout<<"Enter element to search: ";
cin>>x;
result=bs(arr, 0, n-1, x);
(result==-1)?
cout<<"Not present":
cout<<"Present at index "<<result;
break;
case 3:
cout<<"END";
break;
default: cout<<"Invalid choice";
}
}
while(ch!=3);
getch();
return 0;
}

53
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

54
Exp No: 11
Date: 03/09/2022

SELECTION SORT, QUICK SORT, HEAP SORT, MERGE SORT

AIM

To write a C++ program on sorting types – Selection sort, Quick sort, Heap sort & Merge sort.

ALGORITHM
1. Start the Program.

2.
i Selection Sort

In every iteration of selection sort, the minimum element considering ascending order)
from the unsorted sub-array is picked and moved to the sorted sub-array. Repeatedly
finding the minimum element (considering ascending order) from unsorted part and
putting it at the beginning

ii Quick Sort
Select an element x of array as pivot, put x at its correct position in sorted array and
put all smaller elements smaller than before x, and put all greater elements (greater
than x) after x.
iii Heap Sort
Using binary heap ie, parent node is stored at index I, the left child can be calculated by
2*I+1 and right child by 2*I+ 2 assuming the indexing starts at 0).The largest item is
stored at the root of the heap Replace it with the last item of the heap followed by
reducing the size of heap by 1
iv Merge Sort

Divide the input array into two halves, cal and then merges the two sorted halves

3. Take input of elements of array.

4. Prompt the user to perform any sorting.

5. Print the sorted array.

6. End the program.

55
PROGRAM
#include<iostream.h>
#include<conio.h>
#define MAX 10
void swap(int *x, int *y){
int temp=*x;
*x=*y;
*y=temp;
}
void ss(int arr[],int n){
int i, j, min_idx;
for(i=0; i<n-1; i++)
{
min_idx=i;
for(j=i+1; j<n; j++)
if(arr[j]<arr[min_idx])
min_idx=j;
swap(&arr[min_idx], &arr[i]);
}
}
int partition(int arr[], int low, int high){
int pivot=arr[high];
int i=(low-1);
for(int j=low; j<=high-1; j++)
{
if(arr[j]<pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return(i+1);
}
void qs(int arr[], int low, int high){
if(low<high)
{
int p=partition(arr, low, high);
qs(arr,low,p-1);
qs(arr,p+1,high);
}
}
void heap(int arr[],int n,int i){
int largest =i;

56
int l=2*i+1;
int r=2*i+2;
if(l<n && arr[l]>arr[largest])
largest=1;
if(r<n && arr[r]>arr[largest])
largest=r;
if(largest!=i){
swap(&arr[i], &arr[largest]);
heap(arr,n,largest);
}
}
void hs(int arr[],int n){
for(int i=n/2-1;i>=0;i--)
heap(arr,n,i);
for(int j=n-1;j>0;j--)
{
swap(&arr[0], &arr[j]);
heap(arr,j,0);
}
}
void merge(int arr[],int const l,int const m,int const r){
int const n1=m-l+1;
int const n2=r-m;
int L[10];
int R[10];
for(int i=0; i<n1; i++){
L[i]=arr[l+i];
}
for(int j=0; j<n2; j++){
R[j]=arr[m+1+j];
}
i=0;
j=0;
int k=l;
while(i<n1 && j<n2){
if(L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1){
arr[k]=L[i];
i++;
k++;

57
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}
}
void ms(int arr[],int const l,int const r){
if(l>=r){
return;
}
int m=l+(r-l)/2;
ms(arr,l,m);
ms(arr,m+1,r);
merge(arr,l,m,r);
}
void print(int arr[],int size){
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main(){
int arr[MAX];
clrscr();
int ch,n;
cout<<"B.VENKATA KUSHAL - 211061101070\n\n";
cout<<"\nEnter the size of the array: ";
cin>>n;
cout<<"Enter the elements: ";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<endl;
cout<<"---MENU---\n\n";
cout<<"1. Selection Sort"<<endl;
cout<<"2. Quick Sort"<<endl;
cout<<"3. Heap Sort"<<endl;
cout<<"4. Merge Sort"<<endl;
cout<<"5. Exit\n";
do{
cout<<"\nEnter choice: ";
cin>>ch;
switch(ch){
case 1:ss(arr,n);

58
print(arr,n);
break;
case 2: qs(arr,0,n-1);
print(arr,n);
break;
case 3: hs(arr,n);
print(arr,n);
break;
case 4: ms(arr,0,n-1);
print(arr,n);
break;
default: cout<<"END";
}
}
while(ch!=5);
getch();
return 0;
}

59
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

60
Exp No: 12
Date: 03/09/2022

ADDITION & MULTIPLICATION OF SPARSE MARTICES

AIM

To write a C++ program to implement addition and multiplication of Sparse Matrices.

ALGORITHM
1. Start the Program.

2. n = 1

3. ia = ib = ic = ja = jb = jc =1

4. repeat while (ia<=m)

5. End the Program

61
PROGRAM
#include<iostream.h>
#include<conio.h>
#define N 2
void multiply(int mat1[][N], int mat2[][N], int res[][N]){
int i, j, k;
for(i=0; i<N; i++){
for(j=0; j<N ;j++){
res[i][j]=0;
for(k=0; k<N; ++k)
res[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
void add(int mat1[][N], int mat2[][N], int re[][N]){
int i, j, k;
for(i=0; i<N; i++){
for(j=0; j<N; j++){
re[i][j]=mat1[i][j]+mat2[i][j];
}
}
}
int main(){
clrscr();
cout<<"B.VENKATA KUSHAL - 211061101070\n\n";
int i,j;
int res[N][N]={{0,0},{0,0}};
int re[N][N]={{0,0},{0,0}};
int mat1[N][N]={{1,2},{3,4}};
int mat2[N][N]={{4,3},{2,1}};
multiply(mat1, mat2, res);
add(mat1, mat2, re);
cout<<"Multiplication: Result matrix is: \n";
for(i=0; i<N; i++){
for(j=0;j<N;j++)
cout<<res[i][j]<<" ";
cout<<"\n";
}
cout<<"Addition: Result Matrix is: \n";
for(i=0; i<N; i++){
for(j=0;j<N;j++)
cout<<re[i][j]<<" ";
cout<<"\n";
}
getch();
return 0;
}

62
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

63
Exp No: 13
Date: 03/09/2022

POLYNOMIAL ADDITION & MULTIPLICATION

AIM

To write a C++ program to implement Polynomial addition and multiplication.

ALGORITHM
1. Start the Program.

2. Create a sum array sum [] of size equal to maximum of m and n.

3. Copy A[] to sum []

4. Traverse array B[] and do following for every element B[I] sum[I]=sum[I]+B[I]

5. Create a product array prod[] of size m+n-1.

6. Initialize all entries in prod[] as 0.

7. Traverse array A[] and do following for every element A[i]


Traverse arrayB[] and do following for every element B[j].
8. prod[i+j] = prod[i+j] + A[i] * B[j]

9. Return prod[].

10. End of program.

64
PROGRAM
#include<iostream.h>
#include<conio.h>
const int size=3;
int *add(int a[], int b[], int m, int n)
{
int *sum=new int[size];
for(int i=0;i<m;i++)
sum[i]=a[i];
for(i=0;i<n;i++)
sum[i]+=b[i];
return sum;
}
int *multiply(int a[], int b[], int m, int n)
{
int *prod=new int[m+n-1];
for (int i=0; i<m+n-1; i++)
prod[i]=0;
for (int j=0; j<n; j++)
prod[i+j] += a[i]*b[j];
return prod;
}
void print(int p[],int n)
{
for(int i=0;i<n;i++){
cout<<p[i];
if(i!=0)
cout<<"x^"<<i;
if(i!=n-1)
cout<<" + ";
}
}
int main(){
int a1, b1, c1, a2, b2, c2;
clrscr();
cout<<" B.VENKATA KUSHAL - 211061101070 \n\n";
cout<<"Enter the coefficient (3 values)...\n"<<endl;
cout<<"1st polynomial coefficient: ";
cin>>a1>>b1>>c1;
cout<<"\n2nd polynomial coefficient: ";
cin>>a2>>b2>>c2;
int a[]={a1,b1,c1};
int b[]={a2,b2,c2};
int m=sizeof(a)/sizeof(a[0]);

65
int n=sizeof(b)/sizeof(b[0]);
cout<<"\n1st Expression: ";
print(a,m);
cout<<"\n2nd Expression: ";
print(b,n);
int *sum=add(a,b,m,n);
cout<<"\n\nSum of the polynomial: ";
print(sum,size);
int *prod =multiply(a, b, m, n);
cout<<"\n\nProduct polynomial: ";
cout<<prod,m+n-1;
getch();
return 0;
}

66
OUTPUT:

RESULT
Thus the program is executed and the output is verified successfully.

67
Exp No: 14
Date: 03/09/2022

DEPTH FIRST SEARCH OF A GRAPH

AIM

To write a C++ program to implement Depth First Search of a Graph.

ALGORITHM
1. Start the Program.

2. Initialize an Empty stack

3. For Each vertex u, set visited[u]:=false;

4. Push s, v while (s not empty) do u:=pops;

5. If(not visited[u]) then visited[u] := true;

6. for each unvisited neighbour w of u Push s, w;


End If.
End While.

7. Stop

68
PROGRAM
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10], i, j, k, n, stk[10], top, v, visit[10], visited[10];
void main(){
clrscr();
int m;
cout<<" B.VENKATA KUSHAL - 211061101070\n\n";
cout<<"Enter no. of vertices: ";
cin>>n;
cout<<"Enter no. of edges: ";
cin>>m;
cout<<"\nEdges\n";
for(k=1;k<=m;k++)
{
cin>>i>>j;
cost[i][j]=1;
}
cout<<"\nEnter initial vertex: ";
cin>>v;
cout<<"\nOrder of visited vertices: "<<endl;
cout<<v<<" ";
visited[v]=1;
k=1;
while(k<n){
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v<<" ";
k++;
visit[v]=0;
getch();
visited[v]=1;
}
}

69
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

70
Exp No: 15
Date: 03/09/2022

BREADTH FIRST SEARCH OF A GRAPH

AIM

To write a C++ program to implement Breadth First Search of a Graph.

ALGORITHM
1. Start the Program.

2. Construct a graph with inputs from user.

3. Fix start node to traverse as s.

4. Visit s and find adjacent of s.

5. Visit the unvisited adjacent nodes and keep them in queue for traversing to their adjacent
nodes.

6. Take nodes from queue one by one and repeat step 4 & 5 until queue is empty.

7. Stop

71
PROGRAM
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
int cost[10][10],i,j,k,n,qu[10],fr,re,v,visit[10],visited[10];
main(){
int m;
clrscr();
cout<<"B.VENKATA KUSHAL - 211061101070 \n\n";
cout<<"Enter no. of vertices: ";
cin>>n;
cout<<"Enter no. of edges: ";
cin>>m;
cout<<"\nEdges\n";
for(k=1;k<=m;k++)
{
cin>>i>>j;
cost[i][j]=1;
}
cout<<"\nEnter initial vertex: ";
cin>>v;
cout<<"\nVisited vertex: ";
cout<<v<<"\t";
visited[v]=1;
k=1;
while(k<n){
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[re++]=j;
}
v=qu[fr++];
cout<<v<<" ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
return 0;
}

72
OUTPUT

RESULT
Thus the program is executed and the output is verified successfully.

73

You might also like