求点赞,求关注,求评论
求点赞,求关注,求评论
求点赞,求关注,求评论
求点赞,求关注,求评论
求点赞,求关注,求评论
求点赞,求关注,求评论
求点赞,求关注,求评论
这篇文章很短,直接给代码:
#include <iostream>
using namespace std;
template <class T>class Queue {
protected:
struct node {
T data;
node *next;
};
node *head;
node *tail;
size_t len;
public:
Queue() {
head = NULL;
tail = NULL;
len = 0;
}
~Queue() {
node *ptr = NULL;
while(head != NULL) {
ptr = head->next;
delete head;
head = ptr;
}
}
void push(T var) {
node *ptr = new node;
ptr->data = var;
ptr->next = NULL;
if(tail != NULL) {
tail->next = ptr;
}
else {
head = ptr;
}
tail = ptr;
len++;
}
void pop() {
if (len <= 0) {
abort();
}
node *ptr = head->next;
delete head;
head = ptr;
if(head == NULL) {
tail = NULL;
}
len--;
}
T front() {
return head->data;
}
T back() {
return tail->data;
}
bool empty() {
return head == NULL && tail == NULL;
}
size_t size() {
return len;
}
void clear() {
while (len > 0) {
pop();
}
}
};
标签:node,head,STL,C++,queue,tail,len,NULL,ptr
From: https://blog.csdn.net/j5486545648564/article/details/143216732