Skip to content

Instantly share code, notes, and snippets.

@alekxeyuk
Created January 12, 2024 14:56
Show Gist options
  • Save alekxeyuk/157e3eb3e43f1d18b37ede4dbf2ebf49 to your computer and use it in GitHub Desktop.
Save alekxeyuk/157e3eb3e43f1d18b37ede4dbf2ebf49 to your computer and use it in GitHub Desktop.
std::list with iterator pointing to the middle of the list
#include <iostream>
#include <list>
// std::list with iterator pointing to the middle of the list
int main() {
std::list<int> lst{};
size_t N;
char op;
int i;
std::cin >> N;
std::list<int>::iterator mid = lst.begin();
while (N--)
{
std::cin >> op;
switch (op)
{
case '+':
std::cin >> i;
lst.push_back(i);
if (lst.size() == 1)
{
mid = lst.end();
}
else if (lst.size() == 2)
{
--mid;
}
else if (lst.size() % 2 == 1 and mid != lst.end())
{
++mid;
}
break;
case '*':
std::cin >> i;
lst.insert(mid, i);
if (lst.size() == 1)
{
mid = lst.end();
}
else if (lst.size() == 2)
{
--mid;
}
else if (lst.size() % 2 == 0 and mid != lst.end())
{
--mid;
}
break;
case '-':
std::cout << lst.front() << '\n';
lst.pop_front();
if (lst.size() % 2 == 1)
{
++mid;
}
break;
}
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment