I can’t figure out why my print functions won’t work.
It seems that when i create objects of the list they do not point at each other as expected.
Somehow when those objects are created they will point to nullptr in both directions.
#include <iostream>
#include <string>
#include <stdexcept>
template <typename T>
class doubly_list {
private:
doubly_list* next_node;
doubly_list* prev_node;
T value = T();
public :
doubly_list(const T& nvalue, doubly_list* next = nullptr, doubly_list* prev = nullptr)
: value(nvalue), next_node(next), prev_node(prev) {
}
doubly_list* getnext_node() const { return next_node; }
doubly_list* getprev_node() const { return prev_node; }
T getlist_value() const { return value; }
doubly_list* move_to_the_next_node() const { return next_node; }
doubly_list* move_to_the_prev_node() const { return prev_node; }
doubly_list* insert(doubly_list* next); // adding next before prev
doubly_list* insert(doubly_list* prev, doubly_list* next); // adding next before prev
void print_the_list(doubly_list<T>* head) const; //print the list
};
//adding next before prev
template<typename T>
doubly_list<T>* doubly_list<T>::insert(doubly_list* next) {
if (this == nullptr) return next;
if (next == nullptr) return nullptr;
next->next_node = this;
if (this->prev_node) this->prev_node->next_node = next;
this->prev_node = next->prev_node;
this->prev_node = next;
return next;
}
//next before prev
template<typename T>
doubly_list<T>* doubly_list<T>::insert(doubly_list* prev ,doubly_list* next) {
if (prev == nullptr) return next;
if (next == nullptr) return nullptr;
doubly_list<T>* current_node = next;
current_node->next_node = prev;
prev->prev_node = current_node;
current_node->prev_node = nullptr;
if (prev->prev_node != nullptr) {
prev->prev_node->next_node = current_node;
prev->prev_node = next->prev_node;
}
return next;
}
//printing the list
template<typename T>
void doubly_list<T>::print_the_list(doubly_list<T>* head) const {
//checking if the list is valid
if (!head) throw std::invalid_argument("No members found in the list");
//actual output
if (head->next_node == nullptr) {
std::cout << "{" << std::endl;
while (head) {
std::cout << head->value;
head = head->prev_node;
}
std::cout << "}" << std::endl;
}
else if (head->prev_node == nullptr) {
std::cout << "{";
while (head) {
std::cout << head->value;
head = head->next_node;
}
std::cout << "}" << std::endl;
}
}
int main () {
auto head = new doubly_list<std::string>("1");
head->insert(head ,new doubly_list<std::string>("2"));
head->insert(head ,new doubly_list<std::string>("3"));
head->insert(head ,new doubly_list<std::string>("4"));
head->print_the_list(head);
return 0;
}
I am learning c++ with Bjarne Stroustrup’s Principles and Practice book and i wanted to implement this using templates but i was following book’s guidence .
I thought that it must be some kind of a logical problem , so i’ve written those functions couple of times but it was pointless .
Warerre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1