自己实现 STL Queue(用数组和链表)

队列就是先来的先被处理掉,后来的就等,直到成为先来的,实现起来感觉和栈差不多。

模板好用的,功能强大,有些东东还是写成模板的好,

链表版

template<typename T,typename container>
class queue 
{
public:
bool empty() const
{
return len==0;
}

void checkEmpty()
{
if(empty())
{
throw new exception("队列中没有数据");
} 
}

T& back()
{
checkEmpty();
return cur->val;
}

const T& back() const
{
return back();
}

void pop()
{
checkEmpty();
if(head->next==cur)
{
delete head->next;
head->next=NULL;
}else
{
node* tmp=head->next;
head->next=tmp->next;
delete tmp;
}
--len;
}

T& front()
{
checkEmpty();
return head->next->val; 
}

const T& front() const
{
return front();
}

void push(const T& val)
{
node *tmp=new node(val); 
cur->next=tmp; 
cur=tmp; 
++len; 
}

queue()
{
initialize();
}

explicit queue(const container& cont)
{ 
initialize();
vector <int>::const_iterator iter=cont.begin();
while(iter!=cont.end())
{
push(*iter++);
}
}

~queue()
{
node *tmp;
while(tmp!=NULL)
{
tmp=head;
head=head->next;
delete tmp;
tmp=NULL;
}
delete cur;
}


int size()
{
return len;
}

protected:
typedef struct node1
{
node1 *next;
T val;
node1(T v):val(v),next(NULL){} 
}node;

private :
int len; 
node *head; 
node *cur; 
void initialize()
{
head=new node(-1);
cur=head;
len=0;
} 
};

数组版


template<typename T,typename container>
class queue
{
public:
bool empty() const
{
return head==rail;
}

void checkEmpty()
{
if(empty())
{
throw new exception("队列中没有数据");
} 
}

//队尾元素
T& back()
{
checkEmpty();
return arr[rail-1];
}

const T& back() const
{
return back();
}

//出队
void pop()
{
checkEmpty();
arr[head++]=0;
}

//队头元素
T& front()
{
checkEmpty();
return arr[head];
}

const T& front() const
{
return front();
}

//入队
void push(const T& val)
{
if(rail>=capacity){
capacity=(rail-head)*2;
T *tmp=new T[capacity];
int j=0;
for(int i=head;i<rail;i++)
{
tmp[j++]=arr[i];
}
delete arr;
arr=tmp;
rail=rail-head;
head=0;
}
arr[rail++]=val;
}

queue()
{
initialize(4);
}

queue(int capacity)
{
initialize(capacity);
}

explicit queue(const container& cont)
{ 
initialize(cont.size());
vector <int>::const_iterator iter=cont.begin();
while(iter!=cont.end())
{
push(*iter++);
}
}

~queue()
{
delete arr;
}

//队列中元素个数
int size()
{
return rail-head;
}

protected:
typedef struct node1
{
node1 *next;
T val;
node1(T v):val(v),next(NULL){} 
}node;

private :
int capacity; 
int head;//对头元素的位置
int rail;//对尾元素的位置
int *arr; 

void initialize(int cap)
{
capacity=cap;
arr=new int[capacity];
head=rail=0;
}
};


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STLqueue不支持循环队列,它是基于单向链表或双向链表实现的,因此只能在队尾插入元素,在队首删除元素。如果需要实现循环队列,可以使用STL的deque(双端队列),或者自己实现一个循环队列。以下是一个简单的循环队列实现示例: ```c++ #include <iostream> #include <vector> using namespace std; class CircularQueue { private: vector<int> data; int head; int tail; int size; public: CircularQueue(int k) { data.resize(k); head = -1; tail = -1; size = k; } bool enQueue(int value) { if (isFull()) { return false; } if (isEmpty()) { head = 0; } tail = (tail + 1) % size; data[tail] = value; return true; } bool deQueue() { if (isEmpty()) { return false; } if (head == tail) { head = -1; tail = -1; return true; } head = (head + 1) % size; return true; } int Front() { if (isEmpty()) { return -1; } return data[head]; } int Rear() { if (isEmpty()) { return -1; } return data[tail]; } bool isEmpty() { return head == -1; } bool isFull() { return ((tail + 1) % size) == head; } }; int main() { CircularQueue q(5); cout << q.enQueue(1) << endl; // true cout << q.enQueue(2) << endl; // true cout << q.enQueue(3) << endl; // true cout << q.enQueue(4) << endl; // true cout << q.enQueue(5) << endl; // true cout << q.enQueue(6) << endl; // false cout << q.Front() << endl; // 1 cout << q.Rear() << endl; // 5 cout << q.isFull() << endl; // true cout << q.deQueue() << endl; // true cout << q.deQueue() << endl; // true cout << q.enQueue(6) << endl; // true cout << q.Front() << endl; // 3 cout << q.Rear() << endl; // 6 return 0; } ``` 这个循环队列使用了一个vector来存储队列元素,head和tail分别表示队头和队尾的下标,size表示队列的大小。当队列满时,tail会指向队列最后一个元素,此时再插入元素就会导致队列溢出。为了避免这种情况,我们使用了取模运算来实现循环队列

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值