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

Labwork.cpp (1)

Uploaded by

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

Labwork.cpp (1)

Uploaded by

tasriftanim2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Name of Lab Report: Data Structure

Submitted By: Submitted To:


Student Name: Md. Tanim Ahmed Teachers name: Maliha Hossain
Full Id : 41230301391 Lecturer , Dept of CSE
Couse Name : Data Structure Northern University Bangladesh
Couse code : CSE 2111

Student Sign: Lecturer sign:


Lab Work 1:
1. Write a C++ program to take an array of 10 elements and add 5 with each of the
elements .Print the array before and after adding 5.

#include<iostream>

using namespace std;

int main()

int array[10],n,i;

cout<<"Enter array size < 10:"<<endl;

cin>>n;

cout<<"Enter array elements:"<<endl;

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

cin>>array[i];

cout<<endl;

cout<<"Before adding :"<<endl;

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

cout<<array[i]<<" ";

cout<<"After adding 5 with each elements:"<<endl;

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

cout<<array[i]+5<<" ";

}
2. Write a C++ program to insert an element in an array at any index(you must taken the
index number from user) and print the array after inserting the element. The maximum
size of the array is 10.
Sample Input: 10 5 23 11 7 17
Sample Output: 10 5 23 33 11 7 17
#include<iostream>

using namespace std;

int main()

int array[20],n,pos,i,num;

cout<<"Enter the length of the array:"<<endl;

cin>>n;

cout<<"Enter array elements:"<<endl;

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

cin>>array[i];

}
cout<<"Enter insertion position:"<<endl;

cin>>pos;

cout<<"Enter insertion number:"<<endl;

cin>>num;

for(i=n-1;i>=pos-1;i--)

array[i+1]=array[i];

n++;

array[pos-1]=num;

cout<<"Inserted array:"<<endl;

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

cout<<array[i]<<" ";

}
}
3. Write a C++ program to delete an element from an array at any index(you must taken
the index number from user) and print the array after deleting the element. The maximum
size of the array is 10.

Sample Input: 10 5 23 11 7 17
Sample Output: 10 23 11 7 17

#include<iostream>
using namespace std;
int main()
{
int array[20],n,i,pos,num;
cout<<"Enter the length of the array:"<<endl;
cin>>n;

cout<<"Enter array elements:"<<endl;

for(i=0;i<n;i++)
{
cin>>array[i]; // 1 2 3 4 5 6 7 8 9
}

cout<<"Enter deletation position:"<<endl;


cin>>pos;

for(i=pos-1;i<n;i++)
{
array[i]=array[i+1];
}
n--;

for(i=0;i<n;i++)
{
cout<<array[i]<<" "; }

}
Lab work 2:

1.Write a C program to complete the linear search algorithm.

#include<iostream>
using namespace std;
int main()
{
int array[20],n,i,num;

cout<<"Enter the length of the array:"<<endl;


cin>>n;
cout<<"Enter array elements:"<<endl;

for(i=0;i<n;i++)
{
cin>>array[i]; // 1 2 3 4 5 6 7 8 9
}
cout<<"Enter the number you want to search:"<<endl;

cin>>num;
for(i=0;i<n;i++)
{
if(array[i]==num)
{

cout<<"Element found at index:"<<i<<endl;


break;
}
}
if(i==n)
{
cout<<"Element no found in the array"<<endl;
}
}
2. Write a C program to complete the Binary search algorithm by taking a random array
of 10 inputs. At first sort the array by using Bubble sort then perform Binary Search.
#include<iostream>
using namespace std;

int array[10],n,num,i;
int binarysearch(int low,int high)
{
while(low<=high)
{

int mid=(low+high)/2;
if(array[mid]==num)
{
return mid;
}
else if(num >array[mid])
{
low=mid+1;

}
else
{
high=mid-1;
}

return -1;
}

void bubblesort(int array[10],int n)


{
int i,j,temp;
for(i=0;i<n-1;i++)
{
int flag=0;
for(j=0;j<n-1-i;j++)
{
if(array[j]>array[j+1])

{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
flag=1;
}
}

if(flag==0)
{
break;
}
}

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

}
int main()
{
cout<<"Enter the size of the array:"<<endl;
cin>>n;
cout<<"Enter array elements:"<<endl;

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


{
cin>>array[i];

}
bubblesort(array,n);
cout<<"\n Enter the number to search in the array:"<<endl;
cin>>num;
int result= binarysearch(0,n-1);

if(result == -1)
{
cout<<"Elements not found"<<endl;
}
else

{
cout<<"Array found at index:"<<result<<endl;
}

}
Lab work 3:
1. Complete the code of insertion sort and selection sort by taking input from the users .

Insertion sort:

#include <iostream>
using namespace std;
void insertionSort(int array[10], int n) {
for(int i=1;i<n;i++)
{

int current=array[i];
int j=i-1;
while(array[j]>current && j>=0)
{
array[j+1]=array[j];

j--;
}
array[j+1]=current;
}
}
void display(int array[10], int n) {
for (int i = 0; i < n; i++)
cout << array[i] << " ";
cout << endl;
}

int main() {
int array[10],i,n;
cout<<"Enter array size:"<<endl;
cin>>n;
cout<<"Enter array elements:"<<endl;
for(i=0;i<n;i++)

{
cin>>array[i];
}
insertionSort(array, n);

cout << "Sorted array: \n";


display(array, n);
return 0;
}

Selection sort:
#include <iostream>
using namespace std;

void selectionsort(int array[10], int n) {


for(int i=0;i<n-1;i++)
{
int min =i;
int j=i-1;

for(int j=i+1;j<n;j++)
{
if(array[min]>array[j])
{
min=j;

}
}
int temp = array[i];
array[i]=array[min];
array[min]=temp;
}
}
void display(int array[10], int n) {
for (int i = 0; i < n; i++)
cout << array[i] << " ";

cout << endl;


}

int main() {
int array[10],i,n;
cout<<"Enter array size:"<<endl;
cin>>n;

cout<<"Enter array elements:"<<endl;


for(i=0;i<n;i++)
{
cin>>array[i];
}

selectionsort(array, n);

cout << "Sorted array: \n";


display(array, n);
return 0;

}
Lab work 4: Write down a program to traverse a link
list by taking values from users.
Sample Input:
How many nodes user want to insert: 5 (then the link
list will take 5 values from the user)
Sample output:
Shows the values as output.

#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
node(int value)
{
data=value;
next=NULL;
}

};

int main()
{
int n,value;
cout<<"Enter how much number you want to add:"<<endl;
cin>>n;
node* head=NULL;
cout<<"Enter your desire value:"<<endl;
node* tail;
for(int i=0; i<n; i++)
{
cin>>value;
node* one= new node(value);

if(head==NULL)
{
head=one;
tail=head;
}
else
{
node* temp;
temp=new node(value);
tail=tail->next=temp;
}

}
node* sum;
sum=head;
while(sum!=NULL)
{
cout<<sum->data<<" ";
sum=sum->next;
}
}

LAB WORK 5:

1. Complete the link list insert and delete operation. You have to write the code and understand
that.
2. You are given the heads of two sorted linked lists list1 and list2 in the
below-
Input: list1= [1,3,5] & list2= [2,4,6]
Output: [1,2,3,4,5,6]
Write down the C++ code to merge these two sorted link list.
All of you must complete these code. I suggest to give some effort.
Task 1:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
node(int value)
{
data=value;
next =NULL;
}
};

class linkedlist
{
public:
node* head;
linkedlist()
{
head=NULL;
}
int value;
void insert_at_the_beginning(int value)
{
node* one=new node(value);
if(head==NULL)
{
head=one;
}
else
{
node* temp;
temp=new node(value);
temp->next=head;
head=temp;
}
}

void insert_at_the_end(int value)


{
node* one=new node(value);
if(head==NULL)
{
head=one;
}
else
{
node* temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=one;
}
}

void insert_at_any_position(int value,int position)


{
if(position<0)
{
cout<<"Invalid position"<<endl;
return;
}
if(position==0)
{
insert_at_the_beginning(value);
return;
}
node* temp;
temp=head;

for(int i=0; i<position-1; i++)


{
if(head==NULL)
{
cout<<"Invalid"<<endl;
return;
}
temp=temp->next;
}
node* one;
one=new node(value);
one->next=temp->next;
temp->next=one;

void delete_at_the_head()
{
if(head==NULL)
{
cout<<"Linked list is empty"<<endl;
return;
}
else
{
node* todelete=head;
head=head->next;
delete todelete;
}
}

void delete_at_any_position(int position)


{

if (position < 0)
{
cout << "Invalid position" << endl;
return;
}
if (position == 0)
{
delete_at_the_head();
return;
}

node* temp = head;


for (int i = 0; i < position - 1; i++)
{
if (temp == NULL || temp->next == NULL)
{
cout << "Position out of bounds" << endl;
return;
}
temp = temp->next;
}
node* todelete = temp->next;
temp->next = temp->next->next;
delete todelete;

void print()
{
node *print;
print=head;
while(print!=NULL)
{
cout<<print->data<<" -> ";
print=print->next;
}
cout<<"NULL";
}
};
int main()
{
linkedlist link;

int n;
cout<<"Enter the number of nodes:"<<endl;
cin>>n;

cout<<"Enter the values:"<<endl;


for(int i=0; i<n; i++)
{
int value;
cin>>value;
link.insert_at_the_end(value);
}

int position,value;
cout<<"Enter position:"<<endl;
cin>>position;
cout<<"Enter the number you want to insert:"<<endl;
cin>>value;
link.insert_at_any_position(value,position);

link.print();

cout<<"\nEnter the position you want to delete:"<<endl;


cin>>position;

link.delete_at_any_position(position);
link.print();
}
Task 2:
You are given the heads of two sorted linked lists list1 and list2 in
the below-
Input: list1= [1,3,5] & list2= [2,4,6]
Output: [1,2,3,4,5,6]
Write down the C++ code to merge these two sorted link list.
All of you must complete these code. I suggest to give some
effort.

#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
node(int value)
{
data=value;
next=NULL;
}
};

node* mergetwosort(node* head1,node* head2)


{
node* head=new node(0);
node* tail=head;
while(head1!=NULL && head!=NULL)
{
if(head1->data<=head2->data)
{
tail->next=head1;
head1=head1->next;
tail=tail->next;
tail->next=NULL;
}
else
{
tail->next=head2;
head2=head2->next;
tail=tail->next;
tail->next=NULL;
}
}

if(head1!=NULL)
{
tail->next=head1;
}
else
{
tail->next=head2;
}
node* temp=head;
head=head->next;
delete temp;
return head;
}
int main()
{
node* head1=new node(1);
head1->next=new node(3);
head1->next->next=new node(5);

node* head2=new node(2);


head2->next=new node(4);
head2->next->next=new node(6);

node* temp=mergetwosort(head1,head2);
cout<<"Merged linked list is:"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<" -> ";
temp=temp->next;
}
cout<<"NULL"<<endl;
}

You might also like