Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- struct ListNode {
- int value;
- ListNode *next;
- ListNode *prev;
- };
- class LinkedList {
- public:
- ListNode *head, *tail;
- LinkedList();
- explicit LinkedList(int n);
- void pushBack(int x);
- void pushFront(int x);
- void insert(int x, int position);
- void remove(int position);
- void print(bool direction);
- ~LinkedList();
- };
- LinkedList::LinkedList() {
- this->head = nullptr;
- this->tail = nullptr;
- }
- LinkedList::LinkedList(int n) : LinkedList() {
- for (int c, i = 0; i < n; i++) {
- std::cin >> c;
- this->pushBack(c);
- }
- }
- LinkedList::~LinkedList() {
- ListNode *current = this->head;
- while (head) {
- this->head = this->head->next;
- delete current;
- current = this->head;
- }
- }
- void LinkedList::pushBack(int x) {
- ListNode *node = new ListNode{x, nullptr, nullptr};
- if (!this->head) {
- this->head = node;
- this->tail = node;
- } else {
- this->tail->next = node;
- node->prev = tail;
- this->tail = this->tail->next;
- }
- }
- void LinkedList::pushFront(int x) {
- ListNode *node = new ListNode{x, nullptr, nullptr};
- if (!this->head) {
- this->head = node;
- this->tail = node;
- } else {
- this->head->prev = node;
- node->next = head;
- this->head = this->head->prev;
- }
- }
- void LinkedList::insert(int x, int position) {
- ListNode *current = this->head;
- int place = 0;
- while (place != position) {
- current = current->next;
- place++;
- }
- if (!current->next) {
- this->pushBack(x);
- return;
- }
- ListNode *insertion_node = new ListNode{x, current->next, current};
- insertion_node->next->prev = insertion_node;
- current->next = insertion_node;
- }
- void LinkedList::remove(int position) {
- if (position == -1) {
- head = head->next;
- return;
- }
- ListNode *current = this->head;
- int place = 0;
- while (current && place != position) {
- current = current->next;
- place++;
- }
- if (!current || !current->next) {
- return;
- }
- if (!current->next->next) {
- // current->next = tail;
- tail = tail->prev;
- tail->next = nullptr;
- return;
- }
- current->next = current->next->next;
- current->next->prev = current;
- }
- void LinkedList::print(bool direction = true) {
- if (direction) {
- ListNode *current = this->head;
- while (current) {
- std::cout << current->value << " ";
- current = current->next;
- }
- } else {
- ListNode *current = this->tail;
- while (current) {
- std::cout << current->value << " ";
- current = current->prev;
- }
- }
- std::cout << "\n";
- }
- void handleCommand(LinkedList *list, const std::string &command) {
- if (command == "push_back") {
- int x;
- std::cin >> x;
- list->pushBack(x);
- } else if (command == "push_front") {
- int x;
- std::cin >> x;
- list->pushFront(x);
- } else if (command == "insert") {
- int x, position;
- std::cin >> x >> position;
- list->insert(x, position);
- } else if (command == "remove") {
- int position;
- std::cin >> position;
- list->remove(position);
- }
- }
- int main() {
- int n, m;
- std::cin >> n >> m;
- LinkedList *list = new LinkedList(n);
- std::string command;
- for (int i = 0; i < m; i++) {
- std::cin >> command;
- handleCommand(list, command);
- // list.print();
- }
- list->print();
- list->print(false);
- delete list;
- return 0;
- }
Advertisement
RAW Paste Data
Copied
Advertisement