-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.cpp
64 lines (58 loc) · 1.53 KB
/
linked_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <algorithm>
using namespace std;
int main(){
using LinkedList = std::list<int>;
using ListIterator = LinkedList::iterator;
LinkedList list;
list.push_front(0);
ListIterator it = list.begin();
std::string buffer;
size_t pos = 0;
std::string token;
std::string delimiter = " ";
while(getline(cin, buffer)){
/*Parse Input*/
while( (pos = buffer.find(delimiter)) != std::string::npos){
token = buffer.substr(0, pos);
buffer.erase(0, pos + delimiter.length());
}
//move left
if(buffer == "LEFT"){
if(list.begin() != it)
it--;
}
//move right
else if(buffer == "RIGHT"){
if(--list.end() != it)
it++;
}
else{
int value = std::stoi(buffer);
//insert left
if(token == "LEFT"){
if(list.begin() == it){
list.push_front(value);
}
else
list.insert(it, value);
}
//insert right
else{
if(--list.end() == it){
list.push_back(value);
}
else{
ListIterator tmp = it;
list.insert(++tmp, value);
}
}
}
}
for(int element : list)
cout << element << endl;
}