Linked List
Linked List
sum = a + b + carry;
carry = 0;
if (sum > 9) {
sum %= 10;
carry = 1;
}
LLNode* l3 = new LLNode(sum, head);
head = l3;
2.
void Polynomial::insertTerm(const Term& term) {
// STUDENT ANSWER
if (term.exp < 0 || term.coeff == 0) {
return;
}
SLinkedList<Term>::Iterator it;
int i = 0;
for (it = this->terms->begin(); it != this->terms->end(); it++) {
Term curr = *it;
if(curr.exp == term.exp){
curr.coeff += term.coeff;
if(curr.coeff != 0) {
it.set(curr);
}
else it.remove();
return;
}
else if(curr.exp < term.exp){
break;
}
i++;
}
this->terms->add(i,term);
}
3.
template <class T>
SLinkedList<T>::Iterator::Iterator(SLinkedList<T>* pList, bool begin)
{
/*
Constructor of iterator
* Set pList to pList
* begin = true:
* * Set current (index = 0) to pList's head if pList is not NULL
* * Otherwise set to NULL (index = -1)
* begin = false:
* * Always set current to NULL
* * Set index to pList's size if pList is not NULL, otherwise 0
*/
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;
}
4.
void LinkedList::partition(int k) {
Node* temp = head;
Node* irr = head;
Node* prev = NULL;
int count = 0;
5.
template <class T>
void SLinkedList<T>::add(const T& e) {
/* Insert an element into the end of the list. */
if (head == NULL) {
Node* newNode = new Node(e, NULL);
head = newNode;
tail = newNode;
count++;
} else {
Node* irr = head;
while (irr->next != NULL) {
irr = irr->next;
}
Node* newNode = new Node(e, NULL);
irr->next = newNode;
tail = newNode;
count++;
}
}
template<class T>
void SLinkedList<T>::add(int index, const T& e) {
/* Insert an element into the list at given index. */
if (head == NULL) {
Node* newNode = new Node(e, NULL);
head = newNode;
tail = newNode;
count++;
} else {
Node* irr = head;
if (index == 0) {
Node* newNode = new Node(e, irr);
head = newNode;
count++;
} else {
for(int i = 0; i < index - 1; i++) {
irr = irr->next;
}
Node* newNode = new Node(e, irr->next);
irr->next = newNode;
if (index == count) {
tail = newNode;
}
count++;
}
}
template<class T>
int SLinkedList<T>::size() {
/* Return the length (size) of list */
return count;
}
6.
template<class T>
T SLinkedList<T>::get(int index) {
/* Give the data of the element at given index in the list. */
if (index < 0 || index >= count) {
throw out_of_range("");
}
Node* irr = head;
for (int i = 0; i < index; i++) {
irr = irr->next;
}
return irr->data;
template<class T>
bool SLinkedList<T>::empty() {
/* Check if the list is empty or not. */
if (count == 0) {
return true;
} else {
return false;
}
return false;
}
template<class T>
int SLinkedList<T>::indexOf(const T& item) {
/* Return the first index wheter item appears in list, otherwise return -1 */
Node* irr = head;
int index = 0;
while (irr != NULL) {
if (irr->data == item) {
return index;
}
irr = irr->next;
index++;
}
return -1;
}
template<class T>
bool SLinkedList<T>::contains(const T& item) {
/* Check if item appears in the list */
Node* irr = head;
while (irr != NULL) {
if (irr->data == item) {
return true;
}
irr = irr->next;
}
return false;
}
7.
template <class T>
T SLinkedList<T>::removeAt(int index)
{
/* Remove element at index and return removed value */
if (index < 0 || index >= count) {
throw out_of_range("");
}
Node* irr = head;
T temp_data;
if (index == 0) {
temp_data = head->data;
head = head->next;
count--;
delete irr;
return temp_data;
} else {
for (int i = 0; i < index - 1; i++) {
irr = irr->next;
}
if (index == count - 1) {
tail = irr;
}
temp_data = irr->next->data;
Node* temp_ptr = irr->next;
irr->next = irr->next->next;
count--;
delete temp_ptr;
return temp_data;
}
return 0;
}
template<class T>
void SLinkedList<T>::clear(){
/* Remove all elements in list */
Node* irr = head;
while (irr != NULL) {
Node* temp = irr;
irr = irr->next;
delete temp;
}
count = 0;
}
8.
LLNode* reverseLinkedList(LLNode* head) {
// STUDENT ANSWER
LLNode* curr = head;
LLNode* prev = NULL, * next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
return head;
}
9.
LLNode* rotateLinkedList(LLNode* head, int k) {
// STUDENT ANSWER
if (head == NULL) {
return head;
}
LLNode* irr = head;
LLNode* tail = NULL;
int size = 0;
while (irr != NULL) {
irr = irr->next;
size++;
if (irr->next == NULL) {
size++;
tail = irr;
break;
}
}
if (k > size) {
k %= size;
if (k == 0) {
return head;
}
}
int head_index = size - k;
int tail_index = head_index - 1;
LLNode* temp_head = head;
LLNode* temp_tail = head;
for (int i = 0; i < head_index; i++) {
temp_head = temp_head->next;
}
for (int i = 0; i < tail_index; i++) {
temp_tail = temp_tail->next;
}
tail->next = head;
temp_tail->next = NULL;
head = temp_head;
return head;
}