Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This Linked List Program includes ListAPI.hpp, Node.hpp, Node.cpp, DLinkedCircList.hpp, DLinkedCircList.cpp, and main.cpp with test cases.
- //ListAPI.hpp code (pure abstract interface class)
- #ifndef LISTAPI_HPP
- #define LISTAPI_HPP
- #include <string>
- class ListAPI {
- virtual void push_back(int) = 0;
- virtual int pop_back() = 0;
- virtual void insert(int, int) = 0;
- virtual void erase(int) = 0;
- virtual std::string toString() const = 0;
- virtual int getSize() const = 0;
- virtual int at(int)const = 0;
- virtual void reverse() = 0;
- virtual int front()const = 0;
- virtual int back()const = 0;
- virtual void clear() = 0;
- };
- #endif // LISTAPI_HPP
- //Node.hpp code
- #ifndef NODE_HPP
- #define NODE_HPP
- class Node {
- public:
- Node(int _data);
- Node* getNext() const;
- Node* getPrev() const;
- void setNext(Node* ptr);
- void setPrev(Node* ptr);
- int getData() const;
- private:
- Node* prev;
- int data;
- Node* next;
- };
- #endif // NODE_HPP
- //Node.cpp code
- #include "Node.hpp"
- Node::Node(int _data)
- : prev{this}, data{_data}, next{this}
- {}
- Node* Node::getNext() const{return next;}
- Node* Node::getPrev() const{ return prev;}
- void Node::setNext(Node* ptr) {next = ptr;}
- void Node::setPrev(Node* ptr) {prev = ptr;}
- int Node::getData() const {return data;}
- //DLinkedCircList.hpp code
- #ifndef DLINKEDCIRCLIST_HPP
- #define DLINKEDCIRCLIST_HPP
- #include "Node.hpp"
- #include "ListAPI.hpp"
- #include <string>
- #include <iostream>
- #include <vector>
- class DLinkedCircList : public ListAPI {
- public:
- DLinkedCircList();
- ~DLinkedCircList();
- virtual std::string toString() const override;
- virtual void push_back(int val) override;
- virtual int pop_back() override;
- virtual int getSize() const override;
- virtual void clear() override;
- virtual int at(int index) const override;
- virtual void reverse() override;
- virtual void insert(int index, int val) override;
- virtual void erase(int index) override;
- virtual int front() const override;
- virtual int back() const override;
- private:
- Node* head;
- int size;
- };
- #endif // DLINKEDCIRCLIST_HPP
- //DLinkedCircList.cpp code
- #include "DLinkedCircList.hpp"
- DLinkedCircList::DLinkedCircList()
- : head{nullptr}, size{0}
- {}
- DLinkedCircList::~DLinkedCircList() {
- clear();
- }
- std::string DLinkedCircList::toString() const {
- std::string out{"["};
- if (head != nullptr) {
- Node* ptr{head};
- do {
- out += std::to_string(ptr->getData());
- if (ptr->getNext() != head){
- out += ", ";
- }
- ptr = ptr->getNext();
- } while (ptr != head);
- out += "]";
- }
- return out;
- }
- void DLinkedCircList::push_back(int val) {
- Node* newNode{new Node{val}};
- //error check to see if it was allocated
- if (head == nullptr) {
- head = newNode;
- }
- else { //add to the end
- Node* last{head->getPrev()};
- last->setNext(newNode); //sets the next ptr of the last node to the prev ptr of the new node
- newNode->setPrev(last); // sets the prev ptr of the new node to the address of the next ptr's
- //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
- newNode->setNext(head); //sets the next pointer of the new node to the prev pointer of the first object
- head->setPrev(newNode);
- }
- size += 1;
- }
- int DLinkedCircList::pop_back() {
- int value{0};
- if(size == 1){
- value = head->getData();
- delete head;
- head = nullptr;
- }
- else{
- Node* poppedNode{head->getPrev()};
- Node* newBack{head->getPrev()->getPrev()};
- value = head->getPrev()->getData();
- delete poppedNode;
- poppedNode = nullptr;
- newBack->setNext(head);
- head->setPrev(newBack);
- }
- size >= 1 ? size -= 1 : size = 0;
- return value;
- }
- int DLinkedCircList::getSize() const {return size;}
- void DLinkedCircList::clear() {
- for(int i{0}; i < size; i += 1){
- Node* current{head};
- head = head->getNext();
- delete current;
- current = nullptr;
- }
- head = nullptr;
- size = 0;
- }
- int DLinkedCircList::at(int index) const{
- int i{1};
- int valueAt{};
- Node* temp{head->getNext()};
- if (index == 0){
- valueAt = head->getData();
- }
- else {
- while(i < index){
- temp = temp->getNext();
- i += 1;
- }
- valueAt = temp->getData();
- }
- return valueAt;
- }
- void DLinkedCircList::reverse() {
- std::vector<int> tempVec{};
- for(int i{0}; i < size; i += 1){
- int tempData{at(i)};
- tempVec.push_back(tempData);
- }
- clear();
- for(int i{tempVec.size()-1}; i >= 0; i -= 1){
- int temp{tempVec[i]};
- push_back(temp);
- }
- }
- void DLinkedCircList::insert(int index, int val) {
- Node* newNode{new Node{val}};
- Node* temp{head->getPrev()};
- if (index == 0) {
- newNode->setPrev(head->getPrev());
- head->getPrev()->setNext(newNode);
- newNode->setNext(head);
- head->setPrev(temp);
- head = newNode;
- size += 1;
- }
- else if (index == size) {
- push_back(val);
- }
- else{
- temp = head;
- for(int i{0}; i < index; i += 1) {
- temp = temp->getNext();
- }
- Node* prevTemp{temp->getPrev()};
- newNode->setPrev(prevTemp);
- newNode->setNext(temp);
- temp->setPrev(newNode);
- prevTemp->setNext(newNode);
- size += 1;
- }
- }
- void DLinkedCircList::erase(int index) {
- Node* listEnd{head->getPrev()};
- if (index == 0) {
- listEnd->setNext(head->getNext());
- head = head->getNext();
- head->setPrev(listEnd);
- size -= 1;
- }
- else if (index == size - 1) {
- pop_back();
- }
- else{
- Node* temp{head};
- for(int i{0}; i < index; i += 1) {
- temp = temp->getNext();
- }
- Node* prevTemp{temp->getPrev()};
- Node* nextTemp{temp->getNext()};
- prevTemp->setNext(nextTemp);
- nextTemp->setPrev(prevTemp);
- delete temp;
- temp = nullptr;
- size -= 1;
- }
- }
- int DLinkedCircList::front() const{return head->getData();}
- int DLinkedCircList::back() const{return head->getPrev()->getData();}
- //main.cpp code
- #include <iostream>
- #include "ListAPI.hpp"
- #include "Node.hpp"
- #include "DLinkedCircList.hpp"
- #include <cstdlib>
- #include <random>
- using namespace std;
- int main()
- {
- DLinkedCircList thing{};
- for (int i{}; i < 10; i++) {
- thing.push_back(i+1);
- }
- cout << thing.toString() << thing.getSize() << endl;
- cout << thing.toString() << thing.front() << thing.back() << endl;
- thing.reverse();
- cout << thing.toString() << thing.getSize();
- cout << thing.pop_back() << endl;
- thing.clear();
- cout << thing.toString() << thing.getSize() << endl;
- thing.push_back(5);
- thing.push_back(6);
- thing.push_back(7);
- cout << thing.toString() << thing.getSize() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement