Created
October 21, 2016 11:14
-
-
Save geosteffanov/e96d604c0d7f839a54c85c7c1c5197b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class LinkedList | |
{ | |
private: | |
struct Node | |
{ | |
int data; | |
Node *next; | |
Node(int data = 0, Node* next = NULL) : data(data), next(next) | |
{} | |
}; | |
public: | |
LinkedList() : head(NULL), size(0) { } | |
LinkedList(const LinkedList& rhs) : head(NULL), size(0) | |
{ | |
copy(rhs); | |
} | |
LinkedList& operator=(const LinkedList& rhs) | |
{ | |
if (this == &rhs) | |
return *this; | |
deleteMe(); | |
copy(rhs); | |
return *this; | |
} | |
~LinkedList() | |
{ | |
deleteMe(); | |
} | |
void addHead(int value) | |
{ | |
Node* temp = new Node; | |
temp->data = value; | |
temp->next = head; | |
head = temp; | |
++size; | |
} | |
void addTail(int value) | |
{ | |
addAt(size, value); | |
} | |
void addAt(int index, int value) | |
{ | |
if (index == 0) | |
addHead(value); | |
else | |
{ | |
Node* previous = findPrev(index); | |
Node* temp = new Node; | |
temp->data = value; | |
temp->next = previous->next; | |
previous->next = temp; | |
++size; | |
} | |
} | |
int removeAt(int index) | |
{ | |
if (index >= size) | |
throw "Idiot!\n"; | |
if (index == 0) | |
return removeHead(); | |
Node* prev = findPrev(index); | |
int data = prev->next->data; | |
Node* nextNext = prev->next->next; | |
delete prev->next; | |
prev->next = nextNext; | |
--size; | |
return data; | |
} | |
bool isEmpty() const | |
{ | |
return size == 0; | |
} | |
int removeHead() | |
{ | |
if (!head) | |
throw "Basi!\n"; | |
int data = head->data; | |
Node* next = head->next; | |
delete head; | |
head = next; | |
--size; | |
return data; | |
} | |
int getAt(int index) | |
{ | |
if (index >= size) | |
throw "No!\n"; | |
if (index == 0) | |
return getHead(); | |
return (findPrev(index)->next->data); | |
} | |
int getHead() | |
{ | |
return head->data; | |
} | |
private: | |
void copy(const LinkedList& rhs) | |
{ | |
const Node* rhsNode = rhs.head; | |
while (rhsNode) | |
{ | |
addTail(rhsNode->data); | |
rhsNode = rhsNode->next; | |
} | |
} | |
void deleteMe() | |
{ | |
while (size) | |
removeHead(); | |
head = NULL; | |
} | |
Node* findPrev(int index) | |
{ | |
Node* prev = head; | |
while (--index > 0) | |
{ | |
prev = prev->next; | |
} | |
return prev; | |
} | |
private: | |
Node* head; | |
int size; | |
}; | |
int main() | |
{ | |
LinkedList list; | |
for (int i = 0; i < 10; ++i) | |
{ | |
list.addAt(i, i); | |
} | |
list.removeAt(9); | |
list.removeAt(6); | |
list.removeAt(3); | |
for (int i = 0; i < 7; ++i) | |
std::cout << list.getAt(i) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment