c++中栈与队列的实现

  1. 栈:具有先进后出的特点,且只能在一端进行插入与删除的操作,栈的实现如下所示

    目前成都创新互联公司已为上1000+的企业提供了网站建设、域名、雅安服务器托管网站托管、企业网站设计、九江网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

  2. struct truetype

  3. {

  4. bool get()

  5. {

  6. return true;

  7. }

  8. };

  9. struct falsetype

  10. {

  11. bool get()

  12. {

  13. return false;

  14. }

  15. };

  16. template

  17. struct typetraits

  18. {

  19. typedef falsetype  isnpodtype;

  20. };

  21. template <>

  22. struct typetraits

  23. {

  24. typedef truetype  ispodtype;

  25. };

  26. template

  27. class stack

  28. {

  29. protected:

  30. T *_a;

  31. size_t _top;

  32. size_t _capacity;

  33. public:

  34. stack()

  35. : _top(0)

  36. , _capacity(3)

  37. {

  38. _a = new T[_capacity];

  39. }

  40. ~stack()

  41. {

  42. if (_a)

  43. delete[] _a;

  44. }

  45. void  checkcapacity()

  46. {

  47. if (_top == _capacity)

  48. {

  49. _capacity = 2 * _capacity;

  50. T *tem = new T[_capacity];

  51. if ((typetraits::ispodtype()).get())

  52. {

  53. memcpy(tem, _a, sizeof(T)*_capacity);

  54. delete[]_a;

  55. _a = tem;

  56. }

  57. else

  58. {

  59. int i = 0;

  60. for (i = 0; i < _top; i++)

  61. {

  62. tem[i] = _a[i];

  63. }

  64. delete[]_a;

  65. _a = tem;

  66. }

  67. }

  68. }

  69. void push(const T&x)

  70. {

  71. checkcapacity();

  72. _a[_top] = x;

  73. _top++;

  74. }

  75. void pop()

  76. {

  77. _top--;

  78. }

  79. bool Empty()

  80. {

  81. return _top == 0;

  82. }

  83. T &Top()

  84. {

  85. if (_top > 0)

  86. {

  87. size_t ret = _top;

  88. _top--;

  89. return _a[ret - 1];

  90. }

  91. }

  92. };

  93. int main()

  94. {

  95. stack s1;

  96. s1.push(1);

  97. s1.push(2);

  98. s1.push(3);

  99. s1.push(4);

  100. while (!s1.Empty())

  101. {

  102. cout << s1.Top() << " ";

  103. }

  104. getchar();

  105. return 0;

  106. }

2.队列:具有队头插入,队尾删除的特点,具有一个头指针和一个尾指针

template

struct Node

{

T _data;

Node *_next;

Node(const T &data=0)

{

_data = data;

_next = NULL;

}

};

template

class Queue

{

protected:

Node *_head;

Node *_tail;

public:

Queue()

:_head(NULL)

, _tail(NULL)

{}

~Queue()

{

Node *ret = NULL;

if (_head == NULL)

return;

if (_head == _tail)

{

delete _head;

_head = NULL;

_tail = _head;

}

else

{

while (_head)

{

pop();

}

}

}

void push(const T&x)

{

Node *newNode = new Node(x);

if (_tail == NULL)

{

_tail = newNode;

_head = _tail;

}

else

{

_tail->_next = newNode;

_tail = newNode;

}

}

void pop()

{

Node*ret = NULL;

if (_head == NULL)

return;

if (_head == _tail)

{

delete _head;

_head = NULL;

_tail = _head;

}

else

{

ret = _head;

_head = _head->_next;

delete ret;

}

}

T &Front()

{

Node *ret = _head;

_head = _head->_next;

return ret->_data;

}

T &Back()

{

Node *ret = _tail;

_tail = _head->_tail;

return _tail->_data;

}

bool Empty()

{

return _head == _tail;

}

void show()

{

while (_head)

{

cout << _head->_data << " ";

_head = _head->_next;

}

}

};

int main()

{

Queue q;

q.push(1);

q.push(2);

q.push(3);

q.push(4);

q.pop();

q.show();

getchar();

return 0;

}


标题名称:c++中栈与队列的实现
文章链接:http://myzitong.com/article/jjjhei.html