Created
November 20, 2025 14:45
-
-
Save rastr-0/bb67779676a5d577f1f4178c95a39f8d to your computer and use it in GitHub Desktop.
A simple double-ended queue C++ implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef DATATYPE_H | |
| #define DATATYPE_H | |
| typedef int Data; | |
| #endif // DATATYPE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "item.h" | |
| #include <iostream> | |
| Item::Item() | |
| : prev(nullptr), next(nullptr) | |
| { | |
| } | |
| Item::Item(Data d) | |
| : payload(d), prev(nullptr), next(nullptr) | |
| { | |
| } | |
| void Item::print() const{ | |
| std::cout << payload; | |
| } | |
| void Item::printLn() const{ | |
| print(); | |
| std::cout << std::endl; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef ITEM_H | |
| #define ITEM_H | |
| #include "datatype.h" | |
| class Item | |
| { | |
| Data payload; | |
| Item *prev; | |
| Item *next; | |
| public: | |
| Item(); | |
| Item(Data d); | |
| Data getData() const{return payload;} | |
| void setData(const Data d){payload = d;} | |
| void print() const; | |
| void printLn() const; | |
| friend class List; | |
| }; | |
| #endif // ITEM_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "list.h" | |
| #include "item.h" | |
| void List::initList() { | |
| first = new Item(); | |
| last = new Item(); | |
| } | |
| void List::deepCopy(List &src) { | |
| auto ptr = src.first->next; | |
| while (ptr != src.last) { | |
| this->pushBack(ptr->getData()); | |
| ptr = ptr->next; | |
| } | |
| } | |
| List::List() | |
| :first(new Item()), last(new Item()), counter(0) { | |
| initList(); | |
| } | |
| /* | |
| // shallow copy constructor | |
| // complexity O(1) ... constant | |
| List::List(List &other) | |
| : first(other.first), last(other.last), counter(other.counter) | |
| {} | |
| */ | |
| // deep copy constructor | |
| // O(n) ... linear | |
| List::List(List &other) | |
| : first(other.first), last(other.last), counter(other.counter) | |
| { | |
| initList(); | |
| deepCopy(other); | |
| } | |
| List::~List() { | |
| clean(); | |
| delete first; | |
| delete last; | |
| } | |
| /* | |
| // implicit version | |
| // shallow copy AND memory leaks | |
| List&::List::operator=(List& rhs) { | |
| this->first = rhs.first; | |
| this->last = rhs.last; | |
| // memory leak happens here | |
| this->counter = rhs.counter; | |
| } | |
| */ | |
| List&::List::operator=(List& rhs) { | |
| if (this != &rhs) { | |
| // clean data that are already in this List | |
| this->clean(); | |
| // making deep copy of rhs | |
| this->deepCopy(rhs); | |
| } | |
| // returning a pointer to this | |
| return *this; | |
| } | |
| // methods | |
| void List::pushBack(Data d) { | |
| // assign data | |
| last->setData(d); | |
| // create a new Item and assign to next | |
| last->next = new Item(); | |
| // assign new Item to the previous last | |
| last->next->prev = last; | |
| // move pointer to the last item to the newly created | |
| last = last->next; | |
| // increase counter | |
| counter++; | |
| } | |
| void List::pushFront(Data d) { | |
| first->setData(d); | |
| first->prev = new Item(); | |
| first->prev->next = first; | |
| first = first->prev; | |
| counter++; | |
| } | |
| void List::popFront() { | |
| // could be used C approach with global variable | |
| // errno, if usage of exceptions is limited | |
| if (isEmpty()) | |
| return; | |
| // move pointer to first item to the second one | |
| first = first->next; | |
| // free memory | |
| delete first->prev; | |
| // remove link to the original first item | |
| first->prev = nullptr; | |
| counter--; | |
| } | |
| void List::popBack() { | |
| if (isEmpty()) | |
| return; | |
| last = last->prev; | |
| delete last->next; | |
| last->next = nullptr; | |
| counter--; | |
| } | |
| Data List::back() const { | |
| return last->prev->getData(); | |
| } | |
| Data List::front() const { | |
| return first->next->getData(); | |
| } | |
| void List::print_while() const { | |
| auto ptr = first->next; | |
| while (ptr != last) { | |
| ptr->printLn(); | |
| ptr = ptr->next; | |
| } | |
| } | |
| void List::print_for() const { | |
| for (auto ptr = first->next; ptr != last; ptr = ptr->next) { | |
| ptr->printLn(); | |
| } | |
| } | |
| void List::clean() { | |
| while (!isEmpty()) { | |
| popBack(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef LIST_H | |
| #define LIST_H | |
| #include "item.h" | |
| class List | |
| // double-ended queue data structure | |
| { | |
| Item *first; | |
| Item *last; | |
| int counter; | |
| void initList(); | |
| void deepCopy(List &src); | |
| public: | |
| List(); | |
| List(List &src); | |
| ~List(); | |
| List& operator=(List& rhs); | |
| void pushBack(Data d); | |
| void pushFront(Data d); | |
| void popBack(); | |
| void popFront(); | |
| Data back() const; | |
| Data front() const; | |
| inline int getCounter() const { | |
| return counter; | |
| } | |
| inline bool isEmpty() const { | |
| // return (counter == 0); | |
| return not counter; | |
| } | |
| void print_while() const; | |
| void print_for() const; | |
| void clean(); | |
| }; | |
| #endif // LIST_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include "list.h" | |
| using namespace std; | |
| int main() | |
| { | |
| List lst; | |
| for (int i = 0; i < 7; i++) | |
| lst.pushBack((i + 10) % 3); | |
| for (int i = 8; i < 17; i++) | |
| lst.pushFront((i + 8) % 7); | |
| lst.print_for(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment