Data structure & Algorithms Lab
Share more and more And comment for help
Related Subjects:
Getting tools for programs
Download Dev CPP application to run files. ( For Pc) Download DevCpp
Download Dev CPP application to run files. ( For Mobile) App link
Online Compiler for c++. Compiler Link
List of Experiment:
Task 1: Write a program to insert a new element at the end as well as at a given position in an
array. Download Cpp File
Task 2: Write a program to delete an element from a given whose value is given or
whose position is given.
i. With value Download Cpp File
ii.With position Download Cpp File
Task 3: Write a program to find the location of a given element using Linear Search. Download Cpp File
Task 4: Write a program to find the location of a given element using Binary Search.
Task 5: Write a program to implement push and pop operations on a stack using a linear
array.
Task 6: Write a program to convert an infix expression to a postfix expression using stacks.
Task 7: Write a program to evaluate a postfix expression using stacks.
Task 8: Write a recursive function for the Tower of Hanoi problem.
Task 9: Write a program to implement insertion and deletion operations in a
queue using a linear array.
Task 10:Write a menu-driven program to perform the following insertion
operations in a single linked list:
i. Insertion at the beginning
ii. Insertion at the end
iii. Insertion after a given node
iv. Traversing a linked list
Task 11:Write a menu-driven program to perform the following deletion operations
in a single linked list:
i. Deletion at beginning
ii. Deletion at end
iii. Deletion after a given node
Task 12: Write a program to implement push and pop operations on a stack using a linked list.
Task 13: Write a program to implement push and pop operations on a queue using linked
list.
Task 14:Program to sort an array of integers in ascending order using bubble sort.
Task 15:Program to sort an array of integers in ascending order using selection sort.
Task 16: Program to sort an array of integers in ascending order using insertion sort.
Task 17:Program to sort an array of integers in ascending order using quicksort.
Task 18: Program to traverse a Binary search tree in Pre-order, In-order, and Post-order.
Task 19: Program to traverse graphs using BFS.
Task 20: Program to traverse graphs using DFS.
Programs:
Task 1: Write a program to insert a new element at the end as well as at a given position in an array.
Program:
#include<iostream>
using namespace std;
int main()
{int i,a[50],no,pos,size;
cout<<"Enter array size( Max:50 ): ";
cin>>size;
cout<<"\nEnter array elements:\n";
for(i=0; i<size; i++)
{cout<<"\nEnter arr["<<i<<"] Element,position here["<<i+1<<"] :";
cin>>a[i];}
cout<<"\nStored Data in Array :\n\n";
for(i=0;i<size;i++)
{cout<<" "<<a[i]<<" ";}
cout<<"\n\nEnter position to insert number: ";
cin>>pos;
if(pos>size)
{cout<<"\nThis is out of range.\n"; }
else
{ cout<<"\nEnter number to be inserted : ";
cin>>no;
--pos;
for(i=size;i>=pos;i--)
{ a[i+1]=a[i]; }
a[pos]=no;
cout<<"\nNew Array is:\n\n";
for(i=0;i<size+1;i++)
{ cout<<" "<<a[i]<<" ";} }
cout<<"\n";
return 0; }
Output:
Task 2: Write a program to delete an element from a given whose value is given or whose position is given.
i. With the value
Program:
/* C++ Program - Delete Element from Array */
#include<iostream>
using namespace std;
main()
{int arr[50], size, i, del, count=0;
cout<<"Enter array size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{cin>>arr[i];}
cout<<"Enter element to be delete : ";
cin>>del;
for(i=0; i<size; i++)
{if(arr[i]==del)
{for(int j=i; j<(size-1); j++)
{arr[j]=arr[j+1];}
count++;
break;}}
if(count==0)
{cout<<"Element not found..!!";}
else
{cout<<"Element deleted successfully..!!\n";
cout<<"Now the new array is :\n";
for(i=0; i<(size-1); i++)
{cout<<arr[i]<<" ";}}
return 0;}
Output:
Program:
#include <iostream>
using namespace std;
main() { int a[100], size, pos, i, count = 0;
cout << "Enter the size of an array \n";
cin >> size;
cout << "Enter the value in an array \n";
// Take an input array
for (i = 0; i < size; i++) {
cin >> a[i];}
//Input position where we delete an element
cout << "Enter the position ";
cin >> pos;
//Shift element from i+1 to i
for(i = pos-1; i < size; i++) {
a[i] = a[i+1]; }
// Reduce the size of an array
size--;
// Print an array after deleting an element
for(i = 0; i < size; i++) {
cout<<" "<<a[i]; }
return 0;}
Output:
Task 3: Write a program to find the location of a given element using Linear Search.
Program:
#include<iostream>
using namespace std;
int main()
{int a[20],n,x,i,flag=0;
cout<<"How many elements:\n";
cin>>n;
cout<<"\nEnter elements of the array\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter element to search:";
cin>>x;
for(i=0;i<n;++i)
{if(a[i]==x)
{flag=1;
break;}}
if(flag)
cout<<"\nElement is found at position "<<i+1;
else
cout<<"\nElement not found";
return 0;}
Output:
Task 4: Write a program to find the location of a given element using Binary Search. ( Data should be entered in ascending or descending order.)
Program:
#include <iostream>
using namespace std;
int main()
{int count, i, arr[30], num, first, last, middle;
cout<<"How many elements would you like to enter:";
cin>>count;
for (i=0; i<count; i++)
{cout<<"Enter number "<<(i+1)<<": ";
cin>>arr[i];}
cout<<"Enter the number that you want to search:";
cin>>num;
first = 0;
last = count-1;
middle = (first+last)/2;
while (first <= last)
{if(arr[middle] < num)
{first = middle + 1; }
else if(arr[middle] == num)
{cout<<num<<" found in the array at the location "<<middle+1<<"\n";
break; }
else { last = middle - 1; }
middle = (first + last)/2; }
if(first > last)
{cout<<num<<" not found in the array";}
return 0;}
Output:
Comments
Post a Comment