i3geek.com
闫庚哲的个人博客

链式队列

class Queue{
public:
	Queue();
	bool empty() const;
	Error_code append(const Queue_entry &item);//入队
	Error_code serve();//出队
	Error_code retrieve(Queue_entry &item) const;//检索队头元素
	~Queue();
	Queue(const Queue &original);
	void operator= (const Queue &original);
protected:
	Node *front,*rear;
};

Queue::Queue()
{
	front = rear =NULL;
}

Error_code Queue::append(const Queue_entry &item)
{
	Node *new_rear = new Node(item);
	if(new_rear==NULL) return overflow;
	if(rear == NULL) front = rear = new_rear;
	else {
		rear -> next = new_rear;
		rear = new_rear;
	}
	return success;
}

Error_code Queue::serve()
{
	if(front == NULL) return eunderflow;
	Node *old_front =front;
	front = old_front->next;
	if(front == NULL) rear=NULL;
	delete old_front;
	return success;
}

class Extended_queue:public Queue{
public:
	bool full() const;
	int size() const;
	void clear();
	Error_code serve_and_retrieve(Queue_entry &item);
};

int Extended_queue::size() const
{
	Node *window=front;
	int count=0;
	while(window!=NULL){
		window=window->next;
		count++;
	}
	return count;
}

 

赞(0)
未经允许不得转载:爱上极客 » 链式队列
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址