Function description:
1.Add a node
2.Remove a node
3.Find next node
//queue.cpp
//HYQ
//Mar 07, 2013
//Release v0.1
#include "queue.h"
using namespace std;
Node::Node(int data, Node* next){
_data = data;
_next = next;
}
Queue::Queue(){
_head = (Node*)0;
}
//add a Node at the end of queue
void Queue::add(int data){
Node* toAdd=_head;
Node* add = new Node(data);
if (_head){
while ((nextNode(toAdd)!=0) && (toAdd=nextNode(toAdd)));
toAdd->_next = add;
}
else{
_head = add;
}
}
int Queue::remove(){
int ret = _head->_data; // 1
Node* toDel = _head; // 2
_head = _head->_next; // 3
delete toDel; // 5
return ret;
}
bool Queue::isEmpty(){
return !_head;
}
//Return the next Node of current Node
Node* Queue::nextNode(const Node* currentNode) const{
return currentNode->_next;
}
Queue::~Queue(){
while(!isEmpty()){
remove();
}
}
No comments:
Post a Comment