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

DSA Practical Program

The document contains programs to perform various operations on arrays and stacks using both arrays and linked lists. It includes programs to traverse, insert, delete from 1D arrays, find values using linear and binary search, and implement stack operations. It also includes programs to convert infix to postfix notation and evaluate postfix expressions.

Uploaded by

Anuj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

DSA Practical Program

The document contains programs to perform various operations on arrays and stacks using both arrays and linked lists. It includes programs to traverse, insert, delete from 1D arrays, find values using linear and binary search, and implement stack operations. It also includes programs to convert infix to postfix notation and evaluate postfix expressions.

Uploaded by

Anuj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

1. Write a program to traverse 1-d array.

#include<iostream>
using namespace std;
int main()
{

int a[10],i,n;

cout<<"\n enter the value (the size should be less than or equal to 10):";
cin>>n;

if(n>10 ||n<=0 )
{
cout<<"\n size is invalid";
}
else{
cout<<"\n enter the elements";

for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n entered elements are\n";

for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}

return 0;

OUTPUT:

1
2. Write a program of insertion in 1-d array.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[15],i,n,val,pos;
cout<<"enter the size";
cin>>n;
if(n>15||n<=0)
{
cout<<"\n size is invalid";
}
else
{
cout<<"\n enter the element";
for(i=0;i<n;i++){
cin>>a[i];
}
if(n==15)
{
cout<<"\n array is full";
}
else{
cout<<"\n enter the value which you want to insert";
cin>>val;
cout<<"\n enter the position";
cin>>pos;
if(pos<=0||pos>(n+1))
{
cout<<"\n insertion is not possible";
}
else
{
i=n;
while(i>=pos)
{
a[i]=a[i-1];
i=i-1;
}
a[pos-1]=val;
n=n+1;
cout<<"\n after insertion the array elements are";
for(i=0;i<n;i++)
{
cout<< "\t"<<a[i];
}
}
}

2
return 0;
}

OUTPUT:

3
3. Write a program of deletion in 1-d array.
#include<iostream>
using namespace std;
int main()
{
int n,pos,item,i,a[10];
cout<<"\n enter no of elements";
cin>>n;
if(n>10)
{
cout<<"\n size is invalid";
}
else
{
cout<<"\n enter the elements";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n enter the position";
cin>>pos;
if((pos>10 ||pos<1) ||n==0)
{
cout<<"\n deletion is not possible";
}
else{
item=a[pos-1];
for(i=pos-1;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
cout<<"\n deleted value"<<item;
cout<<"\n after deletion elements are"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i];
}
}
}
return 0;
}
OUTPUT:

4
4. Write a program to find a value in 1-d array using linear search.

#include<iostream>
using namespace std;
int main()
{
int a[10],i,n,val,loc;
cout<<"\n enter the size that you want to enter";
cin>>n;
if(n>10 || n<0)
{
cout<<"\n size is invalid";
}
else
{
cout<<"\n enter the elements";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n enter the value that you want to search";
cin>>val;
loc=-1;
i=0;
while(i<n &&a[i]!=val)
{
i++;
}
if(a[i]==val)
{
loc=i+1;
cout<<"\n location="<<loc;
}
else{
cout<<"\n elements not found";
}
}
return 0;
}

OUTPUT:

5
5. Write a program to find a value in 1-d array using binary search.
#include<iostream>
using namespace std;
int main()
{
int a[10],i,n,val,loc,pos;
cout<<"\n enter the size that you want to enter";
cin>>n;
if(n>10 || n<0)
{
cout<<"\n size is invalid";
}
else
{
cout<<"\n enter the elements";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n enter the value that you want to search";
cin>>val;
loc=-1;
i=0;
int beg=0,end=n-1,mid;
while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==val)
{
loc=mid+1;
cout<<"\n location"<<loc;
break;
}
else if(a[mid]>val)
{
end=mid-1;
}
else
{
beg =mid +1;
}
}
if(loc==-1)
cout<<"\n elements not found";
}
return 0;

6
OUTPUT:

7
6. Write a program to implement operation on stack using array.
#include<iostream>
using namespace std;
void push(int a);
void display();
int pop();
int peek();
int top=-1, s[15];
int main()
{
int ch,item;
char c='y';
while(c=='y'){
if(c=='y')
{
cout<<"\n ** stack operation **";
cout<<"\n 1)insertion \n 2)deletion \n 3)topmost value \n 4)display";
cout<<"\n enter your choice";
cin>>ch;
switch(ch){
if(top<15){
case 1:cout<<"\n enter the value";
cin>>item;
push(item);
}
else{
cout<<"\n stack is full";
}
break;
case 2:if(top==-1)
{
cout<<"\n stack is empty";
}
else{
item=pop();
cout<<"\n deletion value ="<<item;
}
break;
case 3:if(top==-1)
{
cout<<"\n stack is empty";
}
else{
item =peek();
cout<<"\n topmost value="<<item;
}
break;
case 4:display();
break;
default: cout<<"\n invalid choice";
8
}
}
else{
cout<<"\n performed operations";
}
cout<<"\n do you again want to perform any operation ? (y/n):";
cin>>c;
}
}
void push( int a)
{
top= top+1;
s[top]=a;
cout<<"\n value is inserted";
}
void display()
{
cout<<"\n values are \n";
for(int i=top;i>=0;i--){
cout<<"\t"<<s[i];
}
}
int pop()
{
int a;
a=s[top];
top=top-1;
return a;
}
int peek()
{
int a;
a=s[top];
return a;
}

9
OUTPUT:

10
7. Write a program to implement operation on stack using linked list.
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *link;
};
Node *top = NULL;
void push(int value)
{
struct Node *newNode;
newNode = new Node;
newNode->data = value;
if (top == NULL)
{
newNode->link = NULL;
}
else
{
newNode->link = top;

}
top = newNode;
cout << "\n Node is inserted";
}
void display()
{
if (top == NULL)
{
cout << "\n stack underflow";
}
else
{
cout << "\n values are :";
struct Node *temp = top;
while (temp->link != NULL)
{
cout << "\t" << temp->data;
temp = temp->link;
}
cout << "\t" << temp->data;
}

}
void pop()
{
if (top == NULL)
{
cout << "\n stack underflow";
11
}
else
{
struct Node *temp = top;
int temp_data = top->data;
top = top->link;
delete temp;
cout << "\n deleted value=" << temp_data;
}
}
void peek()
{
if (top == NULL)
{
cout << "\n stack underflow";
}
else
{
struct Node *temp = top;
int temp_data = top->data;
cout << "\n topmost value=" << temp_data;
}
}
int main()
{
int choice, value;
char ch = 'y';
while (ch == 'y')
{
cout << "\n\t *stack operations*";
cout << "\n 1. push\n 2. pop\n 3. topmost value\n 4. display";
cout << "\n enter your choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n Enter the value to insert:";
cin >> value;
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:

12
cout << "\n wrong choice";
}
cout << "\n do you again want to perform operation again(y/n):";
cin >> ch;
}
return 0;
}

OUTPUT:

13
8. Write a program to convert infix operation into postfix expression.
#include<iostream>
using namespace std;
char p_exp[20];
int top=-1,j=0;
char s[20];
void push(char ch)
{
top=top+1;
s[top]=ch;
}
void pop()
{
if(top==-1)
{
cout<<"\n stack is empty ";
}
else{
if(s[top]!='(')
{
p_exp[j]=s[top];
j=j+1;
}
top=top-1;
}
}
int main()
{
char i_exp[20];
int i=0;
char c;
cout<<"\n enter infix expression and append it with ')'";
cin>>i_exp;
push('(');
while(top>=0)
{
c=i_exp[i];
switch(c)
{
case '+':
case '-':
while(s[top]== '+'||s[top]== '-'||s[top]=='*'||s[top]== '/'||s[top]== '^')
{
pop();
}
push(c);
i++;
break;
case '*':
case '/':
14
while(s[top]== '*'||s[top]== '/'||s[top]== '^')
{
pop();
}
push(c);
i++;
break;
case '(':
push(c);
i++;
break;
case ')':
while(s[top]!='(')
{
pop();
}
pop();
i++;
break;
default :
p_exp[j]=c;
j++;
i++;
}
}
cout<<"\n postfix expression="<<p_exp;
return 0;
}

OUTPUT:

15
9. Write a program to evaluate postfix expression.
#include<iostream>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
void push(int);
void pop();
int top=-1;
int s[10];
int main()
{
string exp;
int ch;
cout<<"\n enter postfix expression";
cin>>exp;
for(int i=0;i<exp.length();i++)
{
ch=int(exp[i]-'0');
if(isdigit(exp[i]))
{
push(ch);
}
else{
int op2=s[top];
pop();
int op1=s[top];
pop();
if(exp[i]=='+')
{
push(op1+op2);
}
else if(exp[i]=='-')
{
push(op1-op2);
}
else if(exp[i]=='*')
{
push(op1*op2);
}
else if(exp[i]=='/')
{
push(op1/op2);
}
}
}
cout<<"\n result="<<s[top];
return 0;
}
void push(int a)
{
16
top=top+1;
s[top]=a;
}
void pop()
{
top=top-1;
}

OUTPUT:

17
10. Write a program to implement queue operations using array.
#include<iostream>
using namespace std;
int arr[10];
int n = 10;
int front = -1;
int rear = -1;
void enqueue(int item){
if(rear == n-1)
{
cout<<"overflow!"<<endl;
return;
}
else{
if(front == -1 && rear == -1){
front=0;
rear=0;
}
else{
rear++;
}
arr[rear] = item;
cout<<"element inserted"<<endl;
}
}
void dequeue(){
if(front == -1 || front > rear){
cout<<"underflow!";
return;
}
else{
int item = arr[front];
cout<<"element deleted from queue is :"<<item<<endl;
if(front == rear){
front=-1;
rear=-1;
}
else{
front++;
}
}
}
void display(){
if(front == -1){
cout<<"queue is empty"<<endl;
return;
}
else{
cout<<"element are:";
for(int i=front;i<=rear;i++)
18
cout<<arr[i]<<"";
cout<<endl;
}
}
void fronte(){
if(front==-1){
cout<<"queue is empty"<<endl;
return;
}
else{
cout<<"front element is :"<<arr[front]<<endl;
}
}
int main(){
int ch;
cout<<"1: inserting element to queue(enqueue)"<<endl;
cout<<"2: deleting element from queue(dequeue)"<<endl;
cout<<"3: display front element to queue"<<endl;
cout<<"4: display all the elements of queue"<<endl;
cout<<"5: exit"<<endl;
do{
cout<<"enter your choice:"<<endl;
cin>>ch;
switch(ch){
case 1: {
cout<<"enter element to be inserted:"<<endl;
int item;
cin>>item;
enqueue(item);
break;
}
case 2: dequeue();
break;
case 3: display();
break;
case 4: fronte();
break;
case 5: cout<<"exit"<<endl;
break;
default: cout<<"invalid choice"<<endl;
}
} while(ch!=5);
return 0;
}

19
OUTPUT:

20
11. Write a program to implement queue operation using linked list.

#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
void Insert() {
int val;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
if (rear == NULL) {
rear = (struct node *)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
} else {
temp=(struct node *)malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
}
void Delete() {
temp = front;
if (front == NULL) {
cout<<"Underflow"<<endl;
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = temp;
} else {
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = NULL;
rear = NULL;
}
}
void Display() {
temp = front;
if ((front == NULL) && (rear == NULL)) {
21
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are: ";
while (temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}

OUTPUT:
22
12. Write a program to traverse singly linked list.
23
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
int printList(node *traverse)
{
if (traverse->next == NULL)
{
return -1;
}
traverse = traverse->next;
printList(traverse);
cout << traverse->data << endl;
return 0;
}
int main()
{
node *head = NULL;
for (int i = 0; i < 10; i++)
{
node *newEntry = new node;
newEntry->data = i;
newEntry->next = head;
head = newEntry;
}
printList(head);
return 0;
}

OUTPUT:

13. Write a program to perform insertion in singly linked list.


#include <iostream>
24
using namespace std;
class Node
{
public:
int data;
Node *next;
};
void insertFront(Node **head, int data)
{
Node *new_node = new Node();
new_node->data = data;
new_node->next = *head;
*head = new_node;
cout << "Inserted Item: " << new_node->data << endl;
}
void printList(Node *node)
{
cout << "\nLinked List : ";
while (node != NULL)
{
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
int main()
{
Node *head = NULL;
insertFront(&head, 4);
insertFront(&head, 5);
insertFront(&head, 6);
insertFront(&head, 7);
insertFront(&head, 8);
insertFront(&head, 9);
printList(head);
return 0;
}

OUTPUT:

14. Write a program to perform deletion in singly linked list.

#include<iostream>
25
using namespace std;
class Node
{
public:
int data;
Node *next;
};
void push(Node **head_ref, int new_data)
{
Node *new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(Node **head_ref, int key)
{
Node *temp = *head_ref;
Node *prev = NULL;
if (temp != NULL && temp->data == key)
{
*head_ref = temp->next;
delete temp;
return;
}
else
{
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
delete temp;
}
}
void printList(Node *node)
{
while (node != NULL)
{
cout << node->data << " ";
node = node->next;
}
}
int main()
{
Node *head = NULL;
push(&head, 17);
push(&head, 11);

26
push(&head, 9);
push(&head, 5);
puts("Created Linked List: ");
printList(head);
deleteNode(&head, 9);
puts("\nLinked List after Deletion of 9: ");
printList(head);
return 0;
}

OUTPUT:

15. Write a program to implement bubble sort.


#include<iostream>
27
using namespace std;
int main()
{
int i,j,temp,pass=0;
int a[10]={10,2,0,14,43,25,18,1,5,45};
cout<<"The array is \n";
for(i=0;i<10;i++)
{
cout<<a[i]<<"\t";
}
cout<<endl;
for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
pass++;
}
cout<<"sorted element list \n ";
for(i=0;i<10;i++)
{
cout<<a[i]<<"\t";
}
cout<<"\n number of passes taken to sort the list:"<<pass<<endl;
return 0;

OUTPUT:

16. Write a program to implement insertion sort.


#include<iostream>
using namespace std;
28
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void insertionSort(int *array, int size) {
int key, j;
for(int i = 1; i<size; i++) {
key = array[i];//take value
j = i;
while(j > 0 && array[j-1]>key) {
array[j] = array[j-1];
j--;
}
array[j] = key; //insert in right place
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
insertionSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
}

OUTPUT:

17. Write a program to implement selection sort.


#include <iostream>
using namespace std;

29
int main()
{
int n, temp, loc, i, j;
cout << "\n enter the size :";
cin >> n;
int arr[n];
cout << "enter the elements:\n";
for (i = 0;i < n; i++)
{
cin >> arr[i];
}
cout << " entered elements are:\n";
for (i = 0; i < n; i++)
{
cout <<"\t" <<arr[i];
}

for (i = 0; i < n - 1; i++)


{
loc = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[loc])
{
loc = j;
}
}
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}

cout << "\n sorted elements\n:";


for (i = 0; i < n; i++)
{
cout << "\t"<<arr[i];
}
return 0;
}

OUTPUT:

18. Write a program to implement merge sort.


#include <iostream>
30
using namespace std;
void Mergesort(int A[], int low, int high);
void Merge(int A[], int Beg, int Mid, int End);

void Mergesort(int A[], int low, int high)


{
if (low < high)
{
int Mid = (low + high) / 2;
Mergesort(A, low, Mid);
Mergesort(A, Mid + 1, high);
Merge(A, low, Mid, high);
}
}

void Merge(int A[], int Beg, int Mid, int End)


{
int temp[8];
int lbindex = Beg;
int rbindex = Mid + 1;
int tbindex = Beg;
while (lbindex <= Mid && rbindex <= End)
{
if (A[lbindex] < A[rbindex])
{
temp[tbindex] = A[lbindex];
lbindex = lbindex + 1;
}
else
{
temp[tbindex] = A[rbindex];
rbindex = rbindex + 1;
}
tbindex = tbindex + 1;
}
while (lbindex <= Mid)
{
temp[tbindex] = A[lbindex];
lbindex = lbindex + 1;
tbindex = tbindex + 1;
}

while (rbindex <= End)


{
temp[tbindex] = A[rbindex];
rbindex = rbindex + 1;
tbindex = tbindex + 1;
}
for (int i = Beg; i <= End; i++)
{

31
A[i] = temp[i];
}
}
int main()
{
int A[] = {2, 8, 6, 11, 13, 45, 19};
int low, high;
int beg = 0;
int end = 6;
Mergesort(A,0,6);
for (int i = 0; i < 7; i++)
{
cout <<" "<<A[i];
}
}

OUTPUT:

19. Write a program to implement quick sort.

32
#include<iostream>
using namespace std;
void swap(int arr[],int pos1, int pos2)
{
int temp;
temp=arr[pos1];
arr[pos1]=arr[pos2];
arr[pos2]=temp;
}
int partition(int arr[], int low, int high, int pivot)
{
int i=low;
int j=low;
while(i<=high)
{
if(arr[i]>pivot)
{
i++;
}
else{
swap(arr,i,j);
i++;
j++;
}
}
return j-1;
}
void quicksort(int arr[],int low, int high)
{
if(low<high)
{
int pivot=arr[high];
int pos=partition(arr,low,high,pivot);
quicksort(arr,low,pos-1);
quicksort(arr,pos+1,high);
}
}
int main()
{
int n;
cout<<"enter the size of array";
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
quicksort(arr,0,n-1);
cout<<"the sorted array is:";
for(int i=0;i<n;i++)

33
{
cout<<arr[i]<<"\t";
}
}

0UTPUT:

20. Write a program to implement heap sort.


34
#include<iostream>
using namespace std;
void maxheapify(int a[],int i ,int n)
{
int j,temp;
temp=a[i];
j=2*i;
while(j<=n)
{
if(j<n && a[j+1]>a[j])
j=j+1;
if(temp>a[j])
break;
else if (temp<=a[j])
{
a[j/2]=a[j];
j=2*j;
}

}
a[j/2]=temp;
return;
}
void heapsort(int a[],int n)
{
int i, temp;
for(i=n;i>=2;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
maxheapify(a, 1, i-1);
}
}
void build_maxheap(int a[], int n)
{
int i;
for(i=n/2;i>=1;i--)
maxheapify(a,i,n);
}
int main()
{
int n,i;
cout<<"\n enter the number of data element to be sorted:";
cin>>n;
n++;
int arr[n];
for(i=1;i<n;i++)
{
cout<<"enter element"<<i<<":";

35
cin>>arr[i];
}
build_maxheap(arr,n-1);
heapsort(arr,n-1);
cout<<"\n sorted data :";
for(i=1;i<n;i++)
cout<<"->"<<arr[i];
return 0;
}

OUTPUT:

36
37

You might also like