Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #pragma once
  2.  
  3. // Provides I/O
  4. #include <iostream>
  5. #include <sstream>
  6. #include <string>
  7.  
  8. //------------------------------------------------------------
  9. // ListNode class
  10. // ListNodes are singly-linked.
  11. //------------------------------------------------------------
  12. template <typename T>
  13. class ListNode {
  14.  public:
  15.   ListNode(const T& data) {
  16.     _data = data;
  17.     _next = NULL;
  18.   }
  19.  
  20.   ListNode(const T& data, ListNode* next) {
  21.     _data = data;
  22.     _next = next;
  23.   }
  24.  
  25.   const T& data() const { return _data; }
  26.   const ListNode* next() const { return _next; }
  27.   ListNode* next() { return _next; }
  28.   void setNext(ListNode* next) { _next = next; }
  29.  
  30.  private:
  31.   T _data;
  32.   ListNode* _next;
  33. };
  34.  
  35. //------------------------------------------------------------
  36. // LinkedList class
  37. //------------------------------------------------------------
  38. template <typename T>
  39. class LinkedList {
  40.  public:
  41.   LinkedList() {
  42.     _head = NULL;
  43.   }
  44.  
  45.   // Inserts a node to head of linked list
  46.   void insert(ListNode<T>* node) {
  47.     if(_head == NULL){
  48.       node->setNext(NULL);
  49.     }
  50.     else{
  51.       node->setNext(_head);
  52.     }
  53.     _head = node;
  54.   }
  55.    
  56.     //Returns the node with passed in value key
  57.     ListNode<T>* find(const T& key) {
  58.         ListNode<T>* currentNode = _head;
  59.         while(currentNode != NULL && currentNode->data() != key){
  60.             currentNode = currentNode->next();  
  61.         }
  62.         return currentNode;
  63.     }
  64.  
  65.     //Returns the node before the key
  66.     ListNode<T>* findPreviousNode(const T& key){
  67.         ListNode<T>* previousNode;
  68.         ListNode<T>* currentNode = _head;
  69.    
  70.         while(currentNode != NULL && currentNode->data() != key){
  71.             previousNode = currentNode;
  72.             currentNode = currentNode->next();  
  73.         }
  74.         return previousNode;
  75.     }
  76.  
  77.     // Removes and deletes the first node in the linked list that has data
  78.     // equal to the  key
  79.     void remove(const T& key) {
  80.         ListNode<T>* nodeToDelete = find(key);
  81.         ListNode<T>* previousNode = findPreviousNode(key);
  82.         ListNode<T>* nextNode = nodeToDelete->next();
  83.         delete nodeToDelete;
  84.         previousNode->setNext(nextNode);
  85.                
  86.     }
  87.  
  88.   friend std::ostream& operator<<(std::ostream& out, const LinkedList& list) {
  89.     ListNode<T>* node = list._head;
  90.     while (node != NULL) {
  91.       out << node->data();
  92.       if (node->next() != NULL) {
  93.         out << " -> ";
  94.       }
  95.       node = node->next();
  96.     }
  97.     return out;
  98.   }
  99.  
  100.   std::string toString() const {
  101.     std::stringstream ss;
  102.     ss << *this;
  103.     return ss.str();
  104.   }
  105.  
  106.  private:
  107.   ListNode<T>* _head;
  108. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement