Creați o lista cu “N” obiecte de tip structura asupra căreia se vor realiza toate operațiile specifice listelor:
-Adăugare
-Ștergere
-Listare
-Căutare
Dau coroană!!!!!!!!
Răspunsuri la întrebare
Răspuns:
#include <iostream>
#include <vector>
struct MyStruct {
int value;
std::string name;
};
std::vector<MyStruct> myList;
void addItem(int value, std::string name) {
MyStruct item;
item.value = value;
item.name = name;
myList.push_back(item);
}
void deleteItem(int value) {
for (int i = 0; i < myList.size(); i++) {
if (myList[i].value == value) {
myList.erase(myList.begin() + i);
break;
}
}
}
void listItems() {
for (int i = 0; i < myList.size(); i++) {
std::cout << "Value: " << myList[i].value << ", Name: " << myList[i].name << std::endl;
}
}
void searchItem(int value) {
for (int i = 0; i < myList.size(); i++) {
if (myList[i].value == value) {
std::cout << "Value: " << myList[i].value << ", Name: " << myList[i].name << std::endl;
return;
}
}
std::cout << "Item not found." << std::endl;
}
int main() {
addItem(1, "Item 1");
addItem(2, "Item 2");
addItem(3, "Item 3");
std::cout << "Listing all items:" << std::endl;
listItems();
std::cout << "Searching for item with value 2:" << std::endl;
searchItem(2);
std::cout << "Deleting item with value 2:" << std::endl;
deleteItem(2);
std::cout << "Listing all items:" << std::endl;
listItems();
return 0;
}
Explicație: