use dynamic array as the underlying data structure to hold the list contents. The array will hold integer data. Here are the functions that your program should offer ( options in MENU):
1. Insert an item
Insert function will insert the value at the start of the list. Tell the user how many list elements are shifted to make room for new item.
2. Delete an item
It will ask the user for the value, if the value is present, it will be deleted from the list and function displays how many items are shifted to delete the value
3. Print out the items in the list
#include <iostream>
#include <vector>
using namespace std;
vector<int> vec;
void insertItem(int item) {
vec.insert(0, item);
}
void deleteItem(int item) {
vec.delete(item);
}
void printVec() {
for (int i : vec) {
cout << i << " ";
}
cout << endl;
}
int main() {
bool flag = true;
while (flag) {
cout << "1. insert an item" << endl
<< "2. delete an item" << endl
<< "3. print all items" << endl
<< "4. exit" << endl;
int n;
cin >> n;
switch(n) {
case 1:
cin >> n;
insertItem(n);
break;
case 2:
cin >> n;
deleteItem(n);
break;
case 3:
printVec();
break;
case 4:
flag = false;
}
}
return 0;
}
Comments
Leave a comment