Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LINKED_LIST_NODE_HPP
- #define LINKED_LIST_NODE_HPP
- template <typename T>
- class Node{
- public:
- T data;
- Node* next;
- Node();
- Node(T);
- Node(const Node&);
- ~Node()
- };
- Node<T>::Node(){}
- Node<T>:: Node(const T data): data(data), next(nullptr){}
- Node<T>::Node(const Node& source){
- Node<T>* copy = new Node<T>(source.data))
- copy.next = nullptr;
- }
- ~Node(){
- delete this;
- }
- #endif //LINKED_LIST_NODE_HPP
- #ifndef LINKED_LIST_SINGLYLINKEDLIST_HPP
- #define LINKED_LIST_SINGLYLINKEDLIST_HPP
- #include <iostream>
- #include "node.hpp"
- template <typename T>
- class SinglyLinkedList {
- private:
- Node<T>* head;
- std::size_t count;
- public:
- SinglyLinkedList();
- SinglyLinkedList(const SinglyLinkedList& source);
- SinglyLinkedList& operator=(const SinglyLinkedList& source);
- ~SinglyLinkedList();
- void insert(T);
- void remove(T);
- bool isEmpty();
- int length();
- void print();
- };
- template <typename T>
- SinglyLinkedList<T>::SinglyLinkedList() : head(nullptr), count(0){}
- SinglyLinkedList<T>::SinglyLinkedList(const SinglyLinkedList& source){
- //This might not be correct
- Node<T> new_head = Node(source.head);
- while(head != nullptr){
- Node<T> prev = new_head;
- head = head.next;
- copy_head = Node(head);
- prev.next = new_head
- }
- }
- SinglyLinkedList<T>::SinglyLinkedList& operator=(const SinglyLinkedList& source){
- //not sure how to implment this.
- }
- template <typename T>
- SinglyLinkedList<T>::~SinglyLinkedList() {
- if(!isEmpty()){
- Node<T>* temp = head;
- Node<T>* prev = nullptr;
- while(temp->next != nullptr){
- prev = temp;
- temp = temp->next;
- delete prev;
- }
- delete temp;
- }
- }
- template <typename T>
- bool SinglyLinkedList<T>::isEmpty() {
- return head == nullptr;
- }
- template <typename T>
- void SinglyLinkedList<T>::insert(T item) {
- Node<T>* p = new Node<T>(item);
- p->next = head;
- head = p;
- count += 1;
- }
- template <typename T>
- void SinglyLinkedList<T>::remove(T item) {
- bool present = false;
- if (head->data == item){
- Node<T>* temp = head;
- head = head->next;
- delete(temp);
- count -= 1;
- return;
- }
- Node<T>* temp = head;
- while (temp->next != nullptr){
- if (temp->next->data == item){
- Node<T>* removable = temp->next;
- temp->next = temp->next->next;
- delete(removable);
- present = true;
- count -= 1;
- break;
- } else{
- temp = temp->next;
- }
- }
- if(!present){
- throw std::invalid_argument("item not present in list");
- }
- }
- template <typename T>
- int SinglyLinkedList<T>::length() {
- return count;
- }
- template <typename T>
- void SinglyLinkedList<T>::print() {
- if(isEmpty()){
- throw std::invalid_argument("Can't print an empty list!");
- }
- Node<T>* temp = head;
- while(temp != nullptr){
- if(temp->next != nullptr){
- std::cout<<temp->data;
- std::cout<<"->";
- }else{
- std::cout<<temp->data;
- }
- temp = temp->next;
- }
- std::cout<<std::endl;
- }
- #endif //LINKED_LIST_SINGLYLINKEDLIST_HPP
Add Comment
Please, Sign In to add comment