Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- 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;
- }
- private:
- Node* prev;
- int data;
- Node* next;
- };
- 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() { //TODO NOT DONE
- cout << "Destructor" << endl;
- Node* nextObj{head->getNext()};
- delete head;
- head = nullptr;
- }
- 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;
- }
- private:
- Node* head;
- int size;
- };
- int main()
- {
- DLinkedCircList testList{};
- testList.push_back(99);
- cout << testList.toString() << endl;
- testList.push_back(105);
- cout << testList.toString() << endl;
- cout << "Hello world!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement