Ds Final Lab Report
Ds Final Lab Report
DATA STRUCTURE
CSE 156
Date of Submission: 15 February, 2023
Submitted To
Dr. Md. Golam Moazzam
Professor
Department of Computer Science and Engineering
Jahangirnagar University
Submitted By
Zakia Binta Syeed
Registration No: 20220655008
Class Roll: 402
Session: 2021-2022
Department of Computer Science and Engineering
Jahangirnagar University
Jahangirnagar University
Savar, Dhaka-1342
Data Structure Lab
Linear Search
Binary Search
String Operation
Menu Driven
(i)Concatenation
(ii)Length
(iii) Indexing
(iv)Substring
First Pattern Matching Algorithm,
Second Pattern Matching Algorithm
Array Operation
(i)Create
(ii)Insertion
(iii)Deletion
Linked List
Menu Driven
(i)Create
(ii)Insert
(iii)Delete
(iv)Search
Stack
(i)Menu Driven (push, pop)
(ii)Infix to Postfix
(ii)Infix to Postfix Evaluation
Quick Sort
Queue (push, pop)
Tree Menu Driven
Create a binary tree by taking input from user
Output In order Traversal of that tree
Graph by taking input from user
Find Out indegree and outdegree of all vertices in a graph
Find Path Matrix
Find Shortest Path Matrix
1.Linear Search
Code:
#include <iostream>
using namespace std;
// Driver code
int main()
{
int N ,x ;
cout<<"How many elements do you want in an array : ";
cin>> N ;
int arr[N];
cout<<"\nEnter "<<N<<" elements : " ;
for(int i=0 ; i<N ; i++)
cin>>arr[i];
cout<<"\nEnter Your searching elements : " ;
cin>> x ;
cout<<endl ;
// Function call
int result = search(arr, N, x);
(result == -1)
? cout << "Element is not present in array\n\n"
: cout << "Element is present at index " << result<<endl ;
return 0;
}
#include <iostream>
using namespace std;
cout<<endl ;
max1= arr[0];
if(arr[i]>max1){
max2=max1 ;
max1=arr[i];
}
else if(arr[i]>max2 && arr[i]<max1){
max2=arr[i];
}
return 0 ;
}
Input and Output :
Code:
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
if (swapped == false)
break;
}
}
int main()
{
int N ;
cout<<"How many elements do you want in an array : ";
cin>> N ;
int arr[N];
cout<<"\nEnter "<<N<<" elements : " ;
for(int i=0 ; i<N ; i++)
cin>>arr[i];
cout<<endl ;
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
cout<<"\nFirst largest number : "<<arr[N-1]<<endl ;
cout<<"\nSecond largest number : "<<arr[N-2]<<endl ;
return 0;
}
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1, str2, result;
int choice, index, length;
do {
cout << "String Operation Menu:" << endl;
cout << "1. Concatenation" << endl;
cout << "2. Length" << endl;
cout << "3. Indexing" << endl;
cout << "4. Substring" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1: cout<<"\n\t\tInput\n"<<endl ;
cout << "Enter string 1: ";
cin >> str1;
cout << "Enter string 2: ";
cin >> str2;
result = str1 + str2;
cout<<"\n\t\tOutput\n"<<endl ;
cout << "Concatenation string: " << result << endl;
break;
case 2:
cout<<"\t\tInput\n"<<endl ;
case 3:
cout<<"\n\t\tInput\n"<<endl ;
cout << "Enter the string: ";
cin >> str1;
cout << "Enter the index: ";
cin >> index;
if (index >= 0 && index < str1.length()) {
cout<<"\n\t\tOutput\n"<<endl ;
cout << "Character at index " << index << ": " << str1[index] << endl;
} else {
cout<<"\n\t\tOutput\n"<<endl ;
cout << "Invalid index!" << endl;
}
break;
case 4:
cout<<"\n\t\tInput\n"<<endl ;
Code
#include <iostream>
#include <string>
int main() {
string text, pattern;
cout << "Enter the text: ";
getline(cin, text);
cout << "Enter the pattern: ";
getline(cin, pattern);
return 0;
}
Input and Output :
5.(ii) Second pattern matching algorithm code
Code:
#include <iostream>
#include <string>
#include <vector>
int main() {
string text, pattern;
cout << "Enter the text: ";
getline(cin, text);
cout << "Enter the pattern: ";
getline(cin, pattern);
return 0;
}
#include <iostream>
void createArray() {
if (arr) {
delete[] arr;
arr = nullptr;
}
std::cout << "Enter the size of the array: ";
std::cin >> size;
arr = new int[size];
std::cout << "New array created.\n";
}
void insertElement() {
if (!arr) {
std::cout << "Array is not created yet.\n";
return;
}
void deleteElement() {
if (!arr) {
std::cout << "Array is not created yet.\n";
return;
}
int position;
std::cout << "Enter the position (0-indexed) to delete from (0 to " << size - 1 << "): ";
std::cin >> position;
void displayMenu() {
std::cout << "\nArray Operations Menu:\n";
std::cout << "1. Create Array\n";
std::cout << "2. Insert Element\n";
std::cout << "3. Delete Element\n";
std::cout << "4. Display Array\n";
int main() {
char choice;
while (true) {
displayMenu();
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case '1':
createArray();
break;
case '2':
insertElement();
break;
case '3':
deleteElement();
break;
case '4':
std::cout << "Current array: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << "\n";
break;
case '5':
delete[] arr;
std::cout << "Exiting the program.\n";
return 0;
default:
std::cout << "Invalid choice. Please try again.\n";
break;
}
}
return 0;
}
class LinkedList
{
private:
Node* head;
public:
LinkedList() : head(nullptr) {}
if (position == 1) {
newNode->next = head;
head = newNode;
std::cout << "Element inserted at position " << position << " in the linked list." << std::endl;
return;
}
if (temp == nullptr) {
std::cout << "Invalid position. Element not inserted in the linked list." << std::endl;
} else {
newNode->next = temp->next;
temp->next = newNode;
std::cout << "Element inserted at position " << position << " in the linked list." << std::endl;
}
}
int main()
{
LinkedList list;
int choice, data , pos=-1,n,cnt=-1;
do { std::cout<<"\n" ;
std::cout << "Menu:\nFirst of all you creat a linked list and do other work\n";
switch (choice)
{
case 1:
std::cout << "Enter elements to create a linked list (enter '-1' to stop taking elements): ";
while (true)
{
std::cin >> data;
if(data<0 && data != -1){
std::cout<<"Negative value cannot take in the linked list"<<std::endl<<"Try
again"<<std::endl ;
break ;
}
if (data==-1)
{
break;
}
list.insert(data);
}
// else{
// choice=1 ;
// }
if(data==-1){
std::cout << "Linked list created." << std::endl;
list.display();}
break;
case 2:
std::cout << "Enter element to search: ";
std::cin >> data;
if (list.search(data))
{
std::cout << "Element found in the linked list." << std::endl;
}
else
{
std::cout << "Element not found in the linked list." << std::endl;
}
break;
case 3:
/*std::cout << "Enter element to insert: ";
std::cin >> data >> pos;
list.insertpos(data,pos);
std::cout << "Element inserted into the linked */
int position;
std::cout << "Enter element to insert: ";
std::cin >> data;
std::cout << "Enter position to insert: ";
std::cin >> position;
if(position>0){
list.insertAtPosition(data, position);
list.display();}
else {
std::cout<<"Invalid position"<<std::endl ;
}
break;
case 4:
std::cout << "Enter element to delete: ";
std::cin >> data;
list.deleteNode(data);
list.display();
break;
case 5:
std::cout << "Exiting the program." << std::endl;
break;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}
} while (choice != 5);
return 0;
}
Input and Output :
8.Stack
(i)Menu Driven : Push , Pop
Code:
#include <iostream>
#include <stack>
int main() {
stack<int> s;
int choice, value;
while (true) {
cout << "\nStack Operations Menu" << endl;
cout << "---------------------" << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Display Top" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout<<"How many element you want to push into the stack : ";
int m ;
cin>>m ;
cout << "Enter the value to push: " ;
while(m--){
//cout << "Enter the value to push: ";
cin >> value;
s.push(value); }
//cout << value << " pushed into the stack." << endl;
break;
case 2:
cout<<"How many element you want to pop into the stack : ";
int p ;
cin>> p ;
if(!s.empty()) {
while(p--){
value = s.top();
s.pop();
cout << value << " popped from the stack." << endl;
}} else {
cout << "Stack is empty." << endl;
}
break;
case 3:
if (!s.empty()) {
cout << "Top of the stack: " << s.top() << endl;
} else {
cout << "Stack is empty." << endl;
}
break;
case 4:
cout << "Exiting the program." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
Input and Output :
8(ii)Infix to Postfix
Code:
#include<iostream>
#include<stack>
using namespace std ;
int prec(char c)
{
if(c=='^')
return 3 ;
else if(c=='*' || c=='/')
return 2 ;
else if(c=='+' || c=='-')
return 1 ;
else
return -1 ;
string InfixToPostfix(string s)
{
stack<char> st ;
string res;
res+=s[i] ;
else if(s[i]=='(')
st.push(s[i]) ;
else if(s[i]==')')
{
while(st.top()!='(' && !st.empty())
{
res+=st.top();
st.pop();
}
if(!st.empty())
st.pop();
else
{
while(!st.empty() && prec(st.top()) > prec(s[i]))
{
res+= st.top() ;
st.pop() ;
}
st.push(s[i]);
}
}
while(!st.empty())
{
res+= st.top() ;
st.pop();
return res ;
}
int main()
{
cout<<"Enter a Infix expression = " ;
string s ;
cin>>s ;
cout<<"infix to Postfix convert = " << InfixToPostfix(s) <<endl ;
return 0 ;
}
string InfixToPostfix(string s)
{
stack<char> st ;
string res;
res+=s[i] ;
else if(s[i]=='(')
st.push(s[i]) ;
else if(s[i]==')')
{
while(st.top()!='(' && !st.empty())
{
res+=st.top();
st.pop();
}
if(!st.empty())
st.pop();
else
{
while(!st.empty() && prec(st.top()) > prec(s[i]))
{
res+= st.top() ;
st.pop() ;
}
st.push(s[i]);
}
}
while(!st.empty())
{
res+= st.top() ;
st.pop();
return res ;
}
int PostfixEvaluation(string s){
stack<int> st ;
for(int i=0 ; i<s.length() ; i++)
{
if(s[i]>='0' && s[i]<='9')
st.push(s[i] - '0') ;
else {
int op2= st.top();
st.pop() ;
int op1= st.top();
st.pop() ;
switch(s[i]){
case '+' :
st.push(op1+op2);
break ;
case '-' :
st.push(op1-op2);
break ;
case '*' :
st.push(op1*op2);
break ;
case '/' :
st.push(op1/op2);
break ;
case '^' :
st.push(op1^op2);
break ;
}}}
return st.top() ;
}
int main()
{
}
Input and Output :
8(iv)Quick sort
Code:
#include<iostream>
using namespace std ;
void swap(int array[],int start, int end)
{
int temp = array[start] ;
array[start] = array[end] ;
array[end] = temp ;
if(low<high)
{
int pivot = Partition (array,low,high) ;
QuickSort(array,low,pivot-1) ;
QuickSort(array,pivot+1, high);
}
}
int main(){
int n ;
cout<<"How much element you want = " ;
cin>> n ;
int array[n] ;
for(int i=0 ; i<n ; i++){
cin>> array[i] ;
}
QuickSort(array,0,n-1);
cout<<endl <<"After apply quick sort algorithm the array is = " <<endl ;
for(int i=0 ; i<n ; i++){
cout<<array[i]<<" " ;
}
cout<<endl ;
return 0 ;
}
Input and Output :
9.Queue (Push,Pop)
Code:
#include<iostream>
int main();
using namespace std ;
class Queue{
private :
int front , rear , *a ;
int maxsize ;
public :
Queue(){}
delete[]a ;
}
int enqueue(int x){
if(rear==maxsize-1){ cout<<"Overflow"<<endl ;
}
else if(front=-1 && rear==-1){
front=rear=0 ;
a[rear]=x ;
}
else {
rear++ ;
a[rear] =x ;
}
}
int dequeue(){
else if(front==rear ){
else {
cout<<"Deleted element is = "<<a[front] <<endl ;
front++ ;
}
cout<<endl <<endl ;
}
int display(){
int i ;
if(front==-1 && rear==-1)
cout<<"Queue is empty "<<endl ;
else {
cout<<endl<<endl ;
}
};
int main()
{ int s ;
cout<<"Enter your Queue size : " ;
cin>> s ;
Queue queue(s) ;
int n ,x ;
cout<<endl <<endl ;
int option=1 ;
while(option !=0){
cout<< "\tIf you want to input value press 1\n\tif you delete press 2\n\tif you display press 3\n\
tif you exit press 0\n\tNow choice the option = " ;
cin>> option ;
cout<<endl <<endl ;
if(option==1){
cout<< " Put value = " ;
cin>> x ;
queue.enqueue( x) ;
}
else if(option==2)
queue.dequeue() ;
else if(option==3)
queue.display() ;
}
}
Input and Output :
10. Tree Menu Driven
(i)Create a Binary Tree by Taking User Input
(ii)OutPut InOrder Traversal Of That Tree
Code:
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left,*right;
};
struct Node *create() {
int x;
struct Node *newnode;
newnode = new Node;
cout << "Enter Data: " << endl;
cin >> x;
if(x == -1) {
return 0;
}
newnode->data = x;
cout << "Enter left child of " << x << " : " << endl;
newnode->left = create();
cout << "Enter right child of " << x << " : " << endl;
newnode->right = create();
return newnode;
}
void preorder(struct Node* root) {
if(root == NULL)
return;
cout << root->data << "->" << endl;
preorder(root->left);
preorder(root->right);
}
void inorder(struct Node* root) {
if(root == NULL)
return;
inorder(root->left);
cout << root->data << "->" << endl;
inorder(root->right);
}
void postorder(struct Node *root) {
if(root == NULL)
return;
postorder(root->left);
postorder(root->right);
cout << root->data << "-> " << endl;
}
int main() {
// int choice;
struct Node *root = NULL;
//root = NULL;
root = create();
cout << "Preorder list: " << endl;
preorder(root);
cout << "Postorder list: " << endl;
postorder(root);
cout << "Inorder list : " << endl;
inorder(root);
}
int main()
{
int vertices;
cout << "Enter the number of vertices: ";
cin >> vertices;
int edges;
cout << "Enter the number of edges: ";
cin >> edges;
int main()
{
int m, i, j, k;
int adj[100][100], path[100][100] = {0};
cout << "Enter the number of vertices: ";
cin >> m;
cout << "Enter a "<< m << "X" << m << " size adjacency matrix: " << endl;
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
cin >> adj[i][j];
}
}
cout << "Enter a "<< m << "X" << m << " size adjacency matrix: " << endl;
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
cin >> adj[i][j];
}
}