0% found this document useful (0 votes)
99 views

Data Structure Record

The document is a record notebook containing details of a student named Nishant Kumar enrolled in the B.Tech Computer Science course. It contains details like the name, register number, course, year, semester, and section of the student. It also contains a bonafide certificate signed by the head of the department certifying the student's lab work. The index section lists various experiments conducted in the data structures lab along with their page numbers and signatures.

Uploaded by

bala
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)
99 views

Data Structure Record

The document is a record notebook containing details of a student named Nishant Kumar enrolled in the B.Tech Computer Science course. It contains details like the name, register number, course, year, semester, and section of the student. It also contains a bonafide certificate signed by the head of the department certifying the student's lab work. The index section lists various experiments conducted in the data structures lab along with their page numbers and signatures.

Uploaded by

bala
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/ 94

RECORD NOTEBOOK

DATA STRUCTURE LAB (BCS18L01


(BCS18L01)

2022 – 2023 (ODD SAMESTER)

DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING

NAME : NISHANT KUMAR


REGISTER NO : 211061101310
COURSE : B.TECH ( CSE )

YEAR/SEM/SEC : II / III / F
BONAFIDE CERTIFICATE

Register number : 211061101310

Name of Lab : DATA STRUCTURE LAB (BCS18L02)

Department : COMPUTER SICENCE AND ENGINEERING

Certified that this is the BONAFIED record of work done by


NISHANT KUMAR of II Year B.Tech -‘F’’ Sec. (Computer Science and
Engineering), in the DATA STRUCTURE LAB during the year 2022-
2022
2023.

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

Submitted for the Practical Examinatio


Examination held on ---------------------------

Internal Examiner External Examiner


INDEX

EX.NO NAME OF THE EXPERIMENT PAGE.NO. SIGN.


1. OPERATION ON ARRAYS-INSERTION AND
DELETION
2. LINKED LISTS-CREATION,INSERTION,DELETION

SINGLE LINKED LIST

DOUBLE LINKED LIST

CIRCULAR LIST

3. STACK-OPERATION USING ARRAY AND LINKED


LISTS.
4. INFIX TO POSTFIX CONVERSION

5. EVALUATION TO POSTFIX EXPRESSION

6. QUEUE-OPERATION USING

ARRAY

LINKED LIST

7. DEQUEUE, CIRCULAR-OPERATION

8. BINARY TREE TRAVERSALS- INORDER,


PREORDER, POSTORDER USING RECURSION
9. BINARY TREE TRAVERSALS- INORDER,
PREORDER, POSTORDER USING NON-RECURSION
10. LINEAR AND BINARY SEARCH

11. SORTING-SELECTION SORT, QUICK SORT, HEAP


SORT AND MERGE SORT
12. ADDITION, MULTIPLICATION OF SPACE
MATRICES
13. POLYNOMIAL ADDITION AND MULTIPLICATION

14. DEPTH FIRST SEARCH OF A GRAPH

15. BREADTH FIRST SEARCH OF A GRAPH


EX.NO:- 1
OPERATION OF AN ARRAY

PROGRAM:-

#include <iostream.h>
//using namespace std;

#include<conio.h>
int arr[10];
void disp()
{
for (int i = 0; i < 10; i++)
cout << arr[i] << " ";
}
void ins()
{
cout << "Enter the values ";
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
cout << "Values inserted";
}
void del()
{
int x;
cout << "Enter the position of the value to be deleted ";
cin >> x;
for (int i = x; i < 10; i++)
{
arr[x] = -1;
}
1
cout << "Element deleted..";
}
void update()
{
int y, in;
cout << "Enter index and value of the element to be updated: ";
cin >> in >> y;
for (int i = 0; i < 10; i++)
{
if (i == in)
{
arr[i] = y;
}
}
cout << "Element Updated..";
}
int main()
{
int ch;
clrscr();
do
{
cout << "\nARRAY OPERATIONS: ";
cout << "1.Insert ";
cout << "2.Delete ";
cout << "3.Display ";
cout << "4.Update ";
cout << "5.Quit\n";
cout << "Enter the choice: ";
cin >> ch;
switch (ch)
{
case 1:

2
ins();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
update();
break;
case 5:
cout << "End of program";
break;
default:
cout << "Invalid choice" << endl;
}
} while (ch != 5);
getch();
return 0;
}

3
OUTPUT:-

4
EX.NO:- 2
LINKED LISTS – CREATION, INSERTION, DELETION OF SINGLE, DOUBLE AND
CIRCULAR LISTS
PROGRAM:-
(SINGLY LINKED LIST)

#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
// Singly Linked List
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 << "Singly Linked List Operations";


do
{
cout << "\nOptions:";
cout << "1:Insert ";
5
cout << "2:Delete ";
cout << "3:Display ";
cout << "4:Exit ";
cout << "Enter your option: ";
cin >> option;
switch (option)
{
case 1:
insert();
break;
case 2:
delete_node();
break;
case 3:
display();
break;
case 4:
cout << "End of Singly Linked List";
default:
break;
}
} while (option != 4);
return 0;
}
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;

6
}
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..";
}
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..";
break;

7
}
else{
i++;
prev_node = temp_node;
temp_node = temp_node->next;
}
}
}
}
else
cout << "\nInvalid Position \n\n";
}
void display(){
int count = 0;
temp_node = first_node;
cout << "\nDisplay Linked List : ";
while (temp_node != 0){
cout << temp_node->value << " ";
count++;
temp_node = temp_node->next;
}
}
int count(){
int count = 0;
temp_node = first_node;
while (temp_node != 0){
count++;
temp_node = temp_node->next;
}
cout << "\nNo Of Items In Linked List : " << count << endl;
return count;
}

8
OUTPUT:-

9
(DOUBLY LINKED LIST)
PROGRAM:-
#include <iostream.h>
#include<conio.h>
// A doubly linked list node
struct Node {
int data;
struct Node *next;
struct Node *prev;
}
;
// inserts node at the front of the list
void insert_front(struct Node **head, int new_data)
{
// allocate memory for New node
struct Node* newNode = new Node;
// assign data to new node
newNode->data = new_data;
// new node is head and previous is null, since we are adding at the front
newNode->next = (*head);
newNode->prev = NULL;
// previous of head is new node
if((*head) != NULL)(*head)->prev = newNode;
// head points to new node
(*head) = newNode;
}
/* Given a node as prev_node, insert a new node after the given node */
void insert_After(struct Node *prev_node, int new_data)
{
// check if prev node is null
if (prev_node == NULL)
{
cout << "Previous node is required , it cannot be NULL";

10
return;
}
// allocate memory for new node
struct Node* newNode = newNode;
// assign data to new node
newNode->data = new_data;
// set next of newnode to next of prev node
newNode->next = prev_node->next;
// set next of prev node to newnode
prev_node->next =newNode;
// now set prev of newnode to prev node
newNode->prev =prev_node;
// set prev of new node's next to newnode
if (newNode->next !=NULL)
newNode->next->prev = newNode;
}
// insert a new node at the end of the list
void insert_end(struct Node **head, int new_data)
{
// allocate memory for node
struct Node *newNode = new Node;
struct Node *last = *head; // set last node value to head
// set data for new node
newNode->data = new_data;
// new node is the last node , so set next of new node to null
newNode->next = NULL;
// check if list is empty, if yes make new node the head of list
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
// otherwise traverse the list to go to last node

11
while (last->next != NULL)
last = last->next;
// set next of last to new node
last->next = newNode;
// set last to prev of new node
newNode->prev = last; return;
}
// This function prints contents of linked list starting from the given node
void displayList(struct Node* node) {
struct Node *last;
while (node != NULL)
{
cout << node->data << "<==>";
last = node;
node = node->next;
}
if (node == NULL)
cout << "NULL";
}
// main program
int main() {

clrscr();

/* Start with the empty list */


struct Node *head = NULL;
// Insert 40 as last node
insert_end(&head, 4);
// insert 20 at the head
insert_front(&head,2);
// Insert 10 at the beginning.
insert_front(&head,1);
// Insert 50 at the end.

12
insert_end(&head, 6);
// Insert 30, after 20.
insert_After(head->next, 3);
cout << "Doubly linked list is as follows: " << endl;
displayList(head);
getch();
return 0;
}

13
OUTPUT:-

14
(CIRCULAR LINKED LIST)
PROGRAM:-
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
//using namespace std;
struct Node
{
int data;
struct Node *next;
};
//insert a new node in an empty list
struct Node *insertInEmpty(struct Node *last, int new_data)
{
// if last is not null then list is not empty, so return
if (last != NULL)
return last;
// allocate memory for node
struct Node *temp = new Node;
// Assign the data.
temp -> data = new_data;
last = temp;
// Create the link.
last->next = last;
return last;
} //insert new node at the beginning of the list
struct Node *insertAtBegin(struct Node *last, int new_data)
{
//if list is empty then add the node by calling insertInEmpty
if (last == NULL)
return insertInEmpty(last, new_data);
//else create a new node
struct Node *temp = new Node;

15
//set new data to node
temp -> data = new_data;
temp -> next = last -> next;
last -> next = temp;
return last;
} //insert new node at the end of the list
struct Node *insertAtEnd(struct Node *last, int new_data)
{
//if list is empty then add the node by calling insertInEmpty
if (last == NULL)
return insertInEmpty(last, new_data);
//else create a new node
struct Node *temp = new Node;
//assign data to new node
temp -> data = new_data;
temp -> next = last -> next;
last -> next = temp;
last = temp;
return last;
} //insert anew node in between the nodes
struct Node *insertAfter(struct Node *last, int new_data, int after_item)
{
//return null if list is empty
if (last == NULL)
return NULL;
struct Node *temp, *p;
p = last -> next;
do
{
if (p ->data == after_item)
{
temp = new Node;
temp -> data = new_data;

16
temp -> next = p -> next;
p -> next = temp;
if (p == last)
last = temp;
return last;
}
p = p -> next;
} while(p != last -> next);
cout << "The node with data "<<after_item << " is not present in the list." << endl;
return last;
} //traverse the circular linked list
void traverseList(struct Node *last) {
struct Node *p;
// If list is empty, return.
if (last == NULL) {
cout << "Circular linked List is empty." << endl;
return;
}
p = last -> next; // Point to the first Node in the list.
// Traverse the list starting from first node until first node is visited again
do {
cout << p -> data << "==>";
p = p -> next;
} while(p != last->next);
if(p == last->next)
cout<<p->data;
cout<<"\n\n";
}
//delete the node from the list
void deleteNode(Node** head, int key)
{
// If linked list is empty retun
if (*head == NULL)

17
return;
// If the list contains only a single node,delete that node; list is empty
if((*head)->data==key && (*head)->next==*head) {
free(*head);
*head=NULL;
}
Node *last=*head,*d;
// If key is the head
if((*head)->data==key) {
while(last->next!=*head) // Find the last node of the list
last=last->next;
// point last node to next of head or second node of the list
last->next=(*head)->next;
free(*head);
*head=last->next;
}
// end of list is reached or node to be deleted not there in the list
while(last->next!=*head&&last->next->data!=key) {
last=last->next;
} // node to be deleted is found, so free the memory and display the list
if(last->next->data==key) {
d=last->next;
last->next=d->next;
cout<<"The node with data "<<key<<" deleted from the list"<<endl;
free(d);
cout<<endl;
cout<<"Circular linked list after deleting "<<key<<" is as follows:"<<endl;
traverseList(last);
}
else
cout<<"The node with data "<< key << " not found in the list"<<endl;
}
// main Program

18
int main()
{
clrscr();
cout<<"211061101310 NISHANT KUMAR EX-2(CIRCULAR LINKED LIST)"<<endl;
struct Node *last = NULL;
last = insertInEmpty(last, 13);
last = insertAtBegin(last, 21);
last = insertAtBegin(last, 11);
last = insertAtEnd(last, 14);
last = insertAtEnd(last, 16);
last = insertAfter(last, 51,14 );
cout<<"The circular linked list created is as follows:"<<endl;
traverseList(last);
deleteNode(&last,13);
getch();
return 0;
}

19
OUTPUT:-

20
EX.NO:- 3
STACK – OPERATIONS USING ARRAYS AND LINKED LISTS
(USING ARRAYS)
PROGRAM:-
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
//using namespace std;
class stack
{
private:
struct node
{ int data;
struct node*link;
}
*top,*t,*temp;
public:
stack(){
top=NULL;
}
void push(int);
int pop();
void display();
};
void stack::push(int num)
{ t=new node;
t->data=num;
t->link=top;
top=t;
} int stack::pop()
{
node*temp;
21
temp=top;
if(top==NULL)
cout<<"Stack is Empty"<<endl;
else
{
cout<<"The deleted value is:\t" <<top->data<<endl;
top=top->link;
}
delete temp;
return(0);
}
void stack::display()
{ for(temp=top;temp!=NULL;temp=temp->link)
{
cout<<temp->data<<"\t";
}
cout<<endl;
} int main()
{
stack s;
int ch,no;
clrscr();
cout<<"211061101310 NISHANT KUMAR EX-3(ARRAY)"<<endl;
cout<<"STACK OPERATIONS \n 1.Push 2.Pop 3.Display 4.Exit \n";
do{
cout<<"Enter the choice\t ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter Value: ";
cin>>no;
s.push(no);
break;

22
case 2:s.pop();
break;
case 3:s.display();
break;
case 4: exit(0);
}}
while(ch!=4);
getch();
return 0;
}

23
OUTPUT:-

24
(USING LINKED LISTS)
PROGRAM:-
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
class stack{
private:
struct node{
int data;
struct node*link;
}
*top,*t,*temp;
public:
stack(){
top=NULL;
}
void push(int);
int pop();
void display();
};
void stack::push(int num){
t=new node;
t->data=num;
t->link=top;
top=t;
}
int stack::pop(){
node*temp;
temp=top;
if(top==NULL)
cout<<"Stack is Empty"<<endl;
else{

25
cout<<"The deleted value is:\t" <<top->data<<endl;
top=top->link;}
delete temp;
return(0);
}
void stack::display(){
for(temp=top;temp!=NULL;temp=temp->link){
cout<<temp->data<<"\t";
}
cout<<endl;
}
int main(){
stack s;
int ch,no;
clrscr();
do{
cout<<"Enter the choice\t1.Insert 2.Delete 3.Display 4.Exit: ";
cin>>ch;
switch(ch){
case 1: cout<<"Enter Value: ";
cin>>no;
s.push(no);
break;
case 2:s.pop();
break;
case 3:s.display();
break;
case 4: exit(0);
}}
while(ch!=4);
getch();
return 0;
}

26
OUTPUT:-

27
EX.NO:- 4
INFIX TO POSTFIX CONVERSION
PROGRAM:-
#include <iostream.h>
#include <conio.h>
#include <malloc.h>
char inf[40], post[40];
int top = 0, st[20];
void postfix();
void push(int);
char pop();
int main(){
cout << "\nEnter the infix Expression:";
cin >> inf;
postfix();
return 0;
}
// postfix function:
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)

28
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);
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;

29
getch();
}
// push function
void push(int ele)
{
top++;
st[top] = ele;
}
// pop function
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);
}

30
OUTPUT:-

31
EX.NO:- 5
EVALUATION TO POSTFIX EXPRESSION
PROGRAM:-
#include<iostream.h>
//using namespace std;
#include<ctype.h>
#include<stdio.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;
}
}
/* define pop operation */
int pop(){
int item;
if (top < 0){
cout<<"stack under flow";
}
else{
item = stack[top];

32
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){
case '*':
val = B * A; break;
case '/':
val = B / A; break;
case '+':
val = B + A;
break;
case '-':
val = B - A;
break;
}

33
push(val);
}
}
cout<<" \n Result of expression evaluation : \n"<<pop();
}
int main(){
clrscr();
int i;
char postfix[POSTFIXSIZE];
cout<<" \nEnter postfix expression,\npress right parenthesis ')' for end expression : ";
for (i = 0; i <= POSTFIXSIZE - 1; i++) {
cin>>postfix[i];
if (postfix[i] == ')') {
break;
}
}
EvalPostfix(postfix);
getch();
return(0);
}

34
OUTPUT:-

35
EX.NO:- 6
QUEUE – OPERATIONS USING ARRAYS AND LINKED LISTS
(USING ARRAYS)
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<<"\nQueue is full";
return;
}
rear++;
cin>>que[rear];
if(front==-1) front++;
}
void queue::dequeue()
{
36
if (front==-1)
{
cout<<"Queue is Empty\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<<"\nQueue is Empty";
return;
}
for(int i=front;i<=rear;i++)
cout<<que[i]<<"\t";
cout<<endl;
}
main()
{
clrscr();

queue q; int ch;


do
{
cout<<"Enter the choice 1.Insert 2.Delete 3.Display 4.Exit: ";
cin>>ch;
switch(ch)

37
{
case 1: cout<<"Enter value:\t";
q.enqueue();
break;
case 2: q.dequeue();
break;
case 3: q.display();
break;
case 4: exit(0);
}
}while(ch!=4);
getch();
return 0;
}

38
OUTPUT:-

39
(USING LINKED LISTS)
PROGRAM:-
#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
class queue
{
private:
struct node
{
int data;
struct node *link;
} * front, *rear, *t, *temp;

public:
queue()
{
front = rear = NULL;
}
void enq(int);
int deq();
void disp();
};
void queue::enq(int num)
{
node *t;
t = new node;
t->data = num;
if (front == NULL)
{
front = t;

40
}
else
rear->link = t;
t->link = NULL;
rear = t;
}
int queue::deq()
{
node *temp;
temp = front;
if (front == NULL)
cout << "Queue is Empty\n";
else
{
temp = front;
cout << "The Deleted Value is: " << temp->data << endl;
front = front->link;
delete temp;
}
return 0;
}
void queue::disp()
{
node *t;
for (t = front; t != NULL; t = t->link)
cout << t->data << "\t";
cout << endl;
}
void main()
{
clrscr();
cout<<"SAIDUR RAHMAN 211061101383:"<<endl;
queue s;

41
int val, ch, no;
do{
cout << "Enter the choice 1.Insert 2.Deete 3.Display 4.Exit: ";
cin >> ch;
switch (ch){
case 1:
cout << "Enter the Value: ";
cin >> no;
s.enq(no);
break;
case 2:
s.deq();
break;
case 3:
s.disp();
break;
case 4:
exit(0);
}
} while (ch != 4);
getch();
}

42
OUTPUT:-

43
EX.NO:- 7
DEQUEUE, CIRCULAR – OPERATIONS
PROGRAM:-
#include <iostream.h>
//using namespace std;
#include <conio.h>
#include <limits.h>
#define MAX 50
class deq
{
int arr[MAX];
int front;
int rear;
int 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();
};

44
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 << "\n Overflow \n"<< endl;
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else if (front == 0)
{
front = size - 1;
}
else
{
front = front - 1;
}
arr[front] = key;

45
cout <<endl<< arr[front] << " inserted at front of dequeue" << endl;
}
void deq::insertRear(int key)
{
if (isFull())
{
cout << "\n 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 << "\n"<< arr[rear] << " inserted at rear of dequeue" << endl;
}
void deq::deleteFront()
{
if (isEmpty())
{
cout << "\n Queue Underflow \n" << endl;
return;
}
cout << endl<< getFront() << "removed from front of dequeue" << endl;

46
if (front == rear)
{
front = -1;
rear = -1;
}
else if (front == size - 1)
{
front = 0;
}
else
{
front = front + 1;
}
}
void deq::deleteRear()
{
if (front == rear)
{
front = -1;
rear = -1;
}
else if (rear == 0)
{
rear = size - 1;
}
else
{
rear = rear - 1;
}
}
int deq::getFront()
{
if (isEmpty())

47
{
cout << "\n Underflow \n"<< endl;
return -1;
}
return arr[front];
}
int deq::getRear()
{
if (isEmpty() || rear < 0)
{
cout << "\nUnderflow \n"<< endl;
return -1;
}
return arr[rear];
}
void deq::display()
{
if (front == -1)
{
cout << "\nDequeue is Empty";
return;
}
cout << "\n 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++)

48
{
cout << arr[i] << " ";
}
}
cout << endl;
}
struct Queue
{
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 << "\nQueue is Full\n";
return;
}
else if (front == -1)
{
front = rear = 0;
arr[rear] = value;
}

49
else if (rear == size - 1 && front != 0)
{
rear = 0;
arr[rear] = value;
}
else
{
rear++;
arr[rear] = value;
}
cout << endl << arr[rear] << " inserted into circular queue" << endl;

}
int Queue::dequ()
{
if (front == -1)
{
cout << "\nQueue is empty\n";
return INT_MIN;
}
cout << endl << arr[front] << "removedfromcircularqueue" << endl;
int data = arr[front];
arr[front] = -1;
if (front == rear)
{
front = -1;
rear = -1;
}
else if (front == size - 1)
{
front = 0;
}
else

50
{
front++;
}
return data;
}
void Queue::displayQ()
{
if (front == -1)
{
cout << "\nQueue is Empty\n";
return;
}
cout << "\n Elementsin 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;
}

51
int main()
{
clrscr();
deq dq(10);
Queue cq(10);
int ch;
do
{
cout << "\nEnter value" << endl;
cout << "\n1. Dequeue" << endl;
cout << "\n2. Circular Queue "<<endl; cout<<"\n3.Exit "<<endl;
cin >>ch;
switch (ch)
{
case 1:
{
int c1;
cout << "\n1. Insertion at Front of Dequeue" << endl;
cout << "\n2. Insertion at Rear of Dequeue" << endl;
cout << "\n3. Deletion at Front of Dequeue" << endl;
cout << "\n4. Deletion at Rear of Dequeue" << endl;
cout << "\n5. Display Dequeue" << endl;
cout << "\n6. Quit" << endl;
do
{
cout << "\nEnter any value" << endl;
cin >> c1;
switch (c1)
{
case 1:
{
int d1;
cout << "\nEnter an element to insert";

52
cin >> d1;
dq.insertFront(d1);
break;
}
case 2:
{
int d2;
cout << "\nEnter 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 << "\nEnd of program on Dequeue Operation" << endl;
break;
}
default:

53
cout << "Invalid choice" << endl;
}
}
while (c1 != 6);
break;
}
case 2:
{
int c2;
cout << "\n1. Enqueue on Circular queue" << endl;
cout << "\n2. Dequeue on Circular queue" << endl;
cout << "\n3. Display Circular queue" << endl;
cout << "\n4. Quit" << endl;
do
{
cout << "\n Enter a choice: "<< endl;
cin >> c2;
switch (c2)
{
case 1:
{
int eq;
cout << "\n Enter an element to insert:";
cin >> eq;
cq.enq(10);
break;
}
case 2:
cq.dequ();
break;
case 3:
cq.displayQ();
break;

54
case 4:
cout << "End of program on Circular Queue" << endl;
break;
default:
cout << "\n 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;
}

55
OUTPUT:-

56
57
EX.NO- 8
BINARY TREE TRAVERSALS – INORDER, PREORDER, POSTORDER USING
RECURSION
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)
58
{
if (node == NULL)
return;
postOrder(node->left);
postOrder(node->right);
cout << node->data << " ";
}
int main()
{
clrscr();
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 << "\nPreorder traversal of binary tree is\n";
preOrder(root);
cout << "\nInorder traversal of binary tree is\n";
inOrder(root);
cout << "\nPostorder traversal of binary tree is\n";
postOrder(root);
getch();
return 0;
}

59
OUTPUT:-

60
EX.NO:- 9
BINARY TREE TRAVERSALS – INORDER, PREORDER, POSTORDER USING
NON- RECURSION
PROGRAM:-
#include <iostream.h>
#include<conio.h>
struct tNode{
int data;
struct tNode *left;
struct tNode *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
{
61
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
{
62
struct tNode *current = root->left;
while (current->right && current->right != root)
current = current->right;
if (current->right == root)
{
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)
63
{
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;
}
64
while (prev != NULL)
{
cout << prev->data << " ";
temp = prev->right;
prev->right = current;
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();
struct tNode *root = newtNode(1);
root = newtNode(1);
root->left = newtNode(2);
65
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;
}

66
OUTPUT:-

67
EX.NO:- 10
LINEAR AND BINARY SEARCH
PROGRAM:-
#include <iostream.h>
#include<conio.h>
#define MAX 10
int ls(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 = l + (r - 1) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return bs(arr, l, mid - 1, x);
return bs(arr, mid + 1, r, x);
}
return -1;
}
int main()
{

68
int arr[MAX];
clrscr();
cout<<"SAIDUR RAHMAN 211061101383:"<<endl;
cout << "Enter the size of array: ";
int n, ch, x, result;
cin >> n;
cout << "\nEnter the elements: " << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "\n1. Linear Search" << endl;
cout << "2. Binary Search" << endl;
cout << "3. Exit" << endl;
do
{
cout << "\nEnter a choice: ";
cin >> ch;
switch (ch)
{
case 1:
cout << "\n Linear Search" << endl;
cout << "Enter element to search ";
cin >> x;
result = ls(arr, n, x);
(result == -1) ? cout << "Not present" : cout << "Present at index: " << result;
break;
case 2:
cout << "\n Binary Search" << 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;

69
break;
case 3:
cout << "Exit";
break;
default:
cout << "Invalid choice";
}
} while (ch != 3);
getch();
return 0;
}

70
OUTPUT:-

71
EX.NO:- 11
SORTING – SLECTION SORT, QUICK SORT, HEAP SORT AND MERGE SORT
PROGRAM:-
#include <iostream.h>
#include<conio.h>
//using namespace std;
#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)

72
{
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;
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)

73
{
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 l, int m, int r)
{
int n1 = m - l + 1;
int 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 = 1;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}

74
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void ms(int arr[], int l, int r)
{
if (l >= r)
{
return;
}
int m = l + (r - 1) / 2;
ms(arr, l, m);
ms(arr, m + 1, r);
merge(arr, l, m, r);
}
void print(int arr[], int size)
{

75
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int ch, n;
clrscr();
cout<<"211061101310 NISHANT KUMAR EX- 11"<<endl;
cout << "\n1. Selection Sort" << endl;
cout << "2.Quick Sort" << endl;
cout << "3.Heap Sort" << endl;
cout << "4.Merge Sort" << endl;
cout << "Enter choice ";
cin >> ch;
cout << "\nEnter the size of the array: ";
cin >> n;
int arr[MAX];
cout << "\nElements: ";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << endl;
switch (ch)
{
case 1:
ss(arr, n);
print(arr, n);
break;
case 2:

76
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 << "Invalid choice";
}
getch();
return 0;
}

77
OUTPUT:-

78
79
EX.NO:- 12
ADDITION, MULTIPLICATION OF SPARSE MARTRICES
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()
{

80
clrscr();
cout<<"SAIDUR RAHMAN 211061101383:"<<endl;
int i, j; // To store result
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;
}

81
OUTPUT:-

82
EX.NO:- 13
POLYNOMIAL ADDITION AND MULTIPLICATON
PROGRAM:-
#include <iostream.h>
//using namespace std;
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 (int 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 i = 0; i < m; i++){
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 << " + ";

83
}
cout << endl;
}
int main(){
int a1, b1, c1, a2, b2, c2;
cout << "Enter the coefficient:" << endl;
cout << "1st polynomial coefficient: ";
cin >> a1 >> b1 >> c1;
cout << "2nd polynomial coefficient: ";
cin >> a2 >> b2 >> c2;
int a[] = {a1, b1, c1};
int b[] = {a2, b2, c2};
int m = sizeof(a) / sizeof(a[0]);
int n = sizeof(b) / sizeof(b[0]);
cout << "First Expression: ";
print(a, m);
cout << "\nSecond Expression: ";
print(b, n);
int *sum = add(a, b, m, n);
cout << "\n Sum of the polynomial: ";
print(sum, size);
int *prod = multiply(a, b, m, n);
cout << "nProduct polynomial is n";
print(prod, m + n - 1);
return 0;
}

84
OUTPUT:-

85
EX.NO:- 14
DEPTH FIRST SEARCH OF A GRAPH
PROGRAM:-
#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();
cout<<"SAIDUR RAHMAN 211061101383:"<<endl;
int m;
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 << "Enter initial vertex: ";
cin >> v;
cout << "Order 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)

86
{
visit[j] = 1;
stk[top] = j;
top++;
}
v = stk[--top];
cout << v << " ";
k++;
visit[v] = 0;
getch();
visited[v] = 1;
}
}

87
OUTPUT:-

88
EX.NO:- 15
BREADTH FIRST SEARCH OF A GRAPH
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];
int main()
{
int m;
clrscr();
cout<<"SAIDUR RAHMAN 211061101383:"<<endl;
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 << "Enter initial vertex: ";
cin >> v;
cout << "Visited 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)

89
{
visit[j] = 1;
qu[re++] = j;
}
v = qu[fr++];
cout << v << " ";
k++;
visit[v] = 0;
visited[v] = 1;
}
getch();
return 0;
}

90
OUTPUT:-

91

You might also like