Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdexcept>
- #include <cassert>
- #include "../Headers/LinkedList.h"
- LinkedList::LinkedList() {
- first = last = nullptr;
- size = 0;
- }
- LinkedList::~LinkedList() {
- while (first) {
- Node* n = first;
- delete first;
- first = n;
- }
- }
- int LinkedList::GetFront() const {
- if (isEmpty()) {
- throw std::runtime_error("List is empty");
- }
- return first->data;
- }
- int LinkedList::GetLast() const {
- if (isEmpty()) {
- throw std::runtime_error("List is empty");
- }
- return last->data;
- }
- void LinkedList::insertFront(int x) {
- Node* node = new Node;
- node->data = x;
- node->next = first;
- if (isEmpty()) {
- last = node;
- }
- first = node;
- ++size;
- }
- void LinkedList::insertBack(int x) {
- Node* node = new Node;
- node->data = x;
- node->next = nullptr;
- if (isEmpty()) {
- first = node;
- last = node;
- } else {
- last->next = node;
- last = node;
- }
- ++size;
- }
- void LinkedList::removeFront() {
- if (isEmpty()) {
- throw std::runtime_error("List is empty");
- }
- Node* node = first;
- first = first->next;
- delete node;
- if (size == 1) {
- last = first;
- }
- --size;
- }
- bool LinkedList::isEmpty() const {
- return size == 0;
- }
- void LinkedList::insertAfter(Node *it, int x) {
- assert( it != nullptr);
- Node* node = new Node;
- node->next = it->next;
- node->data = x;
- it->next = node;
- if (it == last) {
- last = node;
- }
- ++size;
- }
- void LinkedList::RemoveAfter(Node *it) {
- assert(it != nullptr);
- bool changeLast = (it->next == last);
- if (it == last) {
- std::runtime_error("cannot remove");
- }
- Node* n = it->next;
- it->next = it->next->next;
- delete n;
- --size;
- }
- bool predicate(int val) {
- return val == 5;
- }
- const Node* find(const Node* first) {
- while (first != nullptr && !predicate(first->data)) {
- first = first->next;
- }
- return first;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement