Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include "LList.h"
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. LinkedList::~LinkedList(){
  9.     ListNode* current = head;
  10.     while (current != nullptr){
  11.         ListNode* temp = current->next;
  12.         delete current;
  13.         current = temp;
  14.     }
  15. };
  16.  
  17. bool LinkedList::isEmpty() const{
  18.     if (head == nullptr){
  19.         return true;
  20.     }
  21.     return false;
  22. };
  23.  
  24. void LinkedList::insertAtFront(const string & elem){
  25.     ListNode* node = new ListNode(elem);
  26.     node->next = head;
  27.     head = node;
  28. };
  29.  
  30. void LinkedList::insertAtBack(const string & elem){
  31.     ListNode* node = new ListNode(elem);
  32.     last->next = node;
  33.     node->next = nullptr;
  34. };
  35.  
  36. bool LinkedList::removeFromFront(string & output){
  37.     output = head->value;
  38.     if (head != nullptr){
  39.         ListNode* temp = head->next;
  40.         delete head;
  41.         head = temp;
  42.         return true;
  43.     }
  44.     return false;
  45. };
  46.  
  47. bool LinkedList::removeFromBack(string & output){
  48.     output = last->value;
  49.     if (head != nullptr){
  50.         ListNode* nexToEnd = last;
  51.         last = last->next;
  52.         delete last;
  53.         nexToEnd = nullptr;
  54.         return true;
  55.     }
  56.     else {
  57.         delete head;
  58.         head = nullptr;
  59.     }
  60.     return false;
  61. };
  62.  
  63. ostream & operator<<(ostream & stream, const LinkedList & list){
  64.     ListNode* current = list.head;
  65.     while (current != nullptr){
  66.         stream << current->getValue() << endl;
  67.         current = (list.head)->getNext();
  68.     }
  69. };
  70.  
  71. ListNode* LinkedList::search(const string & value){
  72.     ListNode* current = head;
  73.     while (current != nullptr){
  74.         if (current->value == value){
  75.             return current;
  76.             break;
  77.         }
  78.         current = head->next;
  79.     }
  80.  
  81. };
  82.  
  83. void LinkedList::remove(const string & value){
  84.     ListNode* current = head;
  85.     while (current != nullptr){
  86.         if (current->value == value){
  87.             delete current;
  88.         }
  89.         current = head->next;
  90.     }
  91. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement