|
| 1 | +/** |
| 2 | + * Author: TripleZ<[email protected]> |
| 3 | + * Date: 2018-10-10 |
| 4 | + * Brief: Linked list class. |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef _LINKEDLIST_HPP_ |
| 8 | +#define _LINKEDLIST_HPP_ |
| 9 | + |
| 10 | +#include <cstdio> |
| 11 | +#include "ListNode.hpp" |
| 12 | + |
| 13 | +class LinkedList { |
| 14 | +public: |
| 15 | + int size; |
| 16 | + int length; |
| 17 | + ListNode *head; |
| 18 | + LinkedList(); |
| 19 | + LinkedList(int size); |
| 20 | + ~LinkedList(); |
| 21 | + ListNode* FindElem(int elemVal); |
| 22 | + bool DeleteElem(ListNode *elem); |
| 23 | + bool DeleteLastElem(); |
| 24 | + bool InsertElemAtFront(int elemVal); |
| 25 | + bool InsertElemAtBack(int elemVal); |
| 26 | + void PrintList(); |
| 27 | +}; |
| 28 | + |
| 29 | +LinkedList::LinkedList() { |
| 30 | + this -> head = new ListNode(); |
| 31 | + this -> head->next = nullptr; |
| 32 | + this -> head->val = -1; |
| 33 | + this -> size = 10; // default |
| 34 | + this -> length = 0; |
| 35 | +} |
| 36 | + |
| 37 | +LinkedList::LinkedList(int size) { |
| 38 | + this -> head = new ListNode(); |
| 39 | + this -> head->next = nullptr; |
| 40 | + this -> head->val = -1; |
| 41 | + |
| 42 | + this -> size = size; |
| 43 | + this -> length = 0; |
| 44 | +} |
| 45 | + |
| 46 | +LinkedList::~LinkedList() { |
| 47 | + ListNode *p, *q; |
| 48 | + p = this -> head; |
| 49 | + while(p -> next != nullptr) { |
| 50 | + q = p -> next; |
| 51 | + p -> next = p -> next -> next; |
| 52 | + delete q; |
| 53 | + } |
| 54 | + delete head; |
| 55 | + this -> head = nullptr; |
| 56 | + this -> length = 0; |
| 57 | +} |
| 58 | + |
| 59 | +ListNode* LinkedList::FindElem(int elemVal) { |
| 60 | + ListNode *p; |
| 61 | + for (p = this -> head; p != nullptr; p = p -> next) { |
| 62 | + if (p -> val == elemVal) { |
| 63 | + return p; |
| 64 | + } |
| 65 | + } |
| 66 | + return nullptr; |
| 67 | +} |
| 68 | + |
| 69 | +bool LinkedList::DeleteElem(ListNode *elem) { |
| 70 | + ListNode *prev, *next; |
| 71 | + for (prev = this -> head; prev -> next != elem; prev = prev -> next); |
| 72 | + next = elem -> next; |
| 73 | + prev -> next = next; |
| 74 | + delete elem; |
| 75 | + this -> length --; |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +bool LinkedList::DeleteLastElem() { |
| 80 | + ListNode *prev, *elem; |
| 81 | + for (prev = this -> head; prev -> next -> next != nullptr; prev = prev -> next) ; |
| 82 | + elem = prev -> next; |
| 83 | + prev -> next = nullptr; |
| 84 | + delete elem; |
| 85 | + this -> length --; |
| 86 | + return true; |
| 87 | +} |
| 88 | + |
| 89 | +bool LinkedList::InsertElemAtFront(int elemVal) { |
| 90 | + ListNode *newNode = new ListNode(); |
| 91 | + newNode -> val = elemVal; |
| 92 | + newNode -> next = this -> head -> next; |
| 93 | + this -> head -> next = newNode; |
| 94 | + this -> length ++; |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +bool LinkedList::InsertElemAtBack(int elemVal) { |
| 99 | + ListNode *newNode = new ListNode(); |
| 100 | + newNode -> val = elemVal; |
| 101 | + ListNode *end; |
| 102 | + for (end = this -> head; end -> next != nullptr; end = end -> next); |
| 103 | + end -> next = newNode; |
| 104 | + newNode -> next = nullptr; |
| 105 | + this -> length ++; |
| 106 | + return true; |
| 107 | +} |
| 108 | + |
| 109 | +void LinkedList::PrintList() { |
| 110 | + ListNode *elem; |
| 111 | + printf("List: "); |
| 112 | + for (elem = this -> head -> next; elem -> next != nullptr; elem = elem -> next) { |
| 113 | + printf("%d - ", elem -> val); |
| 114 | + } |
| 115 | + printf("%d\n", elem -> val); |
| 116 | +} |
| 117 | + |
| 118 | +#endif |
0 commit comments