0% found this document useful (0 votes)
10 views10 pages

Linked List

Uploaded by

Hello Duy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Linked List

Uploaded by

Hello Duy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

LLNode* reverse(LLNode* head) {


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;
}

LLNode* addLinkedList(LLNode* l0, LLNode* l1) {


// STUDENT ANSWER
LLNode* head = NULL;
int carry = 0, sum = 0, a, b;
while (l0 != NULL || l1 != NULL) {
if (l0 != NULL) {
a = l0->val;
}
else {
a = 0;
}
if (l1 != NULL) {
b = l1->val;
}
else {
b = 0;
}

sum = a + b + carry;
carry = 0;
if (sum > 9) {
sum %= 10;
carry = 1;
}
LLNode* l3 = new LLNode(sum, head);
head = l3;

if (l0 != NULL) l0 = l0->next;


if (l1 != NULL) l1 = l1->next;

if (l0 == NULL && l1 == NULL && carry == 1) {


LLNode* l3 = new LLNode(1, head);
head = l3;
}
}
head = reverse(head);
return head;
}

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);
}

void Polynomial::insertTerm(double coeff, int exp) {


// STUDENT ANSWER
if (exp < 0 || 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 == exp){
curr.coeff += coeff;
if(curr.coeff != 0) {
it.set(curr);
}
else it.remove();
return;
}
else if(curr.exp < exp){
break;
}
i++;
}
Term t(coeff, exp);
this->terms->add(i, t);
}

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;
}
}
}

template <class T>


typename SLinkedList<T>::Iterator& SLinkedList<T>::Iterator::operator=(const
Iterator& iterator)
{
/*
Assignment operator
* Set this current, index, pList to iterator corresponding elements.
*/
this->current = iterator.current;
this->index = iterator.index;
this->pList = iterator.pList;

return *this;
}

template <class T>


void SLinkedList<T>::Iterator::remove()
{
/*
Remove a node which is pointed by current
* After remove current points to the previous node of this position (or
node with index - 1)
* If remove at front, current points to previous "node" of head (current =
NULL, index = -1)
* Exception: throw std::out_of_range("Segmentation fault!") if remove when
current is NULL
*/
if (current == NULL) {
throw out_of_range("Segmentation fault!");
}
int index = this->pList->indexOf(this->current->data);
if (index == 0) {
this->pList->removeAt(index);
this->index = -1;
current = NULL;
} else {
T e = this->pList->removeAt(index - 1);
this->index = index - 1;
current->data = e;
}
}

template <class T>


void SLinkedList<T>::Iterator::set(const T& e)
{
/*
Set the new value for current node
* Exception: throw std::out_of_range("Segmentation fault!") if current is
NULL
*/
if (current == NULL) {
throw out_of_range("Segmentation fault!");
}
current->data = e;
}

template <class T>


T& SLinkedList<T>::Iterator::operator*()
{
/*
Get data stored in current node
* Exception: throw std::out_of_range("Segmentation fault!") if current is
NULL
*/
if (current == NULL) {
throw out_of_range("Segmentation fault!");
}
return current->data;
}

template <class T>


bool SLinkedList<T>::Iterator::operator!=(const Iterator& iterator)
{
/*
Operator not equals
* Returns false if two iterators points the same node and index
*/
return (current != iterator.current) || (index != iterator.index);
}
// Prefix ++ overload
template <class T>
typename SLinkedList<T>::Iterator& SLinkedList<T>::Iterator::operator++()
{
/*
Prefix ++ overload
* Set current to the next node
* If iterator corresponds to the previous "node" of head, set it to head
* Exception: throw std::out_of_range("Segmentation fault!") if iterator
corresponds to the end
*/
if (current == NULL) {
throw out_of_range("Segmentation fault!");
}
current = current->next;
index++;
return *this;
}
// Postfix ++ overload
template <class T>
typename SLinkedList<T>::Iterator SLinkedList<T>::Iterator::operator++(int)
{
/*
Postfix ++ overload
* Set current to the next node
* If iterator corresponds to the previous "node" of head, set it to head
* Exception: throw std::out_of_range("Segmentation fault!") if iterator
corresponds to the end
*/
Iterator temp = *this;
if (current == NULL) {
throw out_of_range("Segmentation fault!");
}
current = current->next;
index++;
return temp;
}

4.
void LinkedList::partition(int k) {
Node* temp = head;
Node* irr = head;
Node* prev = NULL;
int count = 0;

while (irr != NULL) {


if (irr->value < k && count == 1) {
Node* newNode = new Node(irr->value, NULL);
prev->next = newNode;
prev = newNode;
tail = newNode;
}
if (irr->value < k && count == 0) {
Node* newNode = new Node(irr->value, NULL);
prev = newNode;
head = newNode;
count++;
}
irr = irr->next;
}
irr = temp;
while (irr != NULL) {
if (irr->value == k && count == 1) {
Node* newNode = new Node(irr->value, NULL);
prev->next = newNode;
prev = newNode;
tail = newNode;
}
if (irr->value == k && count == 0) {
Node* newNode = new Node(irr->value, NULL);
prev = newNode;
head = newNode;
count++;
}
irr = irr->next;
}
irr = temp;
while (irr != NULL) {
if (irr->value > k && count == 1) {
Node* newNode = new Node(irr->value, NULL);
prev->next = newNode;
prev = newNode;
tail = newNode;
}
if (irr->value > k && count == 0) {
Node* newNode = new Node(irr->value, NULL);
prev = newNode;
head = newNode;
count++;
}
irr = irr->next;
}
}

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>


void SLinkedList<T>::set(int index, const T& e) {
/* Assign new value for 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;
}
irr->data = e;
}

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>


bool SLinkedList<T>::removeItem(const T& item)
{
/* Remove the first apperance of item in list and return true, otherwise return
false */
Node* irr = head;
int index = 0;
bool found = false;
while (irr != NULL) {
if (irr->data == item) {
found = true;
break;
}
irr = irr->next;
index++;
}
if (found) {
removeAt(index);
}
return found;
}

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;
}

You might also like