|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | + |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +class List |
| 7 | +{ |
| 8 | +private: |
| 9 | + typedef struct Node |
| 10 | + { |
| 11 | + int data; |
| 12 | + struct Node* next; |
| 13 | + struct Node* prev; |
| 14 | + } node; |
| 15 | + |
| 16 | + node* list() |
| 17 | + { |
| 18 | + node* head = NULL; |
| 19 | + head = (node*)malloc(sizeof(node)); |
| 20 | + |
| 21 | + head->data = 0; // This just initializes the list the first time, so it's ignored. |
| 22 | + head->next = NULL; |
| 23 | + head->prev = NULL; |
| 24 | + |
| 25 | + return head; |
| 26 | + } |
| 27 | + |
| 28 | + // Private variables. |
| 29 | + int m_size = 0; |
| 30 | + int m_maxIndex = m_size - 1; |
| 31 | + node* m_head = list(); |
| 32 | + node* m_first = m_head; // Sets the first list node to the current state of m_head |
| 33 | + node* m_last = m_head; |
| 34 | + |
| 35 | +public: |
| 36 | + // prints list |
| 37 | + void print() |
| 38 | + { |
| 39 | + m_head = m_first; |
| 40 | + // The list function initializes the list |
| 41 | + // with 0, so when the list is printed, we skip the first one. |
| 42 | + if(m_size != 0) |
| 43 | + { |
| 44 | + if(m_head->next != NULL) |
| 45 | + { |
| 46 | + m_head = m_head->next; |
| 47 | + } |
| 48 | + while(m_head != NULL) |
| 49 | + { |
| 50 | + printf("%d, ", m_head->data); |
| 51 | + m_head = m_head->next; |
| 52 | + } |
| 53 | + } else |
| 54 | + { |
| 55 | + printf("[-] Nothing in list.\n"); |
| 56 | + exit(0); |
| 57 | + } |
| 58 | + printf("\n"); |
| 59 | + } |
| 60 | + |
| 61 | + // removes element from given index |
| 62 | + int remell(int index) |
| 63 | + { |
| 64 | + m_head = m_first; |
| 65 | + if(index != 0) |
| 66 | + { |
| 67 | + m_head = m_head->next; // skip the first one, it's just the initializer. |
| 68 | + } |
| 69 | + if(m_head->next == NULL) |
| 70 | + { |
| 71 | + cout << "ERROR: NULL at index " << index << ".\n"; |
| 72 | + exit(1); |
| 73 | + } |
| 74 | + if(index > m_size - 1) |
| 75 | + { |
| 76 | + cout << "[ERROR]: List is empty!\n"; |
| 77 | + exit(1); |
| 78 | + } |
| 79 | + |
| 80 | + for(int i = 0; i <= index; i++) |
| 81 | + { |
| 82 | + if (i == index - 1 || index == 0) |
| 83 | + { |
| 84 | + if(m_head != NULL) |
| 85 | + { |
| 86 | + node* targetHead = m_head->next; |
| 87 | + int data = targetHead->data; |
| 88 | + if(targetHead->next != NULL) |
| 89 | + { |
| 90 | + node* nextHead = targetHead->next; |
| 91 | + nextHead->prev = m_head; // New head-prev pointer = current head |
| 92 | + m_head->next = nextHead; // current head-next pointer = nextHead |
| 93 | + |
| 94 | + free(targetHead); |
| 95 | + m_size--; |
| 96 | + return data; |
| 97 | + } |
| 98 | + // Target head is last item in list, do this... |
| 99 | + else |
| 100 | + { |
| 101 | + m_head->next = NULL; |
| 102 | + free(targetHead); |
| 103 | + m_size--; |
| 104 | + return data; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + m_head = m_head->next; |
| 109 | + } |
| 110 | + cout << "[ERROR]: Something went wrong while removing element from index " << index << endl; |
| 111 | + cout << "Make sure list is not empty, and index is not out of range.\n"; |
| 112 | + exit(1); |
| 113 | + } |
| 114 | + |
| 115 | + int index(int index) |
| 116 | + { |
| 117 | + m_head = m_first; |
| 118 | + if (index > m_size - 1) |
| 119 | + { |
| 120 | + cout << "[ERROR] No element at index " << index << "!!!\n"; |
| 121 | + cout << "The length of the list you are accessing is only " << m_size << endl; |
| 122 | + exit(1); |
| 123 | + } |
| 124 | + if(m_head->next != NULL) |
| 125 | + { |
| 126 | + m_head = m_head->next; |
| 127 | + } |
| 128 | + for(int i = 0; i <= index; i++) |
| 129 | + { |
| 130 | + if(i == index) |
| 131 | + { |
| 132 | + return m_head->data; |
| 133 | + } |
| 134 | + m_head = m_head->next; |
| 135 | + } |
| 136 | + exit(1); |
| 137 | + } |
| 138 | + |
| 139 | + // appends to list |
| 140 | + void append(int value) |
| 141 | + { |
| 142 | + |
| 143 | + node* nextNode = list(); |
| 144 | + m_last->next = nextNode; |
| 145 | + nextNode->prev = m_last; |
| 146 | + nextNode->data = value; |
| 147 | + m_last = nextNode; |
| 148 | + |
| 149 | + m_size++; |
| 150 | + } |
| 151 | + |
| 152 | + int size() |
| 153 | + { |
| 154 | + return m_size; |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | + |
0 commit comments