Advertisement
Nofxthepirate

Linked List Interface Program

Jun 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.76 KB | None | 0 0
  1. This Linked List Program includes ListAPI.hpp, Node.hpp, Node.cpp, DLinkedCircList.hpp, DLinkedCircList.cpp, and main.cpp with test cases.
  2.  
  3. //ListAPI.hpp code (pure abstract interface class)
  4.  
  5. #ifndef LISTAPI_HPP
  6. #define LISTAPI_HPP
  7. #include <string>
  8.  
  9. class ListAPI {
  10. virtual void push_back(int) = 0;
  11. virtual int pop_back() = 0;
  12. virtual void insert(int, int) = 0;
  13. virtual void erase(int) = 0;
  14. virtual std::string toString() const = 0;
  15. virtual int getSize() const = 0;
  16. virtual int at(int)const = 0;
  17. virtual void reverse() = 0;
  18. virtual int front()const = 0;
  19. virtual int back()const = 0;
  20. virtual void clear() = 0;
  21.  
  22. };
  23. #endif // LISTAPI_HPP
  24.  
  25. //Node.hpp code
  26.  
  27. #ifndef NODE_HPP
  28. #define NODE_HPP
  29.  
  30. class Node {
  31. public:
  32.     Node(int _data);
  33.  
  34.     Node* getNext() const;
  35.  
  36.     Node* getPrev() const;
  37.  
  38.     void setNext(Node* ptr);
  39.  
  40.     void setPrev(Node* ptr);
  41.  
  42.     int getData() const;
  43. private:
  44.     Node* prev;
  45.     int data;
  46.     Node* next;
  47. };
  48. #endif // NODE_HPP
  49.  
  50. //Node.cpp code
  51.  
  52. #include "Node.hpp"
  53.  
  54. Node::Node(int _data)
  55.     :  prev{this}, data{_data}, next{this}
  56.     {}
  57.  
  58.     Node* Node::getNext() const{return next;}
  59.  
  60.     Node* Node::getPrev() const{ return prev;}
  61.  
  62.     void Node::setNext(Node* ptr) {next = ptr;}
  63.  
  64.     void Node::setPrev(Node* ptr) {prev = ptr;}
  65.  
  66.     int Node::getData() const {return data;}
  67.  
  68.  
  69. //DLinkedCircList.hpp code
  70.  
  71. #ifndef DLINKEDCIRCLIST_HPP
  72. #define DLINKEDCIRCLIST_HPP
  73. #include "Node.hpp"
  74. #include "ListAPI.hpp"
  75. #include <string>
  76. #include <iostream>
  77. #include <vector>
  78.  
  79. class DLinkedCircList : public ListAPI {
  80.  
  81. public:
  82.     DLinkedCircList();
  83.     ~DLinkedCircList();
  84.     virtual std::string toString() const override;
  85.     virtual void push_back(int val) override;
  86.     virtual int pop_back() override;
  87.     virtual int getSize() const override;
  88.     virtual void clear() override;
  89.     virtual int at(int index) const override;
  90.     virtual void reverse() override;
  91.     virtual void insert(int index, int val) override;
  92.     virtual void erase(int index) override;
  93.     virtual int front() const override;
  94.     virtual int back() const override;
  95.  
  96. private:
  97. Node* head;
  98. int size;
  99. };
  100. #endif // DLINKEDCIRCLIST_HPP
  101.  
  102. //DLinkedCircList.cpp code
  103.  
  104. #include "DLinkedCircList.hpp"
  105.  
  106. DLinkedCircList::DLinkedCircList()
  107.     : head{nullptr}, size{0}
  108.     {}
  109.  
  110.     DLinkedCircList::~DLinkedCircList() {
  111.         clear();
  112.     }
  113.     std::string DLinkedCircList::toString() const {
  114.         std::string out{"["};
  115.         if (head != nullptr) {
  116.             Node* ptr{head};
  117.             do {
  118.                 out += std::to_string(ptr->getData());
  119.                 if (ptr->getNext() != head){
  120.                     out += ", ";
  121.                 }
  122.                 ptr = ptr->getNext();
  123.             } while (ptr != head);
  124.             out += "]";
  125.         }
  126.         return out;
  127.     }
  128.  
  129.     void DLinkedCircList::push_back(int val) {
  130.         Node* newNode{new Node{val}};
  131.         //error check to see if it was allocated
  132.         if (head == nullptr) {
  133.             head = newNode;
  134.         }
  135.         else { //add to the end
  136.             Node* last{head->getPrev()};
  137.             last->setNext(newNode); //sets the next ptr of the last node to the prev ptr of the new node
  138.             newNode->setPrev(last); // sets the prev ptr of the new node to the address of the next ptr's
  139.         //head's prev ptr is being passed as the argument on line 34 because we set last's data value as the prev head ptr
  140.             newNode->setNext(head); //sets the next pointer of the new node to the prev pointer of the first object
  141.             head->setPrev(newNode);
  142.         }
  143.         size += 1;
  144.     }
  145.  
  146.     int DLinkedCircList::pop_back() {
  147.         int value{0};
  148.         if(size == 1){
  149.             value = head->getData();
  150.             delete head;
  151.             head = nullptr;
  152.         }
  153.         else{
  154.             Node* poppedNode{head->getPrev()};
  155.             Node* newBack{head->getPrev()->getPrev()};
  156.             value = head->getPrev()->getData();
  157.             delete poppedNode;
  158.             poppedNode = nullptr;
  159.             newBack->setNext(head);
  160.             head->setPrev(newBack);
  161.         }
  162.         size >= 1 ? size -= 1 : size = 0;
  163.         return value;
  164.     }
  165.  
  166.     int DLinkedCircList::getSize() const {return size;}
  167.  
  168.     void DLinkedCircList::clear() {
  169.  
  170.         for(int i{0}; i < size; i += 1){
  171.             Node* current{head};
  172.             head = head->getNext();
  173.             delete current;
  174.             current = nullptr;
  175.         }
  176.         head = nullptr;
  177.         size = 0;
  178.     }
  179.     int DLinkedCircList::at(int index) const{
  180.         int i{1};
  181.         int valueAt{};
  182.         Node* temp{head->getNext()};
  183.         if (index == 0){
  184.             valueAt = head->getData();
  185.         }
  186.         else {
  187.             while(i < index){
  188.                 temp = temp->getNext();
  189.                 i += 1;
  190.             }
  191.             valueAt = temp->getData();
  192.         }
  193.     return valueAt;
  194.     }
  195.  
  196.     void DLinkedCircList::reverse() {
  197.         std::vector<int> tempVec{};
  198.         for(int i{0}; i < size; i += 1){
  199.             int tempData{at(i)};
  200.             tempVec.push_back(tempData);
  201.         }
  202.         clear();
  203.  
  204.         for(int i{tempVec.size()-1}; i >= 0; i -= 1){
  205.             int temp{tempVec[i]};
  206.             push_back(temp);
  207.         }
  208.     }
  209.  
  210.     void DLinkedCircList::insert(int index, int val) {
  211.         Node* newNode{new Node{val}};
  212.         Node* temp{head->getPrev()};
  213.         if (index == 0) {
  214.             newNode->setPrev(head->getPrev());
  215.             head->getPrev()->setNext(newNode);
  216.             newNode->setNext(head);
  217.             head->setPrev(temp);
  218.             head = newNode;
  219.             size += 1;
  220.         }
  221.         else if (index == size) {
  222.             push_back(val);
  223.         }
  224.         else{
  225.             temp = head;
  226.             for(int i{0}; i < index; i += 1) {
  227.                 temp = temp->getNext();
  228.             }
  229.             Node* prevTemp{temp->getPrev()};
  230.             newNode->setPrev(prevTemp);
  231.             newNode->setNext(temp);
  232.             temp->setPrev(newNode);
  233.             prevTemp->setNext(newNode);
  234.             size += 1;
  235.         }
  236.  
  237.     }
  238.  
  239.     void DLinkedCircList::erase(int index) {
  240.                 Node* listEnd{head->getPrev()};
  241.         if (index == 0) {
  242.             listEnd->setNext(head->getNext());
  243.             head = head->getNext();
  244.             head->setPrev(listEnd);
  245.             size -= 1;
  246.         }
  247.         else if (index == size - 1) {
  248.            pop_back();
  249.         }
  250.         else{
  251.             Node* temp{head};
  252.             for(int i{0}; i < index; i += 1) {
  253.                 temp = temp->getNext();
  254.             }
  255.             Node* prevTemp{temp->getPrev()};
  256.             Node* nextTemp{temp->getNext()};
  257.             prevTemp->setNext(nextTemp);
  258.             nextTemp->setPrev(prevTemp);
  259.             delete temp;
  260.             temp = nullptr;
  261.             size -= 1;
  262.         }
  263.     }
  264.  
  265. int DLinkedCircList::front() const{return head->getData();}
  266. int DLinkedCircList::back() const{return head->getPrev()->getData();}
  267.  
  268. //main.cpp code
  269.  
  270. #include <iostream>
  271. #include "ListAPI.hpp"
  272. #include "Node.hpp"
  273. #include "DLinkedCircList.hpp"
  274. #include <cstdlib>
  275. #include <random>
  276. using namespace std;
  277.  
  278. int main()
  279. {
  280.     DLinkedCircList thing{};
  281.     for (int i{}; i < 10; i++) {
  282.     thing.push_back(i+1);
  283.     }
  284.     cout << thing.toString() << thing.getSize() << endl;
  285.     cout << thing.toString() << thing.front() << thing.back() << endl;
  286.  
  287.    thing.reverse();
  288.    cout << thing.toString() << thing.getSize();
  289.     cout << thing.pop_back() << endl;
  290.     thing.clear();
  291.     cout << thing.toString() << thing.getSize() << endl;
  292.  
  293.         thing.push_back(5);
  294.     thing.push_back(6);
  295.     thing.push_back(7);
  296.  
  297.     cout << thing.toString() << thing.getSize() << endl;
  298.     return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement