Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "LList.h"
- #include <string>
- using namespace std;
- LinkedList::~LinkedList(){
- ListNode* current = head;
- while (current != nullptr){
- ListNode* temp = current->next;
- delete current;
- current = temp;
- }
- };
- bool LinkedList::isEmpty() const{
- if (head == nullptr){
- return true;
- }
- return false;
- };
- void LinkedList::insertAtFront(const string & elem){
- ListNode* node = new ListNode(elem);
- node->next = head;
- head = node;
- };
- void LinkedList::insertAtBack(const string & elem){
- ListNode* node = new ListNode(elem);
- last->next = node;
- node->next = nullptr;
- };
- bool LinkedList::removeFromFront(string & output){
- output = head->value;
- if (head != nullptr){
- ListNode* temp = head->next;
- delete head;
- head = temp;
- return true;
- }
- return false;
- };
- bool LinkedList::removeFromBack(string & output){
- output = last->value;
- if (head != nullptr){
- ListNode* nexToEnd = last;
- last = last->next;
- delete last;
- nexToEnd = nullptr;
- return true;
- }
- else {
- delete head;
- head = nullptr;
- }
- return false;
- };
- ostream & operator<<(ostream & stream, const LinkedList & list){
- ListNode* current = list.head;
- while (current != nullptr){
- stream << current->getValue() << endl;
- current = (list.head)->getNext();
- }
- };
- ListNode* LinkedList::search(const string & value){
- ListNode* current = head;
- while (current != nullptr){
- if (current->value == value){
- return current;
- break;
- }
- current = head->next;
- }
- };
- void LinkedList::remove(const string & value){
- ListNode* current = head;
- while (current != nullptr){
- if (current->value == value){
- delete current;
- }
- current = head->next;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement