码迷,mamicode.com
首页 > 其他好文 > 详细

栈和队列

时间:2016-05-14 17:10:22      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:栈和队列

  • 栈的定义--Stack

栈只允许在末端(即是栈顶)进行插入和删除的线性表。栈具有后进先出的特性(LIFO,Last In First Out)。

技术分享

下面是关于栈的实现:

#include<iostream>

#include<string>

using namespace std;


template<class T>

//栈有动态与静态之分

//栈适合用顺序表(无中间插入与删除时最好用数组),因为栈选顺序表(即是数组),进行size++与size--比较方便,而且效率较高。并且它的CPU高速缓存利用(即命中)率更高,命中,即是缓存中有,用时可以直接从缓存中取数据。

class stack

{

protected:

           T* _a;     //T类型的指针,指向顺序表(数组)

           size_t _size;    //数据的个数

           size_t _capacity;    //栈的容量


public:


          stack( const stack <T> & s)//拷贝构造函数

                   :_a( new T [_size])   //此处的_size也可以换成_capacity,但是最好不要,因为这样会浪费空间。

                   ,_size(s._size)

                   , _capacity(s._size)

          {

                              for (size_t i = 0; i < _size; ++i)

                             {

                                      _a[i] = s.q[i];

                             }


          }


           //stack<T> &operator=(const stack<T> &s)   //赋值运算符重载

           //{

           //赋值运算符重载的传统写法

           //       if (this != &s)   //首先判断是不是自己给自己赋值

           //       {

           //                 //此处记住不能先释放空间,即delete[] _a,因为先释放再分配空间有缺陷,因为开辟空间可能会失败,但是释放空间一定会成功。

           //                 T* tmp = new T[s._size];

           //                 for (size_t i = 0; i < _size; ++i)

           //                 {

           //                          tmp[i] = s._a[i];

           //                 }

           //                 delete[] _a;

           //                 _a = tmp;

           //       }

           //       return *this;

           //}


           stack<T > &operator=(const stack<T > &s)

          {

           //赋值运算符重载的现代写法(更好)

                   swap(_a, s._a);

                   swap(_size, s._size);

                   swap(_capacity, s._capacity);

                    return *this ;

          }


           void Push(const T& x)

          {

           //检查容量

                   _CheckCpapacity();

                   _a[_size++] = x;

          }

           void Pop()

          {

                   assert(_size > 0);

                   --_size;

          }

           T& Top()   //返回栈顶元素

          {

                   assert(_size > 0);

                    return _a[_size - 1];

          }

           bool Empty()

          {

                    return _size == 0;

          }

           size_t  size()

          {

           return

          }

protected:

           void _CheckCapacity()

          {

                    if (_size == _capacity)

                   {

                             _capacity = _capacity * 2 + 3;

                              T* tmp = new T[_capacity];

                              if (_a)

                             {

                                       for (size_t i = 0; i < _size; ++i)

                                      {

                                                tmp[i] = _a[i];


                                      }

                                       delete[] _a;

                             }

                             _a = tmp;

                   }

          }


};

队列的定义

队列值允许在表的队尾进行插入,在表对头进行删除。队列具有先进先出的特性。(FIFO,first In First Out)

下面是队列的实现

//队列全是动态,没有静态。最好用链式结构。

template<typename T>

struct Node

{

T _data;

Node<T > * _next;

};


template<class T>


class queue

{

protected:

           Node<T > * _tail;

           Node<T > * _head;


public:

          queue()

                   :_head( NULL)

                   , _tail( NULL)

          {}


          queue( const queue <T> & q)    //拷贝构造函数

                   :_head( NULL)

                   , _tail( NULL)

          {

                    Node<T > *cur = q._head;

                    while (cur)

                   {

                             Push(cur->_data);

                             cur = cur->_next;

                   }

          }


           //queue<T> &operator=(const queue &q)    //赋值运算符重载

           //{

           //       //传统写法

           //       if (this != &q)

           //       {

           //                 clear();

           //                 Node<T> *cur = q._head;

           //                 while (cur)

           //                 {

           //                          Push(cur->_data);

           //                          cur = cur->_next;

           //                 }

           //       }

           //       return *this;

           //}


           queue<T > &operator=(const queue &q )    //赋值运算符重载

          {

           //现代写法

                   swap(_head, q._head);

                   swap(_tail, q._tail);

                    return *this ;

          }

         

          ~queue()

          {

                    Node<T > *cur = _head;

                    while (cur)

                   {

                              Node<T > *del = cur;

                             cur = cur->_next;

                              delete del;

                   

                   }

                   _head = NULL;

                   _tail = NULL;

          }

           void Push(const T& x)

          {

                    if (_head == NULL )

                   {

                             _head = _tail = new Node <T>( x);

                   }

                    else

                   {

                             _tail->_next = new Node <T>( x);

                             _tail = _tail->_next;

                   }

          }

           void Pop()               //出队操作,,有三种情况,分别为队列为空,队列只有一个结点,队列有多个结点。

          {

                   assert(_head);

                    if (_head == _tail)   //头指针等于尾指针,即只有一个结点。

                   {

                              delete _head;

                             _head = _tail = NULL;

                   }

                    else

                   {

                              Node<T > *del = _head;

                             _head = _head->_next;

                              delete del;

                   }

          }

           T& Front()       //返回队列的第一个元素

          {

                    return _head->_data;

          }

           T& Back()      //返回队列的最后一个元素

          {

                    return _tail->_data;

          }

           bool Empty()    //判断队列是否为空

          {

                    return _head == NULL ;

          }

           size_t size()     //返回队列的结点的个数

          {

                    size_t size = 0;

                    Node<T > *cur = _head;

                    while (cur)

                   {

                             ++size;

                             cur = cur->_next;

                   }

                    return size;

          }

};



本文出自 “10911544” 博客,请务必保留此出处http://10921544.blog.51cto.com/10911544/1773322

栈和队列

标签:栈和队列

原文地址:http://10921544.blog.51cto.com/10911544/1773322

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!