1️⃣6️⃣ 链式队列
#include <iostream>
using namespace std;
class ListQueue{
private:
struct Node{
int data;
Node* next;
Node(int d, Node* n = 0):data(d), next(n) {}
};
Node* Listfront;
Node* Listback;
public:
ListQueue();
~ListQueue();
bool IsEmpty()const;
int getFront()const;
void enqueue(const int i);
int dequeue();
void makeEmpty();
};
ListQueue::ListQueue(){
Listback = Listfront = 0;
}
ListQueue::~ListQueue(){
makeEmpty();
}
void ListQueue::makeEmpty(){
while(!IsEmpty())
dequeue();
}
bool ListQueue::IsEmpty()const{
return Listfront == 0;
}
int ListQueue::getFront()const{
if(IsEmpty())
throw"Queue is empty.";
return Listfront->data;
}
void ListQueue::enqueue(const int i){
if(IsEmpty())
Listback = Listfront = new Node(i);
else Listback = Listback->next = new Node(i);
}
int ListQueue::dequeue(){
int frontitem = getFront();
Node* p = Listfront;
Listfront = Listfront->next;
delete p;
return frontitem;
}
int main() {
ListQueue l1;
l1.enqueue(1);
l1.enqueue(2);
l1.enqueue(3);
l1.enqueue(4);
cout << l1.getFront() << endl;
cout << "删除了" << l1.dequeue() << endl;
cout << "删除了" << l1.dequeue() << endl;
cout << "删除了" << l1.dequeue() << endl;
return 0;
}