int Queue::removeNode(int nodeNum) {
QNode* delNode = (QNode*)0;
int val = 0;
// if list is not empty and nodeNum is within the size of list
if (nodeNum <= _size && _size > 0) {
//if nodeNum is 1st node in list
if (nodeNum == 1) {
val = remove();
}
// if nodeNum is last node in list
else if (nodeNum == _size) {
delNode = _tail;
_tail = _tail->_prev;
_tail->_next = (QNode*)0;
}
else {
_cur = _head;
int i;
for(i = 1;i < nodeNum;i++) {
_cur = _cur->_next;
}
delNode = _cur;
_cur->_next->_prev = _cur->_next->_prev->_prev;
_cur->_prev->_next = _cur->_prev->_next->_next;
}
_size--;
}
if (delNode) {
val = delNode->_data;
delete delNode;
}
return val;
}
Tuesday, 13 December 2011
Linked list - Remove node given the node position
Since Fardad didn't give out the answer of test1, I decide to do this coding for practice
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment