DLL
DLL
template<class T>
void DLinkedList<T>::add(int index, const T& e) {
/* Insert an element into the list at given index. */
if (index < 0 || index > count) {
return;
}
if (head == NULL) {
Node* newNode = new Node(e);
head = tail = newNode;
} else {
Node* itr = head;
for (int i = 0; i < index; i++) {
itr = itr->next;
}
if (index == 0) {
Node* newNode = new Node(e);
newNode->next = itr;
itr->previous = newNode;
head = newNode;
} else if (index == count) {
Node* newNode = new Node(e);
newNode->previous = tail;
tail->next = newNode;
tail = newNode;
} else {
Node* newNode = new Node(e);
newNode->next = itr;
newNode->previous = itr->previous;
itr->previous->next = newNode;
itr->previous = newNode;
}
}
count++;
}
template<class T>
int DLinkedList<T>::size() {
/* Return the length (size) of list */
return count;
}
2.
template<class T>
T DLinkedList<T>::get(int index) {
/* Give the data of the element at given index in the list. */
if (index < 0 || index > count) {
return -1;
}
Node* itr = head;
for (int i = 0; i < index; i++) {
itr = itr->next;
}
return itr->data;
template<class T>
bool DLinkedList<T>::empty() {
/* Check if the list is empty or not. */
if (head == NULL) {
return 1;
} else {
return 0;
}
}
template<class T>
int DLinkedList<T>::indexOf(const T& item) {
/* Return the first index wheter item appears in list, otherwise return -1 */
Node* itr = head;
for (int i = 0; i < count; i++) {
if (itr->data == item) {
return i;
}
itr = itr->next;
}
return -1;
}
template<class T>
bool DLinkedList<T>::contains(const T& item) {
/* Check if item appears in the list */
Node* itr = head;
for (int i = 0; i < count; i++) {
if (itr->data == item) {
return 1;
}
itr = itr->next;
}
return 0;
}
3.
/*
* TODO: Implement class Iterator's method
* Note: method remove is different from SLinkedList, which is the advantage of
DLinkedList
*/
template <class T>
DLinkedList<T>::Iterator::Iterator(DLinkedList<T> *pList, bool begin)
{
this->pList = pList;
if (begin) {
if (pList != NULL) {
this->current = pList->head;
index = 0;
} else {
this->current = NULL;
index = -1;
}
} else {
this->current = NULL;
if (pList != NULL) {
index = pList->size();
} else {
index = 0;
}
}
}
return *this;
}
template<class T>
T& DLinkedList<T>::Iterator::operator*()
{
if (current == NULL) {
throw out_of_range("Segmentation fault!");
}
return current->data;
}
template<class T>
void DLinkedList<T>::Iterator::remove()
{
/*
* TODO: delete Node in pList which Node* current point to.
* After that, Node* current point to the node before the node just
deleted.
* If we remove first node of pList, Node* current point to nullptr.
* Then we use operator ++, Node* current will point to the head of pList.
*/
if (current == NULL) {
throw out_of_range("Segmentation fault!");
}
int index = this->pList->indexOf(this->current->data);
if (index == 0) {
this->pList->removeAt(index);
current = NULL;
this->index = -1;
} else {
T e = this->pList->removeAt(index - 1);
this->index = index - 1;
current->data = e;
}
}
template<class T>
bool DLinkedList<T>::Iterator::operator!=(const DLinkedList::Iterator &iterator)
{
return (current != iterator.current) || (index != iterator.index);
}
template<class T>
typename DLinkedList<T>::Iterator& DLinkedList<T>::Iterator::operator++()
{
if (current == NULL) {
current = pList->head;
this->index = 0;
return *this;
}
current = current->next;
index++;
return *this;
}
template<class T>
typename DLinkedList<T>::Iterator DLinkedList<T>::Iterator::operator++(int)
{
Iterator temp = *this;
if (current == NULL) {
current = pList->head;
this->index = 0;
return temp;
}
current = current->next;
index++;
return temp;
}
4.
template <class T>
T DLinkedList<T>::removeAt(int index)
{
/* Remove element at index and return removed value */
if (index < 0 || index >= count) {
throw out_of_range("");
}
Node* itr = head;
for (int i = 0; i < index; i++) {
itr = itr->next;
}
Node* temp_ptr = itr;
T temp_data = itr->data;
if (head == temp_ptr) {
head = temp_ptr->next;
}
if (tail == temp_ptr) {
tail = temp_ptr->previous;
}
if (temp_ptr->next != NULL) {
temp_ptr->next->previous = temp_ptr->previous;
}
if (temp_ptr->previous != NULL) {
temp_ptr->previous->next = temp_ptr->next;
}
delete temp_ptr;
count--;
return temp_data;
}
template<class T>
void DLinkedList<T>::clear(){
/* Remove all elements in list */
Node* itr = head;
while (itr != NULL) {
Node* temp = itr;
itr = itr->next;
delete temp;
}
head = tail = NULL;
count = 0;
}
5.
DataLog::DataLog()
{
/*
* TODO: add the first state with 0
*/
logList.push_back(0);
currentState = logList.begin();
}
void DataLog::save()
{
/*
* TODO: This function will create a new state, copy the data of the
currentState
* and move the currentState Iterator to this new state. If there are
other states behind the
* currentState Iterator, we delete them all before creating a new state.
*/
while (currentState != prev(logList.end(), 1)) {
logList.pop_back();
}
logList.push_back(*currentState);
currentState++;
}
void DataLog::undo()
{
/*
* TODO: Switch to the previous state of the data
* If this is the oldest state in the log, nothing changes
*/
if (prev(currentState) != logList.end()) {
currentState = prev(currentState);
}
}
void DataLog::redo()
{
/*
* TODO: Switch to the latter state of the data
* If this is the latest state in the log, nothing changes
*/
if (next(currentState) != logList.end()) {
currentState = next(currentState);
}
}
6.
/*
struct ListNode {
int val;
ListNode *left;
ListNode *right;
ListNode(int x = 0, ListNode *l = nullptr, ListNode* r = nullptr) : val(x),
left(l), right(r) {}
};
*/
ListNode* reverse(ListNode* head, int a, int b) {
// To Do
if (a == b) {
return head;
}
ListNode* ptr_a = head;
ListNode* ptr_b = head;
for (int i = 1; i < a; i++) {
ptr_a = ptr_a->right;
}
for (int i = 1; i < b; i++) {
ptr_b = ptr_b->right;
}
ListNode* begin = ptr_a->left;
ListNode* end = ptr_b->right;
ListNode* curr = ptr_a;
ListNode* prev = begin;
if (begin != NULL) {
begin->right = ptr_b;
}
if (end != NULL) {
end->left = ptr_a;
}
while (curr != end) {
ListNode* next = curr->right;
curr->right = prev;
curr->left = next;
prev = curr;
curr = next;
}
ptr_a->right = end;
ptr_b->left = begin;
if (ptr_a == head) {
head = ptr_b;
}
return head;
}