Skip to content

Instantly share code, notes, and snippets.

@Siunami
Last active February 1, 2017 00:50
Show Gist options
  • Save Siunami/38f3f2a1b3ad6073c36dff84ab11ea46 to your computer and use it in GitHub Desktop.
Save Siunami/38f3f2a1b3ad6073c36dff84ab11ea46 to your computer and use it in GitHub Desktop.
CPSC 221 UBC: Move a queue value to the front
// Input is a queue
// Ex : 100,200,300,400,500
// key = an element in the queue
void Queue::move_to_front(const QueueElement & key) {
Node *curr = myFront;
Node *temp = myFront;
int moreVal = true;
cout << "Debug: " << endl;
cout << curr->data << endl;
cout << curr->next->data << endl;
if (curr->data == key) {
cout << "Return the list " << endl;
return;
}
while (moreVal){
if (curr->data == key && curr != myFront) {
cout << "Key Match!: " << endl;
bool emptySpot = false;
int holder = temp->data;
int secondHolder;
myFront->data = curr->data;
while (temp->next != NULL){
if (temp->next == curr){
if (temp->next->next != NULL){
secondHolder = temp->next->next->data;
} else {
secondHolder = 0;
}
temp->next->data = holder;
holder = secondHolder;
temp = temp->next;
emptySpot = true;
} else if (emptySpot == false) {
secondHolder = temp->next->data;
temp->next->data = holder;
holder = secondHolder;
temp = temp->next;
} else if (emptySpot == true) {
if (temp->next->next != NULL){
secondHolder = temp->next->next->data;
} else {
secondHolder = 0;
}
temp->next->data = holder;
holder = secondHolder;
temp = temp->next;
}
}
moreVal = false;
} else {
if (curr->next != NULL){
curr = curr->next;
} else {
cout << "Key is not valid!" << endl;
moreVal = false;
}
}
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment