真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

c++中棧與隊列的實現(xiàn)

  1. 棧:具有先進(jìn)后出的特點(diǎn),且只能在一端進(jìn)行插入與刪除的操作,棧的實現(xiàn)如下所示

    目前成都創(chuàng)新互聯(lián)公司已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、九江網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

  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.隊列:具有隊頭插入,隊尾刪除的特點(diǎn),具有一個頭指針和一個尾指針

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;

}


網(wǎng)站題目:c++中棧與隊列的實現(xiàn)
分享路徑:http://weahome.cn/article/jjjhei.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部