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

DSA Lab Task 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DSA Lab Task 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Task 02

Name: Kamal Shah


Class ID: CU-4320-2023
Subject: DSA Lab
Submitted to: Sikander Azam
Date: 11/ 19/ 2024
Array Operations:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class LinearSearch {
private:
vector<int> list;

public:
void insertion() {
int element;
cout << "Enter element to insert: ";
cin >> element;
list.push_back(element);
cout << "Element inserted successfully.\n";
}
void deletion() {
if (list.empty()) {
cout << "List is empty. Cannot delete.\n";
return;
}

int element;
cout << "Enter element to delete: ";
cin >> element;

auto it = find(list.begin(), list.end(), element);


if (it != list.end()) {
list.erase(it);
cout << "Element deleted successfully.\n";
} else {
cout << "Element not found.\n";
}
}
void searching() {
if (list.empty()) {
cout << "List is empty. Cannot search.\n";
return;
}

int element;
cout << "Enter element to search: ";
cin >> element;

for (size_t i = 0; i < list.size(); ++i) {


if (list[i] == element) {
cout << "Element found at index " << i << ".\n";
return;
}
}
cout << "Element not found.\n";
}
void traversing() {
if (list.empty()) {
cout << "List is empty.\n";
return;
}

cout << "List elements: ";


for (int element : list) {
cout << element << " ";
}
cout << "\n";
}

void smallest() {
if (list.empty()) {
cout << "List is empty.\n";
return;
}
int min = list[0];
for (int element : list) {
if (element < min) {
min = element;
}
}
cout << "Smallest element: " << min << "\n";
}

void largest() {
if (list.empty()) {
cout << "List is empty.\n";
return;
}

int max = list[0];


for (int element : list) {
if (element > max) {
max = element;
}
}
cout << "Largest element: " << max << "\n";
}

void menu() {
int choice;
do {
cout << "\n--- Linear Search Menu ---\n";
cout << "1. Insertion\n";
cout << "2. Deletion\n";
cout << "3. Searching\n";
cout << "4. Traversing\n";
cout << "5. Smallest\n";
cout << "6. Largest\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: insertion(); break;
case 2: deletion(); break;
case 3: searching(); break;
case 4: traversing(); break;
case 5: smallest(); break;
case 6: largest(); break;
case 7: cout << "Exiting program.\n"; break;
default: cout << "Invalid choice. Try again.\n";
}
} while (choice != 7);
}
};

int main() {
LinearSearch search;
search.menu();
return 0;
}

You might also like