Created
February 28, 2014 19:19
-
-
Save alireza1989/9277809 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+++++++++++++++++++++++++++++++++++++++++++++++++++ | |
// THE QUEUE CLASS | |
// | |
// | |
//+++++++++++++++++++++++++++++++++++++++++++++++++++ | |
# include <cstring> | |
# include <iostream> | |
# include "queue.h" | |
using namespace std; | |
//+++++ QUEUE CONSTRUCTOR +++++ | |
queue::queue(){ | |
front_p = NULL; | |
back_p = NULL; | |
current_size=0; | |
} | |
//++++++++++++++++++++++++++++ | |
//+++++ ENQUEUE +++++ | |
void queue::enqueue(int item){ | |
if (current_size == 0){ | |
front_p = new node( item , NULL); | |
back_p = front_p; | |
current_size ++; | |
} | |
else { | |
node *temp = back_p; | |
back_p = new node(item , NULL); | |
temp->next = back_p; | |
delete temp; | |
current_size ++; | |
} | |
} | |
//++++++++++++++++++++++++++++ | |
//+++++ DEQUEUE +++++ | |
int queue::dequeue(){ | |
if (current_size != 0){ | |
node *temp = front_p; | |
front_p = front_p->next; | |
int i = temp->data; | |
delete temp; | |
current_size --; | |
return i; | |
} | |
} | |
//++++++++++++++++++++++++++++ | |
//+++++ FRONT +++++ | |
int queue::front(){ | |
return front_p -> data; | |
} | |
//++++++++++++++++++++++++++++ | |
//+++++ EMPTY +++++ | |
bool queue::empty(){ | |
if(current_size == 0) | |
return true; | |
} | |
//++++++++++++++++++++++++++++ | |
//+++++ SIZE +++++ | |
int queue::size(){ | |
return current_size; | |
} | |
//++++++++++++++++++++++++++++ | |
//+++++ REMOVE +++++ | |
int queue::remove(int item){ | |
int counter = 0; | |
while (front_p -> data == item){ | |
node * temp = front_p; | |
if(front_p -> next != NULL){ | |
front_p = front_p -> next; | |
counter++; | |
delete temp; | |
current_size --; | |
} | |
else{ | |
front_p = back_p; | |
counter++; | |
delete temp; | |
current_size --; | |
return counter; | |
} | |
} | |
node * curr_node = front_p; | |
while (curr_node -> next != NULL){ | |
node * next_node = curr_node -> next; | |
if (next_node -> data == item){ | |
node * temp = next_node; | |
if(next_node -> next != NULL){ | |
curr_node -> next = next_node -> next; | |
counter ++; | |
current_size --; | |
delete temp; | |
} | |
} | |
else{ | |
curr_node -> next = back_p; | |
node * temp = curr_node; | |
counter ++; | |
delete temp; | |
return counter; | |
} | |
} | |
curr_node = next_node; | |
return counter; | |
} | |
//++++++++++++++++++++++++++++ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment