This could implement to CMenu to change the CMenuItem position in R0.6.
//queue.cpp
bool Queue::moveNode(int toNode, int fromNode) {
QNode* nodeToMove = (QNode*)0;
bool r = false;
if ((toNode <= _size) && (fromNode <= _size) && toNode != fromNode) {
r = true;
// extract the node
_cur = _head;
int i;
for(i = 1;i < fromNode && _cur->_next ;i++) {
_cur = _cur->_next;
}
nodeToMove = _cur;
if (_cur == _tail) {
_tail = _cur->_prev;
_cur->_prev->_next = (QNode*)0;
}
else if (_cur == _head) {
_head = _cur->_next;
_cur->_next->_prev = (QNode*)0;
}
else {
_cur->_next->_prev = _cur->_next->_prev->_prev;
_cur->_prev->_next = _cur->_prev->_next->_next;
}
// place the node
_cur = _head;
for (i = 1;i < toNode && _cur->_next ;i++) {
_cur = _cur->_next;
}
if (_cur == _head) {
_head = nodeToMove;
nodeToMove->_next = _cur;
nodeToMove->_prev = (QNode*)0;
_cur->_prev = nodeToMove;
}
else if (_cur == _tail) {
_tail = nodeToMove;
nodeToMove->_next = (QNode*)0;
nodeToMove->_prev = _cur;
_cur->_next = nodeToMove;
}
else {
nodeToMove->_prev = _cur->_prev;
nodeToMove->_next = _cur;
_cur->_prev->_next = nodeToMove;
_cur->_prev = nodeToMove;
}
}
return r;
}
//queuetest.cpp
#include "queue.h"
#include <iostream>
using namespace std;
int main(){
int i;
Queue Q;
for(i=10;i<100;i+=10){
Q.append(i);
}
if(Q.goHead()){
do{
cout<<Q.visit()<<", ";
}while(Q.goNext());
}
cout<<endl;
//cout<<"removed: "<<Q.remove()<<endl;
//Q.append(3000);
Q.moveNode(9, 1);
cout<<"from 1 to 9: ";
if(Q.goHead()){
do{
cout<<Q.visit()<<", ";
}while(Q.goNext());
}
cout<<endl;
cout<<"from 9 to 1: ";
Q.moveNode(1, 9);
if(Q.goHead()){
do{
cout<<Q.visit()<<", ";
}while(Q.goNext());
}
cout<<endl;
cout<<"from 7 to 3: ";
Q.moveNode(3, 7);
if(Q.goHead()){
do{
cout<<Q.visit()<<", ";
}while(Q.goNext());
}
cout<<endl;
return 0;
}
//Output
10, 20, 30, 40, 50, 60, 70, 80, 90,
from 1 to 9: 20, 30, 40, 50, 60, 70, 80, 90, 10,
from 9 to 1: 10, 20, 30, 40, 50, 60, 70, 80, 90,
from 7 to 3: 10, 20, 70, 30, 40, 50, 60, 80, 90,
deleting: 10
deleting: 20
deleting: 70
deleting: 30
deleting: 40
deleting: 50
deleting: 60
deleting: 80
deleting: 90
Press any key to continue . . .
No comments:
Post a Comment