A few things:
- I tried to do it with smart pointers because I wanted to learn about them. I'm not sure I made the right choice of type, however (and started to regret it half-way through).
- This is for school, so some operations were required. I can't change main.cpp or the general layout.
insert()
works. However, I'm suspicious it's not working properly. I haven't implemented delete functionality yet.
Any comments / suggestions appreciated.
Here's my singly linked list.
I've also posted this on Reddit.
List.cpp
#include "List.h"
void List::reverse_print(std::shared_ptr<Link> curr) const {
if (curr) {
reverse_print(curr->next);
std::cout << curr->datum << '\n';
}
}
void List::push_front(int val) {
std::unique_ptr<Link> new_link(new Link(val));
new_link->next = root;
root = std::move(new_link);
}
void List::push_back(int val) {
std::unique_ptr<Link> new_link(new Link(val));
if (!root)
root = std::move(new_link);
else {
std::shared_ptr<Link> curr(root);
while (curr->next) {
curr = curr->next;
}
curr->next = std::move(new_link);
}
}
void List::insert(int val, int loc){
if (loc > length( ) + 1 || loc < 1)
std::cout << "Insertion location is invalid.\n";
else {
if (loc == 1)
push_front(val);
else if (loc == length() + 1)
push_back(val);
else {
int node_count = 1;
std::shared_ptr<Link> ptr(root);
while (node_count < loc - 1) {
ptr = ptr->next;
++node_count;
}
std::shared_ptr<Link> new_link(new Link(val));
ptr->next = new_link;
}
}
}
int List::length() const {
int node_count = 0;
std::shared_ptr<Link> ptr(root);
while (ptr) {
ptr = ptr->next;
++node_count;
}
return node_count;
}
void List::print() const {
std::shared_ptr<Link> temp(root);
if (!temp) {
std::cout << "List is empty.\n";
return;
}
std::cout << "Beginning.\n";
while (temp) {
std::cout << temp->datum << '\n';
temp = temp->next;
}
std::cout << "Ending.\n"
"Length of list : " << length() << " nodes.\n";
}
void List::reverse_print() const {
std::cout << "Ending.\n";
reverse_print(get_first());
std::cout << "Beginning.\n"
"Length of list : " << length() << " nodes.\n";
}
void List::initialize_list() const {
if (this) // not sure what to put here
std::cout << "Empty list exists.\n";
}
std::shared_ptr<List::Link> List::get_first() const {
return root;
}
void List::destroy_list() {
root.reset();
};
bool List::empty( ) const {
return root == nullptr;
}
/*
void delete_link( ){ }
void delete_first( ) { }
void delete_last( ) { }
*/
List.h
#ifndef LIST_H
#define LIST_H
#include <memory>
#include <iostream>
class List {
struct Link {
int datum;
std::shared_ptr<Link> next;
explicit Link(int val = 0) : datum{ val } {
std::cout << "Link constructor invoked.\n";
}
~Link() {
std::cout << "Link destructor invoked.\n";
}
};
std::shared_ptr<Link> root;
void reverse_print(std::shared_ptr<Link>) const;
public:
explicit List() : root{ nullptr } {
std::cout << "List constructor invoked.\n";
}
~List() {
std::cout << "List destructor invoked.\n";
}
void push_front(int);
void push_back(int);
void insert(int, int);
int length() const;
void print() const;
void reverse_print() const;
void initialize_list() const;
std::shared_ptr<Link> get_first( ) const;
void destroy_list( );
bool empty( ) const;
//void delete_first( );
//void delete_last( );
//bool delete_link(int val);
};
#endif
Main.cpp
#include "Menu.h"
#include "List.h"
int main() {
Menu menu;
std::unique_ptr<List>list(new List());
do {
menu.display();
menu.query_user();
menu.process_command(list);
} while (menu.cont());
}