--------------ArrayOperation.cpp
#include <iostream>
class Array {
private:
int* data; // Pointer to dynamically allocated array
int size; // Size of the array
int capacity; // Capacity of the array
public:
// Constructor
Array(int capacity)
{
this->capacity = capacity;
size = 0;
data = new int[capacity];
}
// Destructor
~Array() {
delete[] data;
}
// Function to insert an element at the beginning
void insertAtBeginning(int element)
{
if (size < capacity) {
for (int i = size; i > 0; --i)
{
data[i] = data[i - 1];
}
data[0] = element;
size++;
} else {
std::cout << "Array is full. Cannot insert element.\n";
}
}
// Function to insert an element at the end
void insertAtEnd(int element) {
if (size < capacity) {
data[size++] = element;
} else {
std::cout << "Array is full. Cannot insert element.\n";
}
}
// Function to insert an element at a given index
void insertAt(int index, int element)
{
if (index >= 0 && index <= size && size < capacity) {
for (int i = size; i > index; --i) {
data[i] = data[i - 1];
}
data[index] = element;
size++;
} else {
std::cout << "Invalid index or array is full. Cannot insert element.\n";
}
}
// Function to delete an element at the beginning
void removeFromBeginning() {
if (size > 0) {
for (int i = 0; i < size - 1; ++i) {
data[i] = data[i + 1];
}
size--;
} else {
std::cout << "Array is empty. Cannot remove element.\n";
}
}
// Function to delete an element at the end
void removeFromEnd() {
if (size > 0) {
size--;
} else {
std::cout << "Array is empty. Cannot remove element.\n";
}
}
// Function to delete an element at a given index
void removeAt(int index) {
if (index >= 0 && index < size) {
for (int i = index; i < size - 1; ++i) {
data[i] = data[i + 1];
}
size--;
} else {
std::cout << "Invalid index. Cannot remove element.\n";
}
}
// Function to search for an element
int search(int element) {
for (int i = 0; i < size; ++i) {
if (data[i] == element) {
return i; // Return index of element if found
}
}
return -1; // Return -1 if element not found
}
// Function to access element at given index
int get(int index) {
if (index >= 0 && index < size) {
return data[index];
} else {
std::cout << "Invalid index. Cannot access element.\n";
return -1; // Return -1 if index is out of bounds
}
}
// Function to display the elements of the array
void display() {
std::cout << "Array Elements: ";
for (int i = 0; i < size; ++i) {
std::cout << data[i] << " ";
}
std::cout << std::endl;
}
};
int main() {
int capacity;
std::cout << "Enter the capacity of the array: ";
std::cin >> capacity;
Array arr(capacity);
int choice, element, index;
do {
std::cout << "\nChoose operation:\n";
std::cout << "1. Insert at beginning\n";
std::cout << "2. Insert at end\n";
std::cout << "3. Insert at specific position\n";
std::cout << "4. Remove from beginning\n";
std::cout << "5. Remove from end\n";
std::cout << "6. Remove from specific position\n";
std::cout << "7. Search for an element\n";
std::cout << "8. Access element at index\n";
std::cout << "9. Display array\n";
std::cout << "0. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Enter element to insert at the beginning: ";
std::cin >> element;
arr.insertAtBeginning(element);
break;
case 2:
std::cout << "Enter element to insert at the end: ";
std::cin >> element;
arr.insertAtEnd(element);
break;
case 3:
std::cout << "Enter element to insert: ";
std::cin >> element;
std::cout << "Enter position to insert at: ";
std::cin >> index;
arr.insertAt(index, element);
break;
case 4:
arr.removeFromBeginning();
break;
case 5:
arr.removeFromEnd();
break;
case 6:
std::cout << "Enter position to remove at: ";
std::cin >> index;
arr.removeAt(index);
break;
case 7:
std::cout << "Enter element to search for: ";
std::cin >> element;
index = arr.search(element);
if (index != -1) {
std::cout << "Element found at index: " << index << std::endl;
} else {
std::cout << "Element not found.\n";
}
break;
case 8:
std::cout << "Enter index to access element: ";
std::cin >> index;
std::cout << "Element at index " << index << " is: " << arr.get(index) << std::endl;
break;
case 9:
arr.display();
break;
case 0:
std::cout << "Exiting...\n";
break;
default:
std::cout << "Invalid choice. Please try again.\n";
}
} while (choice != 0);
return 0;
}
No comments:
Post a Comment
Fell free to write your query in comment. Your Comments will be fully encouraged.