RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
数据结构:模板实现栈和队列

(一)模板实现栈

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的东乃网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

#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;
};

网站名称:数据结构:模板实现栈和队列
本文URL:http://cqwzjz.cn/article/ieccij.html