You are on page 1of 110

EXPT NO:1 DATE: 10-08-2022

IMPLEMENTATION OF ARRAYS-INSERTION AND DELETION

AIM :

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

ALGORITHM:

Step 1 - Start the program.

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

Step 3- Check the condition and select the choice.

Case 1- Insert in to the stack

Check whether the stack is full .if it is full display stack is full otherwise push
operation increases top by one and writes pushed element to array;

Case 2- Delete from the stack

If the stack is empty then it is underflow else delete the items from the b stack pop
operation checks that top is not equal to -1 and decreases top variable by 1

Case 3 - DISPLAY

Check if top is equal to -1 ,print element is empty or else display the content of
stack.

AREATI MAHENDRA 1 211061101031


PROGRAM:
#include<iostream.h>
#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;
}
cout<<"Element deleted..";
}
void update(){int y,in;
cout<<"Enter indexand 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:.";

AREATI MAHENDRA 2 211061101031


}
int main()
{
cout<<”AREATI MAHENDRA : 211061101031\n”;
int ch;clrscr();
do{
cout<<"\n ARRAY 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:
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;
}

AREATI MAHENDRA 3 211061101031


OUTPUT:

RESULT:

Thus the program is executed and the output is verified.

AREATI MAHENDRA 4 211061101031


EXPT NO:2A DATE: 17-08-2022

LINKED LISTS- CREATION, INSERTION, DELETION OF SINGLE, DOUBLE AND


CIRCULAR LISTS. (SINGLY LINKED LISTS)

AIM:

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

ALGORITHM:

Step 1: Start the program.

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

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

Step 4: Check the condition, whether the list is empty or not.

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

Step 6: End of the program.

AREATI MAHENDRA 5 211061101031


PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
//using namespace std;
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<<”AREATI MAHENDRA : 211061101031\n”;
cout<<"\n----SINGLY LINKED LIST OPERATIONS----\n";
do{
cout<<"\n ----What you wanna do ----.\n";
cout<<"1. Insert. 2.Delete. 3.Display. 4.Exit. \n";
cout<<" Enter your option: ";
cin>>option;
switch(option){
case 1:
insert();
break;
case 2:
delete_node();

AREATI MAHENDRA 6 211061101031


break;
case 3:
display();
break;
case 4:
cout<<" \t\nEnd of singly linked list...............";
break;
default:
break;
}
}while(option!=4);
return 0;
}
void insert(){
cout<<"Enter element for inserting in linked list:\n";
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;
}
void display(){
int count=0;
temp_node=first_node;
cout<<"\n Displaying the linked list :";
while(temp_node!=0){

AREATI MAHENDRA 7 211061101031


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<<"\n No of items in Linked: "<<count<<endl;
return count;
}
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;

AREATI MAHENDRA 8 211061101031


}
cout<<"Deleted successfully..";
break;
}else{
i++;
prev_node=temp_node;
temp_node=temp_node->next;
}
}
}
}else{
cout<<"\n Invalid position\n\n";
getch()
}
}

AREATI MAHENDRA 9 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 10 211061101031


EXPT NO:2B DATE: 17-08-2022

LINKED LISTS- CREATION, INSERTION, DELETION OF SINGLE, DOUBLE AND


CIRCULAR LISTS. (DOUBLY LINKED LISTS)

AIM:

To write a c++ program to implement doubly linked list.

ALGORITHM:

Step 1: Start the Program.

Step 2: Read the Value of Ch.

Step 3: For ch=1; ch=2; ch=3; ch=4;ch=5; Call Create, insert, delete, view and exit functions
respectively.

Step 4: Repeat until ch!=5.

Step 5: Create a list.

Step 6: Add an element in the list.

Step 7: Delete a node from the list.

Step 8: View the list.

Step 9: Stop.

AREATI MAHENDRA 11 211061101031


PROGRAM:
#include <iostream.h>
#include<conio.h>
#include<malloc.h>
//using namespace std;
// 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;
newNode->next = (*head);
newNode->prev = NULL;
// previous of head is new node
if ((*head) != NULL)
(*head)->prev = newNode;
(*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";

AREATI MAHENDRA 12 211061101031


return;
}
// allocate memory for new node
struct Node *newNode = new Node;
newNode->data = new_data;
newNode->next = prev_node->next;
prev_node->next = newNode;
newNode->prev = prev_node;
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;
newNode->data = new_data;
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
while (last->next != NULL)
last = last->next;
last->next = newNode;
newNode->prev = last;
return;
}

AREATI MAHENDRA 13 211061101031


// 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()
{
/* Start with the empty list */
struct Node *head = NULL;
insert_end(&head, 13);
insert_front(&head, 34);
insert_front(&head, 88);
insert_end(&head, 1);
insert_After(head->next, 11);
clrscr();
cout << "Doubly linked list is as follows: " << endl;
displayList(head);
getch();
return 0;
}

AREATI MAHENDRA 14 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 15 211061101031


EXPT NO:2C DATE: 17-08-2022

LINKED LISTS- CREATION, INSERTION, DELETION OF SINGLE, DOUBLE AND


CIRCULAR LISTS. (CIRCULAR LINKED LISTS)

AIM:

To write a c++ program to implement circular linked list.

ALGORITHM:

Step 1: Start the Program.

Step 2: Read the Value of Ch.

Step 3: For ch=1; ch=2; ch=3; ch=4; ch=5; Call Create, insert, delete, view and exit functions
respectively.

Step 4: Repeat until ch!=5

Step 5: Create a list.

Step 6: Add an element in the list.

Step 7: Delete a node from the list.

Step 8: View the list.

Step 9: Stop.

AREATI MAHENDRA 16 211061101031


PROGRAM:

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.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)

AREATI MAHENDRA 17 211061101031


return insertInEmpty(last, new_data);
//else create a new node
struct Node *temp = new Node;
//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 a new 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;

AREATI MAHENDRA 18 211061101031


do
{
if (p ->data == after_item)
{
temp = new Node;
temp -> data = new_data;
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)

AREATI MAHENDRA 19 211061101031


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

AREATI MAHENDRA 20 211061101031


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
int main()
{
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 );
clrscr();
cout<<"The circular linked list created is as follows:"<<endl;
traverseList(last);
deleteNode(&last,13);
getch();
return 0;
}

AREATI MAHENDRA 21 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 22 211061101031


EXPT NO:3A DATE: 24-08-2022

STACK-OPERATIONS USING ARRAYS AND LINKED LIST

(USING LINKED LIST)

AIM:

To write a c++ program to implement stack using linked lists.

ALGORITHM:

Step 1: Start the program.

Step 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.

Step 3: The stack is growable by attaching more nodes, the newly created node becomes
the top node and the link of top becomes head node.

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

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

Step 6: End of the program.

AREATI MAHENDRA 23 211061101031


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()

AREATI MAHENDRA 24 211061101031


{
node*temp;
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();
do{
cout<<"Enter the choice"<<endl;
cout<<"1.Insert 2.Delete 3.Display 4.Exit:";
cin>>ch;
switch(ch)
{

AREATI MAHENDRA 25 211061101031


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;
}

AREATI MAHENDRA 26 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 27 211061101031


EXPT NO:3B DATE: 24-08-2022

STACK-OPERATIONS USING ARRAYS AND LINKED LISTS (USING ARRAYS)

AIM:

To write a c++ program to implement stack using array.

ALGORITHM:

Step 1 - Start the program.

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

Step 3 - check the condition and select the choice.

Case 1- Insert in to the stack Check whether the stack is full .if it is full display stack is full
otherwise push operation increases top by one and writes pushed element to array;

Case 2- Delete from the stack If the stack is empty then it is underflow else delete the items
from the b stack pop operation checks that top is not equal to -1 and decreases top
variable by 1

Case 3 –Display, Check if top is equal to -1, print element is empty or else display the content
of stack.

AREATI MAHENDRA 28 211061101031


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()

AREATI MAHENDRA 29 211061101031


{
node*temp;
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<<"STACK OPERATIONS USING ARRAY:"<<endl;
cout<<"\n1.Push 2.Pop 3.Display 4.Exit";
do{
cout<<"\nEnter the choice: ";
cin>>ch;
switch(ch)

AREATI MAHENDRA 30 211061101031


{
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;
}

AREATI MAHENDRA 31 211061101031


AREATI MAHENDRA 32 211061101031
OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 33 211061101031


EXPT NO:4 DATE: 07-09-2022

INFIX TO POSTFIX CONVERSION

AIM:

To write a c++ program to implement infix expression to postfix expression.

ALGORITHM:

1. Start the program

2. Scan the Infix string from left to right.

3. Initialize an empty stack.

4. A) If the scanned character is an operand, add it to the Postfix string.

B) If the scanned character is an operator and if the stack is empty Push the character to
stack.

C) If the scanned character is an Operand and the stack is not empty, compare the
precedence of the character with the element on top of the stack (topstack).

D) If top stack has higher precedence over the scanned character Pop the stack else push
the scanned character to stack. Repeat this step as long as stack is not empty and
topstack has precedence over the character. Repeat this step till all the characters are
scanned.

5. (After all characters are scanned, we have to add any character that the stack may have to
the Postfix string.) If stack is not empty add topstack to Postfixstring and pop the
stack.Repeat this step as long as stack is not empty.

6. Return the Postfix string.

7. Terminate the program.

8. STOP

AREATI MAHENDRA 34 211061101031


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(){
clrscr();
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)
post[j++] = pop();
push(2);
break;

AREATI MAHENDRA 35 211061101031


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;
getch();
}
// push function

AREATI MAHENDRA 36 211061101031


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;
}
getch();
return (e);
}

AREATI MAHENDRA 37 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 38 211061101031


EXPT NO:5 DATE: 14-09-2022

EVALUATION TO POSTFIX EXPRESSION

AIM:

To write a c++ program to implement to postfix expression.

ALGORITHM:

1. Start the program

2. Scan the Infix string from left to right.

3. Initialize an empty stack.

4. A) If the scanned character is an operand, add it to the Postfix string.

B) If the scanned character is an operator and if the stack is empty Push the character to
stack.

C) If the scanned character is an Operand and the stack is not empty, compare the
precedence of the character with the element on top of the stack (topstack).

D) If top stack has higher precedence over the scanned character Pop the stack else

Push the scanned character to stack.

Repeat this step as long as stack is not empty and topstack has precedence over the
character.

Repeat this step till all the characters are scanned.

5. (After all characters are scanned, we have to add any character that the stack may have to
the Postfix string.) If stack is not empty add topstack to Postfixstring and Pop the stack.
Repeat this step as long as stack is not empty.

6. Return the Postfix string.

7. Terminate the program. 8. STOP.

AREATI MAHENDRA 39 211061101031


PROGRAM:
#include<iostream.h>
#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];
top = top - 1;
return item;

AREATI MAHENDRA 40 211061101031


}
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;
}
push(val);

AREATI MAHENDRA 41 211061101031


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

AREATI MAHENDRA 42 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 43 211061101031


EXPT NO:6A DATE: 21-09-2022

QUEUE-OPERATION USING ARRAYS AND LINKED LISTS (USING ARRAY)

AIM:

To write a c++ program to implement queue using array

ALGORITHM:

Step 1 - Start the program.

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

Step 3 - check the condition and select the choice.

Case 1- ADD QUEUE

Check whether the queue is full. If queue is full display queue is full otherwise
increment rear.

Case 2- DEL QUEUE

Check whether the queue is full. If queue is EMPTY OR not, if queue is empty.

Return null otherwise make front is -1 or rear is -1 else increment front.

Case 3 -DISPLAY

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

AREATI MAHENDRA 44 211061101031


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()

AREATI MAHENDRA 45 211061101031


{
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;
}
int main()
{
clrscr();
queue q;
int ch;
do
{
cout<<"\nEnter the choice 1.Insert 2.Delete 3.Display 4.Exit: ";
cin>>ch;

AREATI MAHENDRA 46 211061101031


switch(ch)
{
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;
}

AREATI MAHENDRA 47 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 48 211061101031


EXPT NO:6B DATE: 21-09-2022

QUEUE-OPERATION USING ARRAYS AND LINKED LISTS

(USING LINKED LISTS)

AIM:

To write a c++ program to implement queue using linked lists.

ALGORITHM:

Step 1: Start the program

Step 2: Declare a class contain a status to decides nodes

Step 3: Promot user for choice if choice,

a) Inqueue b)delqueue c)display d)exit

Step 4: Inqueue

a) Allocate a new node

b) put value in the field

c)mark it rear

d)return

Step 5: Then do step'3' for delqueue display & exit

Step 6: Exit the program.

AREATI MAHENDRA 49 211061101031


PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
//using namespace std;
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;

AREATI MAHENDRA 50 211061101031


}
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;
}
int main()
{
queue s;
int val, ch,no;

AREATI MAHENDRA 51 211061101031


clrscr();
do
{
cout<<"Enter the choice 1.Insert 2.Delete 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();
return 0;
}

AREATI MAHENDRA 52 211061101031


OUTPUT:

RESULT:

AREATI MAHENDRA 53 211061101031


Thus, the program is executed and the output is verified.

EXPT NO:7 DATE: 08-10-2022

DEQUEUE, CIRCULAR- OPERATIONS.

AIM:

To write a c++ program to implement dequeue using circular operations.

ALGORITHM:

Step 1: Start the program

Step 2: Declare a class contain a status to decides nodes

Step 3: Promote user for choice if choice,

a) Inqueue b) Delqueue c) Display d)Exit

Step 4: Inqueue

a) Allocate a new node

b) put value in the field

c) mark it rear

d) return

Step 5: Then do step'3' for delqueue display & exit

Step 6: Exit the program.

AREATI MAHENDRA 54 211061101031


PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<limits.h>
//using namespace std;
#define MAX 10
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();
};
int deq::isFull()
{

AREATI MAHENDRA 55 211061101031


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

AREATI MAHENDRA 56 211061101031


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 << "\nQueue Underflow\n"

AREATI MAHENDRA 57 211061101031


<< endl;
return;
}
cout << endl
<< 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 (front == rear)
{
front = -1;
rear = -1;
}
else if (rear == 0)
rear = size - 1;
else
{
rear = rear - 1;
}

AREATI MAHENDRA 58 211061101031


}
int deq::getFront()
{
if (isEmpty())
{
cout << "\nUnderflow\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 << "\nElements in Dequeue are: ";
if (rear >= front)
{
for (int i = front; i <= rear; i++)

AREATI MAHENDRA 59 211061101031


{
cout << arr[i] << " ";
}
}
else
{
for (int i = front; i < size; i++)
{
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)))
{

AREATI MAHENDRA 60 211061101031


cout << "\nQueue 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 << 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] << " removed from circular queue" << endl;
int data = arr[front];
arr[front] = -1;

AREATI MAHENDRA 61 211061101031


if (front == rear)
{
front = -1;
rear = -1;
}
else if (front == size - 1)
{
front = 0;
}
else
{
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
{

AREATI MAHENDRA 62 211061101031


for (int i = front; i < size; i++)
{
cout << arr[i] << " ";
}
for (int j = 0; j <= rear; j++)
{
cout << arr[j] << " ";
}
}
cout << endl;
}
int main()
{
deq dq(10);
Queue cq(10);
int ch;
clrscr();
do
{
cout << "\nEnter value" << endl;
cout << "1. Dequeue" << endl;
cout << "2. Circular Queue" << endl;
cout << "3. Exit" << endl;
cin >> ch;
switch (ch)
{
case 1:
{
int c1;
cout << "\n1. Insertion at Front of Dequeue" << endl;
cout << "2. Insertion at Rear of Dequeue" << endl;
cout << "3. Deletion at Front of Dequeue" << endl;

AREATI MAHENDRA 63 211061101031


cout << "4. Deletion at Rear of Dequeue" << endl;
cout << "5. Display Dequeue" << endl;
cout << "6. Quit" << endl;
do
{
cout << "\nEnter any value" << endl;
cin >> c1;
switch (c1)
{
case 1:
{
int d1;
cout << "\nEnter an element to insert: ";
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:
{

AREATI MAHENDRA 64 211061101031


dq.deleteRear();
break;
}
case 5:
{
dq.display();
break;
}
case 6:
{
cout << "\nEnd of program on Dequeue Operation" << endl;
break;
}
default:
cout << "Invalid choice" << endl;
}
} while (c1 != 6);
break;
}
case 2:
{
int c2;
cout << "\n1. Enqueue on Circular queue" << endl;
cout << "2. Dequeue on Circular queue" << endl;
cout << "3. Display Circular queue" << endl;
cout << "4. Quit" << endl;
do
{
cout << "\n Enter a choice: "<<endl;
cin >> c2;
switch (c2)
{

AREATI MAHENDRA 65 211061101031


case 1:
{
int eq;
cout << "\nEnter 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 << "\nInvalid choice" << endl;
}
} while (c2 != 4);
break;
}
case 3:
{
cout << "End of program" << endl;
break;}
default:
cout << "Invalid Option" << endl;
}} while (ch != 3);
return 0;
}

AREATI MAHENDRA 66 211061101031


OUTPUT:

RESULT:

AREATI MAHENDRA 67 211061101031


Thus, the program is executed and the output is verified.

EXPT NO:8 DATE: 19-10-2022

BINARY TREE TRAVERSALS – INORDER, PREORDER, POSTORDER USING RECURSION

AIM:

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

ALGORITHM:

Step 1: Start the program.

Step 2: Enter all nodes into stack.

Step 3: Input the element in the array.

Step 4: For preorder traversal process root, left & right subtree.

Step 5: For postorder traversal process from left, right, root.

Step 6: For inorder traversal process from left, root, right.

Step 7: Display all the traversal.


Step 8: End the program.

AREATI MAHENDRA 68 211061101031


PROGRAM:
#include<iostream.h>
#include<conio.h>
//using namespace std;
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)

AREATI MAHENDRA 69 211061101031


{
if(node==NULL)
return;
postOrder(node->left);
postOrder(node->right);
cout<<node->data<<” “;
}
int main()
{
struct Node *root=new Node(10);
root->left=new Node(20);
root->right=new Node(30);
root->left->left=new Node(40);
root->left->right=new Node(50);
clrscr();
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;
}

AREATI MAHENDRA 70 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 71 211061101031


EXPT NO:9 DATE: 26-10-2022

BINARY TREE TRAVERSALS - INORDER, PREORDER, POSTORDER USING


NON-RECURSION

AIM:

To write a C++program to perform a Binary tree traversals - in order, pre order,


postorder using non recursion.

ALGORITHM:

Step1: Start the program.

Step 2: Enter all nodes into stack.

Step 3: Input the element in the array.

Step 4: For preorder traversal process root, left & right subtree.

Step 5: For postorder traversal process from left, right, root.

Step 6: For inorder traversal process from left, root, right.

Step 7: Display all the traversal.

Step 8: End the program.

AREATI MAHENDRA 72 211061101031


PROGRAM:
#include<iostream.h>
#include<conio.h>
//using namespace std;
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{
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;

AREATI MAHENDRA 73 211061101031


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)
{
current->right=NULL;
root=root->right;
}
else{
cout<<root->data<<" ";
current->right=root;
root=root->left;
}
}
}
}

AREATI MAHENDRA 74 211061101031


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;

AREATI MAHENDRA 75 211061101031


current->right=prev;
prev=current;
current=temp;
}
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()
{
struct tNode *root=newtNode(1);
root= newtNode(10);
root->left=newtNode(20);
root->right=newtNode(30);

AREATI MAHENDRA 76 211061101031


root->left->left=newtNode(40);
root->left->right=newtNode(50);
root->right->left=newtNode(60);
root->right->right=newtNode(70);
root->left->left->left=newtNode(80);
root->left->left->right=newtNode(90);
root->left->right->left=newtNode(100);
root->left->right->right=newtNode(110);
clrscr();
cout<<"Inorder: ";
inorder(root);
cout<<endl;
cout<<"Preorder: ";
preorder(root);
cout<<endl;
cout<<"Postorder: ";
postorder(root);
cout<<endl;
getch();
return 0;
}

AREATI MAHENDRA 77 211061101031


AREATI MAHENDRA 78 211061101031
OUTPUT:

RESULT:

AREATI MAHENDRA 79 211061101031


Thus, the program is executed and the output is verified.

EXPT NO:10 DATE: 09-11-2022

LINEAR AND BINARY SEARCH

AIM:

To write a C++ program on linear and binary search

ALGORITHM:

Step 1: Start the program

Step 2:

(i) Linear Search

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

If x matches with an element, return the index.

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

(ii) Binary Search

Compare x with the middle element

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

Else If x greater than the mid element, then x can only lie in right half subarray after
the middle element So we recur for right half.

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

Step 3: Prompt the user to select between linear and binary search.
Step 4: End the program

AREATI MAHENDRA 80 211061101031


PROGRAM:
#include<iostream.h>
#include<conio.h>
//using namespace std;
#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()
{

AREATI MAHENDRA 81 211061101031


int arr[MAX];
clrscr();
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;

AREATI MAHENDRA 82 211061101031


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

AREATI MAHENDRA 83 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 84 211061101031


EXPT NO:11 DATE: 23-11-2022

SORTING- SELECTION SORT, QUICK SORT, HEAP SORT AND MERGE SORT

AIM:

To write a C++ program on Sorting types – Selection Sort, Quick Sort, Heap Sort and
Merge Sort.

ALGORITHM:

Step-1: Start the program

Step-2: Case-1: Selection Sort

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

Case-2: 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.

Case-3: 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

Case 4 Merge Sort

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

Step-3: Take input of elements of array

Step-4: Prompt the user to perform any sorting

Step-5: Print the sorted array

Step-6 End the program.

AREATI MAHENDRA 85 211061101031


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)
{
i++;

AREATI MAHENDRA 86 211061101031


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

AREATI MAHENDRA 87 211061101031


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

AREATI MAHENDRA 88 211061101031


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)
{
for (int i = 0; i < size; i++)

AREATI MAHENDRA 89 211061101031


{
cout << arr[i] << “ “;
}
cout << endl;
}
int main()
{
int ch, n;
clrscr();
do
{
cout << “\n1. Selection Sort” << endl;
cout << “2.Quick Sort” << endl;
cout << “3.Heap Sort” << endl;
cout << “4.Merge Sort” << endl;
cout << “5. EXIT\n”;
cout << “Enter choice :\t”;
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);

AREATI MAHENDRA 90 211061101031


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;
case 5:
cout << “Ending the program…”;
break;
default:
cout << “Invalid choice”;
}
} while (ch != 5);
getch();
return 0;
}

AREATI MAHENDRA 91 211061101031


OUTPUT:

AREATI MAHENDRA 92 211061101031


RESULT:

Thus, the program is executed and verified successfully.

AREATI MAHENDRA 93 211061101031


EXPT NO:12 DATE: 24-11-2022

ADDITION, MULTIPLICATION OF SPARSE MATRICES

AIM:

To write a C++ program to implement addition and multiplication of sparse matrices.

ALGORITHM:

Step 1: Start the Program

Step 2: n=1

Step 3: ia=ib=ic=ja=jb=jc=1

Step 4: Repeat while(ia<=m)

Step 5: end.

Step 6: Stop the Program.

AREATI MAHENDRA 94 211061101031


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();
int i, j; // To store result
int re[N][N]={{0,0},{0,0}};
int res[N][N]={{0,0},{0,0}};

AREATI MAHENDRA 95 211061101031


int mat1[N][N] = { { 1, 2 },
{ 5, 4 } };
int mat2[N][N] = { {9, 13 },
{ 5, 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;
}

AREATI MAHENDRA 96 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

EXPT NO:13 DATE: 09-12-2022

AREATI MAHENDRA 97 211061101031


POLYNOMIAL ADDITION AND MULTIPLICATION

AIM:

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

ALGORITHM:

Step 1: Start the Program

Step 2: create a sum array sum [] of size equal to maximum of m and n

Step 3: copy A[] to sum []

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

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

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

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

Traverse array B[] and do following for every element B[j].

Step 8: prod[i+j] = prod[i+j] + A[i] * B[j]

Step 9: Return prod[]

Step 10: End of program.

AREATI MAHENDRA 98 211061101031


PROGRAM:
#include<iostream.h>
#include<conio.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 ( 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 ( 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;

AREATI MAHENDRA 99 211061101031


if (i != n - 1)
cout << " + ";
}
cout<<endl;
}
int main()
{
int a1, b1, c1, a2, b2, c2;
clrscr();
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);
getch();
return 0;
}

AREATI MAHENDRA 100 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 101 211061101031


EXPT NO:14 DATE: 10-12-2022

DEPTH FIRST SEARCH OF A GRAPH.

AIM:

To write a C++ program to implement depth first search of a graph.

ALGORITHM:

Step 1: Start

Step 2: Initialize an Empty stack

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

Step 4: push s,v

While (s not empty) do

U:pop s;

Step 5: if(not visited[u]) then

Visited[u] := true;

Step 6: For each unvisited neighbour w of u

Push s,w;

End If.

End While.

Step7: Stop.

AREATI MAHENDRA 102 211061101031


PROGRAM:
#include<iostream.h>
#include<conio.h>
//using namespace std;
int cost[10][10], i, j, k, n, stk[10], top, v, visit[10], visited[10];
int main()
{
int m;
clrscr();
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)
{
visit[j] = 1;

AREATI MAHENDRA 103 211061101031


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

AREATI MAHENDRA 104 211061101031


AREATI MAHENDRA 105 211061101031
OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 106 211061101031


EXPT NO:15 DATE: 10-12-2022

BREADTH FIRST SEARCH OF A GRAPH.

AIM:

To write a C++ program to implement breadth first search.

ALGORITHM:

Step 1: start

Step 2: Construct a graph with inputs from user

Step 3: Fix start node to traverse as s

Step 4: Visit s and find adjacent of s

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

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

Step 7: Stop.

AREATI MAHENDRA 107 211061101031


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<<"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)
{
visit[j]=1;

AREATI MAHENDRA 108 211061101031


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

AREATI MAHENDRA 109 211061101031


OUTPUT:

RESULT:

Thus, the program is executed and the output is verified.

AREATI MAHENDRA 110 211061101031

You might also like