CS1020E Tutorial 6
Qn 4
Jumping LinkedList
Ok...Lets start with the simpler stuff..
#ifndef k_list_node_h
#define k_list_node_h
class KListNode {
private:
int _item;
int _k; // I didn't really use this since we're assuming a constant k for all nodes.
KListNode* _kNext;
KListNode* _next;
public:
KListNode(int item, int k);
int getItem();
KListNode* getKNext();
KListNode* getNext();
void setKNext(KListNode* listNode);
void setNext(KListNode* listNode);
};
#endif
#include <iostream>
#include "KListNode.h"
KListNode::KListNode(int item, int k) {
this->_item = item;
this->_k = k;
this->_kNext = NULL;
this->_next = NULL;
}
int KListNode::getItem() {
return this->_item;
}
KListNode* KListNode::getKNext() {
return this->_kNext;
}
KListNode* KListNode::getNext() {
return this->_next;
}
void KListNode::setNext(KListNode* listNode) {
this->_next = listNode;
}
void KListNode::setKNext(KListNode* listNode) {
this->_kNext = listNode;
}
OK now the complicated stuff...
#ifndef list_h
#define list_h
#include "KListNode.h"
class List {
private:
KListNode* _head;
int _size;
int _skipInt;
public:
List(int skipInt);
bool insertKListNode(int index, const int& newItem);
bool removeKListNode(int index);
void pushBack(int &item);
void printList();
KListNode* traverseTo(int index);
};
#endif
Your usual List specifications...
KListNode* List::traverseTo(int index) {
if(index < 0 || index > this->_size) {
return NULL;
} else {
KListNode* ptr;
ptr = this->_head;
for(int i = 1; i < index; ++i) {
if(index - i >= this->_skipInt) {
ptr = ptr->getKNext();
i += this->_skipInt - 1;
} else {
ptr = ptr->getNext();
}
}
return ptr;
}
}
List::traverseTo(int index)
for(int i = 1; i < index; ++i) {
if(index - i >= this->_skipInt) {
ptr = ptr->getKNext();
i += this->_skipInt - 1;
} else {
ptr = ptr->getNext();
}
}
bool List::insertKListNode(int index, const int& newItem) {
int newLength = this->_size + 1;
if(index < 1 || index > newLength) {
return false;
} else {
KListNode* ptr = new KListNode(newItem, this->_skipInt);
KListNode* cur;
this->_size = newLength;
if(index == 1) {
ptr->setNext(this->_head);
cur = ptr;
for(int i = 0; i < this->_skipInt; ++i) {
cur = cur->getNext();
}
ptr->setKNext(cur);
this->_head = ptr;
} else {
KListNode* prev = this->traverseTo(index - 1);
ptr->setNext(prev->getNext());
prev->setNext(ptr);
int range = this->_size - this->_skipInt;
if(range > 0) {
ptr = this->_head;
while(ptr->getNext() != NULL && range != 0) {
cur = ptr;
for(int i = 0; i < this->_skipInt; ++i) {
cur = cur->getNext();
}
ptr->setKNext(cur);
ptr = ptr->getNext();
range--;
}
}
}
}
return true;
}
List::insertKListNode(int index, const int& newItem)
if(index == 1) {
ptr->setNext(this->_head);
cur = ptr;
for(int i = 0; i < this->_skipInt; ++i) {
cur = cur->getNext();
}
ptr->setKNext(cur);
this->_head = ptr;
} else {
KListNode* prev = this->traverseTo(index - 1);
ptr->setNext(prev->getNext());
prev->setNext(ptr);
int range = this->_size - this->_skipInt;
if(range > 0) {
ptr = this->_head;
while(ptr->getNext() != NULL && range != 0) {
cur = ptr;
for(int i = 0; i < this->_skipInt; ++i) {
cur = cur->getNext();
}
ptr->setKNext(cur);
ptr = ptr->getNext();
range--;
}
}
}
bool List::removeKListNode(int index) {
if(index < 1 || index > this->_size) {
return false;
} else {
KListNode* ptr;
if(index == 1) {
ptr = this->_head;
_head = (this->_head)->getNext();
} else {
KListNode *prev = traverseTo(index - 1);
ptr = prev->getNext();
prev->setNext(ptr->getNext());
int range = this->_size - this->_skipInt;
KListNode* newPtr;
if(range > 0) {
newPtr = this->_head;
int i = 1;
while(newPtr != NULL && i <= range) {
int rangeIndex = i + this->_skipInt;
if(rangeIndex >= index) {
if(rangeIndex >= this->_size) {
newPtr->setKNext(NULL);
} else {
KListNode* cur;
cur = newPtr;
for(int i = 0; i < this->_skipInt; ++i) {
cur = cur->getNext();
}
newPtr->setKNext(cur);
}
}
newPtr = newPtr->getNext();
i++;
}
}
}
delete ptr;
ptr = NULL;
--this->_size;
}
return true;
}
List::removeKListNode(int index)
KListNode *prev = traverseTo(index - 1);
ptr = prev->getNext();
prev->setNext(ptr->getNext());
int range = this->_size - this->_skipInt;
KListNode* newPtr;
if(range > 0) {
newPtr = this->_head;
int i = 1;
while(newPtr != NULL && i <= range) {
int rangeIndex = i + this->_skipInt;
if(rangeIndex >= index) {
if(rangeIndex >= this->_size) {
newPtr->setKNext(NULL);
} else {
KListNode* cur;
cur = newPtr;
for(int i = 0; i < this->_skipInt; ++i) {
cur = cur->getNext();
}
newPtr->setKNext(cur);
}
}
newPtr = newPtr->getNext();
i++;
}
}
void List::pushBack(int &item) {
KListNode *ptr;
KListNode *newPtr = new KListNode(item, this->_skipInt);
ptr = this->_head;
if(ptr == NULL) {
newPtr->setNext(this->_head);
this->_head = newPtr;
this->_size++;
} else {
ptr = this->traverseTo(this->_size);
ptr->setNext(newPtr);
this->_size++;
int prevKIndex = this->_size - this->_skipInt;
if(prevKIndex > 0) {
KListNode* prevPtr = this->traverseTo(prevKIndex);
prevPtr->setKNext(newPtr);
}
}
}
List::pushBack(int &item)
int prevKIndex = this->_size - this->_skipInt;
if(prevKIndex > 0) {
KListNode* prevPtr = this->traverseTo(prevKIndex);
prevPtr->setKNext(newPtr);
}
Ok that was a alot to take in...
Implement it to really understand it...
b1) 2
b2) 2 + (size - index - 2k)
Thats all!!
CS1020E Tutorial 6 Qn 4
By tgxworld
CS1020E Tutorial 6 Qn 4
- 1,064