Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- using namespace std;
- class Node {
- public:
- Node(int _data)
- : data{_data}, prev{this}, next{this}
- {}
- Node* getNext() const {
- return next;
- }
- Node* getPrev() const {
- return prev;
- }
- void setNext(Node* ptr) {
- next = ptr;
- }
- void setPrev(Node* ptr) {
- prev = ptr;
- }
- int getData() const {
- return data;
- }
- Node* prev;
- Node* next;
- private:
- int data;
- };
- class DLinkedCircList {
- public:
- string toString() const {
- string out{"["};
- if (head != nullptr) {
- int data{};
- Node* currentNode{head};
- do {
- data = currentNode->getData();
- out += to_string(data);
- if (currentNode->getNext() != head) {
- out += ", ";
- }
- currentNode = currentNode->getNext();
- } while (currentNode != head);
- }
- out += "]";
- return out;
- }
- DLinkedCircList()
- : head{nullptr}, size{0} {
- }
- ~DLinkedCircList() {
- Node* lastNode = head->getPrev();
- Node* currentNode = head;
- Node* nextNode{};
- while (currentNode != lastNode) {
- nextNode = currentNode->getNext();
- delete currentNode;
- currentNode = nextNode;
- }
- delete lastNode;
- }
- void push_back(int input) {
- Node* newNode{new Node{input}};
- //did the memory get allocated???
- if (head == nullptr) {
- head = newNode;
- }
- else { //add to end
- Node* last{head->getPrev()};
- last->setNext(newNode);
- newNode->setPrev(last);
- newNode->setNext(head);
- head->setPrev(newNode);
- }
- size +=1;
- }
- int getSize() {
- return size;
- }
- void insert(int data, int location) {
- if (head == nullptr) {
- push_back(data);
- }
- else {
- Node* newNode{new Node{data}};
- if (location == 0) {
- newNode->setPrev(head->getPrev());
- newNode->setNext(head);
- (head->getPrev())->setNext(newNode);
- head->setPrev(newNode);
- head = newNode;
- size +=1;
- }
- else {
- if (location <= size) {
- Node* currentNode = at(location);
- newNode->setPrev(currentNode->getPrev());
- newNode->setNext(currentNode);
- (currentNode->getPrev())->setNext(newNode);
- currentNode->setPrev(newNode);
- size+=1;
- }
- else {
- cout << "LOCATION INVALID IDIOT" << endl; //throw an exception or something instead of calling the user an idiot
- }
- }
- }
- }
- int pop_back() {
- int result{00};
- if (head != nullptr) {
- result = (head->getPrev())->getData();
- Node* newLast = (head->getPrev())->getPrev();
- newLast->setNext(head);
- delete head->getPrev();
- head->setPrev(newLast);
- size = size-1;
- }
- else {
- cout << "you have no items in this list, idiot" << endl; //throw an exception or something instead of calling the user an idiot
- }
- return result;
- }
- erase(int location) {
- if (location >= 0 && location < size) {
- if (head != nullptr) {
- Node* currentNode = at(location);
- (currentNode->getPrev())->setNext(currentNode->getNext());
- (currentNode->getNext())->setPrev(currentNode->getPrev());
- if (location == 0) { head = currentNode->getNext(); }
- delete currentNode;
- currentNode = nullptr;
- size = size-1;
- }
- else {
- cout << "you have no items in this list, idiot" << endl; //throw an exception or something instead of calling the user an idiot
- }
- }
- else {
- cout << "invalid location dumbass" << endl;
- }
- }
- int cut(int location) {
- int result;
- if (location >= 0 && location < size) {
- if (head != nullptr) {
- result = (at(location))->getData();
- erase(location);
- }
- else {
- cout << "you have no items in this list, idiot" << endl; //throw an exception or something instead of calling the user an idiot
- }
- }
- return result;
- }
- Node* at(int location) const {
- if (head != nullptr) {
- Node* currentNode = head;
- if (location <= size) {
- for (int i = 0; i < location; i++) {
- currentNode = currentNode->getNext();
- }
- }
- return currentNode;
- }
- }
- int front() const {
- if (head != nullptr) {
- return head->getData();
- }
- else { cout << "no items in list idiot" << endl;}
- }
- int back() const {
- if (head != nullptr) {
- return head->getPrev()->getData();
- }
- else { cout << "no items in list idiot" << endl;}
- }
- void clear() {
- int originalSize = size;
- for (int i = 0; i < originalSize; i++) {
- erase(0);
- }
- head = nullptr;
- }
- DLinkedCircList reverse() {
- DLinkedCircList result{};
- if (head != nullptr) {
- Node* currentNode = head->getPrev();
- Node* nextNode{};
- Node* lastNode = head;
- while (currentNode != lastNode) {
- result.push_back(currentNode->getData());
- nextNode = currentNode->getPrev();
- currentNode = nextNode;
- }
- result.push_back(head->getData());
- }
- else {
- throw invalid_argument("You cannot reverse a list with no elements.");
- }
- return result;
- }
- private:
- Node* head;
- int size;
- };
- void test1() {
- DLinkedCircList test{};
- for (int i{0}; i < 10; i++) {
- test.push_back(i);
- }
- cout << test.toString() << endl;
- cout << test.pop_back() << endl;
- }
- int main()
- {
- test1();
- DLinkedCircList testList{};
- testList.push_back(99);
- cout << testList.toString() << endl;
- testList.push_back(105);
- cout << testList.toString() << endl;
- testList.insert(5,0);
- cout << testList.toString() << endl;
- testList.insert(69,0);
- testList.insert(0,0);
- cout << testList.toString() << endl;
- testList.insert(699999,3);
- cout << testList.toString() << endl;
- testList.insert(6999999,6);
- cout << "dividing line..." << endl;
- cout << testList.toString() << endl;
- testList.insert(1111111111,8);
- cout << testList.toString() << endl;
- //cout << testList.pop_back() << endl;
- cout << testList.toString() << endl;
- //cout << testList.cut(5) << endl;
- cout << testList.toString() << endl;
- cout << "trying to erase item zero..." << endl;
- cout << testList.at(0)->getPrev()->getData() << " : " << testList.at(0)->getNext()->getData() << endl;
- //testList.erase(0);
- cout << testList.toString() << endl;
- cout << "success" << endl;
- cout << testList.toString() << endl;
- //testList.erase(4);
- cout << testList.toString() << endl;
- cout << testList.front() << endl;
- cout << testList.back() << endl;
- testList.clear();
- testList.push_back(1);
- testList.push_back(2);
- testList.push_back(3);
- testList.push_back(4);
- testList.push_back(5);
- cout << testList.toString() << endl;
- cout << "reversed: " << testList.reverse().toString() << endl;
- DLinkedCircList x{};
- x.push_back(1);
- x.push_back(2);
- x.push_back(3);
- cout << "x = " << x.toString() << endl;
- cout << "x reverse: " << x.reverse().toString() << endl;
- DLinkedCircList test2{};
- test2.push_back(1);
- cout << test2.toString() << endl;
- cout << "reversed: " << test2.reverse().toString() << endl;
- DLinkedCircList test3{};
- cout << "test3 = " << test3.toString() << endl;
- test3.push_back(1);
- cout << test3.reverse().toString() << endl;;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement