Professional Documents
Culture Documents
LAB REPORT
Course name: Data Stucture
Course Code: CSE 2112
Date of submission:
1
serial Experiment Name Page
1 Array Operation. 03
I. Write a C++ program to traverse an Array.
II. Write a C++ program to Insert a new element in Array.
2
EXPERIMENT NO:01(ARRAY OPERATION)
EXPERIMENT NAME: Write a c++ program to traverse an array in array list.
SOLVING TECHNIQUE: To traverse an array means to access each element (item) stored in
the array so that the data can be checked or used as part of a process.
Step 01: Start
Step 06: Stop
Implementation in C++:
#include<iostream>
using namespace std;
int main()
{
int a[100],i,n;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<a[i];
}
return 0;
3
}
INPUT: 5
12345
OUTPUT:1 2 3 4 5
Step 01: Start
Step 03: Stop
Visual Representation:
Implementation in C++:
#include<iostream>
using namespace std;
int main()
{
int a[100],i,n,pos,item;
4
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cin>>item;
cin>>pos;
a[pos-1]=item;
for(i=0;i<n;i++)
{
cout<<a[i];
}
return 0;
}
INPUT: 5
12345
2 3
OUTPUT:1 3 2 3 4 5
5
Implementation in C++:
#include<iostream>
using namespace std;
int main()
{
int a[100],i,n,pos,value;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cin>>pos>>value;
for(i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=value;
for(i=0;i<n+1;i++)
{
cout<<a[i]<<" ";
}
return 0;
6
}
INPUT : 5
12345
2 7
OUTPUT: 1 7 3 4 5
Visual Representation:
Implementation in C++:
#include<iostream>
int main()
int a[100],i,n,pos;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
7
cin>>pos;
for(i=pos-1;i<=n-2;i++)
a[i]=a[i+1];
for(i=0;i<n-1;i++)
cout<<a[i]<<" ";
return 0;
INPUT: 5
12345
OUTPUT:1 2 4 5
Step 1: Start
Step 2: initialization arr[] = {25, 11, 7, 75, 56}
Step 3: length= sizeof(arr)/sizeof(arr[0])
Step 4: max = arr[0]
Step 5: SET i=0. repeat step 6 and step 7 i<length
Step 6: if(arr[i]>max)
Step 7: i=i+1.
Step 8: Print "Largest element in given array:" assigning max.
Step 9: return 0
Step 9: end.
Implementation in C++:
8
#include<iostream>
int main()
int i,n,array[100],item,pos;
cin>>n;
for(i=0;i<n;i++)
cin>>array[i];
for(i=0;i<n;i++)
if(array[0]<array[i])
array[0]=array[i];
return 0;
INPUT: 5
3 5 9 100 4
9
EXPERIMENT NO:02(Search in element)
EXPERIMENT NAME: Write a c++ program for search item in array list by using
linear search.
SOLVING TECHNIQUE: Linear search is also called as sequential search algorithm. It is the
simplest searching algorithm. In Linear search, we simply traverse the list completely and
match each element of the list with the item whose location is to be found. If the match is
found, then the location of the item is returned; otherwise, the algorithm returns NULL.
Now, start from the first element and compare K with each element of the array.
The value of K, i.e., 41, is not matched with the first element of the array. So, move to
the next element. And follow the same process until the respective element is found.
Implementation in C++:
#include<iostream>
10
using namespace std;
int main()
{
int a[100],i,item,n;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cin>>item;
for(i=0;i<n;i++)
{
if(a[i]==item)
{
cout<<"item is found="<<item<<" "<<"position="<<i+1;
break;
}
}
if(n==i)
{
cout<<"item is not found";
}
return 0;
}
INPUT: 5
11
12345
10
OUTPUT: item is not found
EXPERIMENT NAME: Write a c++ program for search item in array list by
using binary search.
SOLVING TECHNIQUE:
We have to use the below formula to calculate the mid of the array -
1. mid = (beg + end)/2
beg = 0
end = 8
Implementation in C++:
#include<iostream>
int main()
12
int a[100],i,n,beg,lst,mid,item;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
beg=0;
lst=n-1;
mid=(beg+lst)/2;
cin>>item;
while(beg<=lst)
if(a[mid]<item)
beg=mid+1;
else if(a[mid]==item)
break;
else
lst=mid-1;
mid=(beg+lst)/2;
if(beg>lst)
13
return 0;
INPUT: 8
10 12 24 29 39 40 56 37
56
EXPERIMENT NO:03(SORTING)
EXPERIMENT NAME: Write a c++ program to sort array element by busing
bubble sort.
SOLVING TECHNIQUE: Bubble sort is a data sorting algorithm that works by randomly copying
elements from the first array into a smaller second array, and then reversing the order of
these arrays.
14
Hence, there is no swapping required, so the array is completely sorted.
Implementation in C++:
#include<iostream>
int main()
15
int a[100],i,j,n;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
INPUT: 7
9351287
OUTPUT: 1 2 3 5 7 8 9
16
EXPERIMENT NAME: Write a c++ program to sort array element by busing
insertion sort.
SOLVING TECHNIQUE: Insertion sort is a simple sorting algorithm that works similar to the
way you sort playing cards in your hands. The array is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the correct position
in the sorted part.
Implementation in C++:
#include<iostream>
using namespace std;
int main()
{
int n,a[100],i,j;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
int temp=a[i];
17
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j]=a[j+1];
j--;
}
a[j+1]=temp;
}
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
INPUT: 5
85201
OUTPUT: 0 1 2 5 8
18
19
Implementation in C++:
#include <iostream>
using namespace std;
int partition(int arr[], int start, int end)
{
int pivot = arr[start];
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
int pivotIndex = start + count;
swap(arr[pivotIndex], arr[start]);
int i = start, j = end;
while (i < pivotIndex && j > pivotIndex) {
while (arr[i] <= pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
20
}
void quickSort(int arr[], int start, int end)
{
if (start >= end)
return;
int p = partition(arr, start, end);
quickSort(arr, start, p - 1);
quickSort(arr, p + 1, end);
}
int main()
{
int arr[100],n;
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
input: 5
52341
output:1 2 3 4 5
21
EXPERIMENT NO:04(String processing)
EXPERIMENT NAME: Write a c++ program to find string length by using
strlen() function.
SOLVING TECHNIQUE:to solve the problem i used strln() function to find out length of a
string.
Implementation in C++:
#include<bits/stdc++.h>
int main()
char str[10];
gets(str);
return 0;
input:nusrat
output:6
EXPERIMENT NAME: Write a c++ program combine two string by using
concatenation function.
SOLVING TECHNIQUE: To solve the problem i used strcat() function to combine two string
by using concatnation function.
Implementation in C++:
#include<bits/stdc++.h>
int main()
char s1[10],s2[34];
gets(s1);
22
gets(s2);
return 0;
input :nusrat
jahan
output: nusrtjahan
EXPERIMENT NAME: Write a c++ program to find substring of string by using substring
function.
SOLVING TECHNIQUE: To solve the problem i used strsub() function to find out substring
of a string.
Implementation in C++:
#include<bits/stdc++.h>
int main()
cin>>str;
int start,pos;
cin>>start;
cin>>pos;
string s =str.substr(start,pos);
cout<<s;
return 0;
input: nusrat 22
output: us
23
EXPERIMENT NAME: Write a c++ program to find index of a substring .
SOLVING TECHNIQUE: To solve the problem i used index function to find out index from
substring of a string.
Implementation in C++:
#include<bits/stdc++.h>
int main()
string str;
cin>>str;
int pos;
cin>>pos;
cout<<str[pos];
input: nusrat 2
output:u
24
EXPERIMENT NO:05(Word processing)
EXPERIMENT NAME: Write a c++ program replacement a word from a line by
using replace function.
SOLVING TECHNIQUE: To solve the problem i used strreplace() function to replace word
from a line .
Implementation in C++:
#include<iostream>
using namespace std;
int main()
{
string str1 = "This is C language";
string str2 = "C++";
str1.replace(8,1,str2);
cout << "string is :"<<str1<<'\n';
return 0;
}
#include<iostream>
using namespace std;
int main()
25
string str1= "java tutorial";
cout<<"String contains :" <<str1<<'\n';
cout<<"After insertion, String value is :"<<str1.insert(5,"point");
return 0;
}
EXPERIMENT NAME: Write a c++ program delete a word from a line by using
delete function.
SOLVING TECHNIQUE: To solve the problem i used strdelete() function to replace word
from a line .
Implementation in C++:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
char filename[] = "program.cpp";
return 0;
}
Each element in a linked list is called a node. A single node contains data and a pointer to
the next node, which helps to maintain the structure of the list.
26
The head is a pointer that points to the first node of the list and, hence, helps us traverse
the list. The last node points to nullptr, which helps us in determining when the list ends.
Implementation in C++:
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;
}
output: 3 1 7 2 9
27
Implementation in C++:
#include <bits/stdc++.h>
class Node
public:
int data;
Node *next;
Node *prev;
};
newnode->data = newdata;
newnode->prev = NULL;
newnode->next = (*head);
if((*head) != NULL)
(*head)->prev = newnode ;
(*head) = newnode;
28
{
while(head != NULL)
head = head->next;
int main()
push(&head, 7);
push(&head, 5);
push(&head, 3);
push(&head, 2);
printlist(head);
return 0;
output:7 5 3 2
30
EXPERIMENT NO:06(stack and queue)
EXPERIMENT NAME: Write a c++ program stack ( push, pop, display) in data
structure.
Implementation in C++:
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int value) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=value;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
31
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Output
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit
Enter choice: 1
Enter value to be pushed: 2
Enter choice: 1
Enter value to be pushed: 6
Enter choice: 1
32
Enter value to be pushed: 8
Enter choice: 1
Enter value to be pushed: 7
Enter choice: 2
The popped element is 7
Enter choice: 3
Stack elements are:8 6 2
Enter choice: 5
Invalid Choice
Enter choice: 4
Exit
33
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
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
1) Insert element to queue
2) Delete element from queue
3) Display all the elements of queue
4) Exit
Enter your choice : 1
Insert the element in queue : 4
Enter your choice : 1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
34
Queue elements are : 3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit
35