Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using std::cout;
- using std::endl;
- struct Node
- {
- int value{0};
- Node* next{nullptr};
- };
- struct Lista
- {
- int count{0};
- Node *head{ nullptr };
- Node *tail{ nullptr };
- void print()
- {
- if (!head)
- {
- cout << "[]" <<endl;
- return;
- }
- int index = 0;
- Node* current = head;
- cout <<count << " [" ;
- while (current)
- {
- cout <<current->value;
- index++;
- if (index <count) cout << ",";
- current = current->next;
- }
- cout <<"]" <<endl;
- }
- void push_front(int value)
- {
- Node* node = new Node();
- node->value = value;
- if(!head)
- {
- head = node;
- tail = node;
- }else
- {
- node->next = head;
- head = node;
- }
- count++;
- }
- void push_back(int value)
- {
- Node* node = new Node();
- node->value = value;
- if(!head)
- {
- head = node;
- tail = node;
- }else
- {
- tail->next = node;
- tail = node;
- }
- count++;
- }
- bool pop_front()
- {
- if (!head) return false;
- Node* tmp = head->next;
- // cout << "delete:" << head->value << endl;
- delete head;
- head = tmp;
- count--;
- return true;
- }
- bool pop_back()
- {
- if (!head) return false;
- if (count==1)
- {
- return pop_front();
- }
- Node* current = head;
- Node* prev = nullptr;
- while(current->next)
- {
- prev = current;
- current = current->next;
- }
- if(prev)
- {
- Node* del = prev->next;
- tail = prev;
- tail->next = nullptr;
- cout << del->value << endl;
- delete del;
- }
- count--;
- return true;
- }
- void clear()
- {
- Node* tmp=head;
- while (head)
- {
- tmp = head->next;
- // cout << "delete:" << head->value << endl;
- delete head;
- head = tmp;
- }
- count = 0;
- }
- bool insert(int index, int value)
- {
- if(index<=0)
- {
- push_front(value);
- return true;
- }
- if(index>=count)
- {
- push_back(value);
- return true;
- }
- Node* prev = head;
- Node* current = nullptr;
- for (int i = 0; i <= index - 1; i++)
- {
- current = prev;
- prev = prev->next;
- }
- Node* node = new Node();
- node->value = value;
- node->next = prev;
- current->next = node;
- count++;
- // cout<<"index: " << index << endl;
- // cout<<"prev: " << prev->value << endl;
- // cout<<"currn: " << current->value << endl;
- return false;
- }
- bool remove(int index)
- {
- if (!head) return false;
- if (index <= 0) return pop_front();
- if (index >= count) return pop_back();
- Node* prev = head;
- Node* current = nullptr;
- for (int i = 0; i <= index - 1; i++)
- {
- current = prev;
- prev = prev->next;
- }
- // cout<<"prev: " << prev->value << endl;
- // cout<<"currn: " << current->value << endl;
- current->next = prev->next;
- delete prev;
- count--;
- return false;
- }
- };
- int main()
- {
- Lista lista;
- for (int i = 0; i <= 5;i++)
- lista.push_back(i);
- lista.insert(5, 100);
- // lista.remove(5);
- // lista.push_back(1);
- // lista.pop_back();
- // lista.pop_back();
- // lista.pop_back();
- // lista.pop_back();
- lista.print();
- lista.clear();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment