资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

数据结构:模板实现栈和队列-创新互联

(一)模板实现栈

成都创新互联2013年至今,是专业互联网技术服务公司,拥有项目成都网站建设、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元青山湖做网站,已为上家服务,为青山湖各地企业和个人服务,联系电话:18982081108
#pragma once
typedef unsigned int size_t;
template 
class Stack
{
public:
 Stack()
  :_array(NULL)
  ,_top(-1)
  ,_capacity(0)
 {}
 ~Stack()
 {
  if(_array)
  {
   delete[] _array;
  }
 }
public:
 void Push(const T& num)
 {
  _CheckCapacity();
  _array[++_top] = num;
 }
 void Pop()
 {
  if(Empty())
  {
   printf("Empty!");
  }
  else
  {
   _top--;
  }
 }
 T& GetTop()
 {
  return _array[_top];
 }
 void PrintStack()
 {
  cout<

2.模板实现队列

#pragma once
template 
struct Node
{
 T _data;
 Node* _next;
 Node(const T& d)
  :_data(d)
  ,_next(NULL);
 {}
};
template 
class Queue
{
public:
 Queue()
  :_head(NULL)
  ,_tail(NULL)
  ,_size(0)
 {}
 ~Queue
 {
  if(_head)
   delete _head;
  if(_tail)
   delete _tail;
 }
public:
 void Push(const T& d)
 {
  if(_head == NULL)
  {
   _head = _tail = new Node(d);
  }
  else
  {
   _tail->_next = new Node(d);
   _tail = _tail->_next;
  }
  _size++;
 }
 void Pop()
 {
  if(_head == NULL)
  {
   cout<<"Empty!"<* tmp = _head;
   _head = _head->_next;
   delete tmp;
  }
  _size--;
 }
 bool Empty()
 {
  return _head == NULL;
 }
 size_t Size()
 {
  return _size;
 }
 T& Front()
 {
  assert(_head);
  return _head->_data;
 }
 T& Back()
 {
  assert(_tail);
  return _tail->_data;
 }
private:
 Node* _head;
 Node* _tail;
 size_t _size;
};

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


当前名称:数据结构:模板实现栈和队列-创新互联
URL分享:http://cdkjz.cn/article/hscgj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220