You are on page 1of 35

Department of Computer Science and Engineering

Bangamata Sheikh Fojilatunnesa Mujib Science & Technology


University

LAB REPORT
Course name: Data Stucture
Course Code: CSE 2112

Submitted by: Submitted to:


MD Saifullah Azad Sujit Roy
ID: 21111112, Session:2020-21 Assistant Professor
Dept. of Computer Science &Engineering Dept. of Computer Science &Engineering
Bangamata Sheikh Fojilatunnesa Mujib Bangamata Sheikh Fojilatunnesa Mujib
Science & Technology University, Science & Technology University,
Jamalpur Jamalpur

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.

III. Write a C++ program to Delete an element of Array.


IV. Write a C++ program to update an element of Array

V. Write a C++ program to find largest and Smallest number of


an Array.
2 Searching in Data Structure. 10
I. Write a C++ program to search an array element using Linear
search technique.
II. Write a C++ program to search an array element using Binary
search technique.
3 Sorting in Data Structure. 14

I. Write a C++ program to sort an array element using Insertion


sort technique.
II. Write a C++ program to sort an array element using Quick
sort technique.
4 String processing in Data Structure 22
I. Write a C++ program to concatenate two string.

II. Write a C++ program to find length of a string.


III. Write a C++ program to find index of a Sub-String from a
String.
IV. Write a C++ program to find Sub-String from a String.
5 Word processing in Data structure. 25
I. Write a C++ program to Replace a word from a line.
II. Write a C++ program to Insert a word from a line.

III. Write a C++ program to Delete a word from a line.


6 Stack and Queue in Data Structure 31
I. Write a C++ program to implement Stack operation.
II. Write a C++ program to implement Queue operation.

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 02: [Initialize counter variable. ] Set i = LB.

Step 03: Repeat for i = LB to UB.

Step 04: Apply process to arr[i].

Step 05: [End of loop. ]

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

EXPERIMENT NAME: Write a c++ program to update array in array list.


SOLVING TECHNIQUE: The process of updating (modifying/changing) an existing element
of an array at a specified index with another element is called updating an array or update
operation in array.
ALGORITHM

Step 01: Start

Step 02: Set a[pos-1] = x

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

EXPERIMENT NAME: Write a c++ program to insert an element in array list.


SOLVING TECHNIQUE: In the insertion operation, we are adding one or more elements to the
array. Based on the requirement, a new element can be added at the beginning, end, or any
given index of array.

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

EXPERIMENT NAME: Write a c++ program to delete an element in array list.


SOLVING TECHNIQUE: Deletion operation in Array or delete an element from an Array simply
means, Removing an existing element from a specific position of an array.

Visual Representation:

Implementation in C++:
#include<iostream>

using namespace std;

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

EXPERIMENT NAME: Write a c++ program to find large array element in


array list.
SOLVING TECHNIQUE: To find the largest element from the array, a simple way is to
arrange the elements in ascending order. After sorting, the first element will represent the
smallest element, the next element will be the second smallest, and going on, the last
element will be the largest element of the array.

 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>

using namespace std;

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

cout<<" the large number is="<<array[0];

return 0;

INPUT: 5

3 5 9 100 4

OUTPUT: 100 is largest number.

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.

Let the elements of array are -

Let the element to be searched is K = 41

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:

Let the elements of array are -

Let the element to search is, K = 56

We have to use the below formula to calculate the mid of the array -

1. mid = (beg + end)/2  

So, in the given array -

beg = 0

end = 8

mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.

Implementation in C++:
#include<iostream>

using namespace std;

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)

cout<<"item is not found="<<item<<" "<<"pos="<<mid+1;

break;

else

lst=mid-1;

mid=(beg+lst)/2;

if(beg>lst)

cout<<"item is not found";

13
return 0;

INPUT: 8
10 12 24 29 39 40 56 37

56

OUTPUT: item is found

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.

let's see the working of Bubble sort Algorithm.

Let the elements of array are -

14
Hence, there is no swapping required, so the array is completely sorted.

Implementation in C++:

#include<iostream>

using namespace std;

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

EXPERIMENT NAME: Write a c++ program to sort array element by busing


quick sort.
SOLVING TECHNIQUE: Quicksort picks an element as pivot, and then it partitions the
given array around the picked pivot element. In quick sort, a large array is divided into
two arrays in which one holds values that are smaller than the specified value (Pivot),
and another array holds the values that are greater than the pivot.

Let the elements of array are -

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>

using namespace std;

int main()

char str[10];

gets(str);

cout<<" the length is ="<< strlen(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>

using namespace std;

int main()

char s1[10],s2[34];

gets(s1);

22
gets(s2);

cout<<"concatination function is ="<<strcat(s1,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>

using namespace std;

int main()

string str=" ";

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>

using namespace std;

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;  

}  

output: string is: This is c++ language

EXPERIMENT NAME: Write a c++ program insert a word from


a line by using insert function.
SOLVING TECHNIQUE: To solve the problem i used string-insert() function to replace word
from a line .
Implementation in C++:

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

output :java point tutorial  

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

int result = delete(filename);

cout << result;

return 0;
}

EXPERIMENT NO:05(Linked list)


EXPERIMENT NAME: Write a c++ program in single linked list.
SOLVING TECHNIQUE: A singly linked list is a type of linked list that is unidirectional,
i.e., it can be traversed in only one direction from the head to the last node.

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

EXPERIMENT NAME: Write a c++ program in doubly linked


list.
SOLVING TECHNIQUE: Doubly linked list is a data structure which consists of nodes which
have data, a pointer to the next node, and also a pointer to the previous node

27
Implementation in C++:

#include <bits/stdc++.h>

using namespace std;

class Node

public:

int data;

Node *next;

Node *prev;

};

void push(Node** head, int newdata)

Node* newnode = new Node();

newnode->data = newdata;

newnode->prev = NULL;

newnode->next = (*head);

if((*head) != NULL)

(*head)->prev = newnode ;

(*head) = newnode;

void printlist(Node *head)

28
{

while(head != NULL)

cout << head->data << " ";

head = head->next;

int main()

Node* head = NULL;

push(&head, 7);

push(&head, 5);

push(&head, 3);

push(&head, 2);

cout << "Created Doubly Linked list:" << endl;

printlist(head);

return 0;

output:7 5 3 2

EXPERIMENT NAME: Write a c++ program in circular linked


list.
SOLVING TECHNIQUE: A circular linked list is a type of linked list in which the first and the
last nodes are also connected to each other to form a circle.
Implementation in C++:
29
#include <iostream>
using namespace std;
struct Node {
   int data;
   struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata) {
   struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
   struct Node *ptr = head;
   newnode->data = newdata;
   newnode->next = head;
   if (head!= NULL) {
      while (ptr->next != head)
      ptr = ptr->next;
      ptr->next = newnode;
   } else
   newnode->next = newnode;
   head = newnode;
}
void display() {
   struct Node* ptr;
   ptr = head;
   do {
      cout<<ptr->data <<" ";
      ptr = ptr->next;
   } while(ptr != head);
}
int main() {
   insert(3);
   insert(1);
   insert(7);
   insert(2);
   insert(9);
   cout<<"The circular linked list is: ";
   display();
   return 0;
}
output:3 1 7 2 9

30
EXPERIMENT NO:06(stack and queue)

EXPERIMENT NAME: Write a c++ program stack ( push, pop, display) in data
structure.

SOLVING TECHNIQUE: A stack is an abstract data structure that contains a collection


of elements. Stack implements the LIFO mechanism i.e. the element that is pushed at the
end is popped out first. Some of the principle operations in the stack are −

 Push - This adds a data value to the top of the stack.


 Pop - This removes the data value on top of the stack

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

EXPERIMENT NAME: Write a c++ program queue


(rear,front,display)operation in data structure.
SOLVING TECHNIQUE: A queue is an abstract data structure that contains a collection of
elements. Queue implements the FIFO mechanism i.e. the element that is inserted first
is also deleted first. In other words, the least recently added element is removed first in
a queue.
Implementation in C++:
#include <iostream>
using namespace std;
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert() {
   int val;
   if (rear == n - 1)
   cout<<"Queue Overflow"<<endl;
   else {
      if (front == - 1)
      front = 0;
      cout<<"Insert the element in queue : "<<endl;
      cin>>val;
      rear++;
      queue[rear] = val;
   }
}
void Delete() {
   if (front == - 1 || front > rear) {
      cout<<"Queue Underflow ";
      return ;
   } else {
      cout<<"Element deleted from queue is : "<< queue[front] <<endl;
      front++;;
   }
}
void Display() {

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

You might also like