基本概念
c++STL的三种基本容器:vector、list、deque。同时c++STL也提供了三种容器适配器:stack、queue、priority_queue。
其中stack栈能够用任何序列容器实现:vector、list、deque。默认情况下为deque。
适配器queue使用:deque、list实现。默认情况下为deque。
适配器priority_queue使用:vector、deque实现。默认情况下为vector。
什么是容器适配器?
我们以stack栈为例:以某种既有容器作为底部结构,将其接口改变,使之符合“先进先出”的特性,形成一个stack,由于stack是以底部容器完成其所有工作,而具有这种“修改某物接口,形成另一种风貌”的性质者,称为adapter(适配器),因此,STL stack往往不被归类为container(容器),而被归类为container adapter(容器适配器)。同理queue,priority_queue也是容器适配器。
默认stack是通过deque容器实现的,deque是双向开口的数据结构,若以deque为底部结构并封闭其头端开口,便很容易形成一个stack。
//deque的STL源码
// deque的数据结构
template <class T, class Alloc = alloc, size_t BufSiz = 0>
class deque
{
public: // Basic types
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
public: // Iterators
typedef __deque_iterator<T, T&, T*, BufSiz> iterator;
protected: // Internal typedefs
typedef pointer* map_pointer;
// 这个提供STL标准的allocator接口, 见<stl_alloc.h>
typedef simple_alloc<value_type, Alloc> data_allocator;
typedef simple_alloc<pointer, Alloc> map_allocator;
.................
}
//STL中stack的源码
template <class T,class Sequence = deque<T> >
class stack
{
//以下__STL_NULL_TMPL_ARGS 会展开为<>.
friend bool operator==__STL_NULL_TMPL_ARGS(const stack&,const stack&);
friend bool operator<__STL_NULL_TMPL_ARGS(const stack&,const stack&);
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::size_type size_type;
typedef typename Sequence::reference reference;
typedef typename Sequence::const_reference const_reference;
protect:
Sequence c://底层容器
public:
//以下完全是利用Sequence c的操作,完成stack的操作。
bool empty() const {return c.empty();}
size_type size() const {return c.size();}
reference top() {return c.back();}
const_reference top() const {return c.back();}
//deque是两头可进出,stack是末端进,末端出。
void push(const value_type& x) {c.push_back(x);}
void pop() {c.pop_back();}
}
template<class T,class Sequence>
bool operator==(const stack<T,Sequence>& x,const stack<T,Sequence>& y)
{
return x.c==y.c;
}
template<class T,class Sequence>
bool operator<(const stack<T,Sequence>& x,const stack<T,Sequence& y>)
{
return x.c<y.c;
}
示例程序:
Reverse Nodes in k-Group (Hard) 栈实现方式
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* reverseKGroup(ListNode* head,int k)
{
if(k==0||k==1) return head;
if(head==NULL) return NULL;
ListNode* newHead=new ListNode(-1);
ListNode* movPtr=newHead;
stack<ListNode*>MyStack;
int coun=k;
ListNode* tempRecord=NULL;
while(1)
{
tempRecord=head;//记录开始下次循环时的首指针;
while(coun&&head!=NULL)
{
MyStack.push(head);
head=head->next;
coun--;
}
if (coun==0&&head!=NULL)
{
while(!MyStack.empty())
{
movPtr->next=MyStack.top();
MyStack.pop();
movPtr=movPtr->next;
movPtr->next=NULL;
}
coun=k;
continue;
}
else if (coun==0 &&head==NULL)
{
while(!MyStack.empty())
{
movPtr->next=MyStack.top();
MyStack.pop();
movPtr=movPtr->next;
movPtr->next=NULL;
}
return newHead->next;
}
else
{
movPtr->next=tempRecord;
return newHead->next;
}
}
}