Changkun's Blog

Science and art, life in between.


  • Home

  • Ideas

  • Archives

  • Tags

  • Bio

模版链栈实现

Published at: 2013-10-04   |   Reading: 746 words ~2min   |   PV/UV: /

栈只具备入栈和出栈的性质,实现起来较为简单,其本质还是链表,只不过限制了任意节点的访问权限,因此栈不具备迭代器的功能。代码中注释了一个迭代器,是测试代码时写的,迭代效果略有Bug,访问数据会多访问到一个,但不影响栈本身的功能,无视就好~ p.s. 可以考虑重载[]来实现栈中的数据访问。。当然这是后话了。。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
//  Stack.h
//  链栈
//
//  Created by 欧 长坤 on 13-10-4.
//  Copyright (c) 2013年 欧 长坤. All rights reserved.
//

#ifndef ______Stack__
#define ______Stack__

#include <iostream>

template <class T> class Stack;
template <class T> class StackStackNode;
//template <class T> class StackIterator;
template <class T>
class StackNode {

    friend class Stack<T>;
    //friend class StackIterator<T>; // 用于测试迭代器而添加

private:
    T data;
    StackNode<T> *next;
};

template <class T>
class Stack {

    //friend class StackIterator<T>; // 用于测试迭代器而添加

private:

    StackNode<T> *Top;
    StackNode<T> *Bottom;
    size_t Size;

public:
    Stack();
    ~Stack();
    T top();
    void push(T value);
    bool pop();
    bool isEmpty();
    void clear();
    size_t size();
};

// 迭代器用于测试push是否成功而写

// 链栈迭代器
// 使用方法:
// int *x;
// StackIterator<int> inter;
// x = inter.initialize(XXX); // XXX是栈
// while (inter) {
//      cout << *x << ' ';
//      x = inter.next();
// }
/*
template <class T>
class StackIterator
{
private:

    StackNode<T> *location;

public:

    T* initialize(const Stack<T>&amp; stack)
    {
        location = stack.Top;
        if (location)
            return &amp;location->data;
        return 0;
    }
    T* next()
    {
        if (!location)
            return &amp;location->data;
        location = location->next;
        if (location) {
            return &amp;location->data;
        }
        return 0;
    }

};
*/

template <class T>
Stack<T>::Stack()
{
    Top = new StackNode<T>;
    if (NULL == Top) {
        throw "Out of Memory!";
    } else {
        Bottom = Top;
        Top->next = NULL;
    }
    Size = 0;
}

template <class T>
Stack<T>::~Stack()
{
    delete Top;
    Top = Bottom = NULL;
    Size = 0;
}

template <class T>
void Stack<T>::push(T value)
{
    StackNode<T> *newStackNode = new StackNode<T>;
    if (NULL == newStackNode) {
        throw "Out Of Memory!";
    }
    newStackNode->data = value;
    newStackNode->next = Top;
    Top = newStackNode;
    Size++;
}

template <class T>
bool Stack<T>::pop()
{
    if (isEmpty()) {
        return false;
    } else {
        StackNode<T> *p = Top;
        Top = p->next;
        delete p;
        return true;
    }
    Size--;
}

template <class T>
bool Stack<T>::isEmpty()
{
    if (Top == Bottom)
        return true;
    else
        return false;
}

template <class T>
void Stack<T>::clear()
{
    if (isEmpty())
        return;
    else {
        StackNode<T> *p = Top, *q;
        while (p != Bottom) {
            q = p->next;
            delete p;
            p = q;
        }
        Top = Bottom;
    }
    Size = 0;
}

template <class T>
T Stack<T>::top()
{
    return Top->data;
}

template <class T>
size_t Stack<T>::size()
{
    return Size;
}

#endif /* defined(______Stack__) */
#数据结构# #C++#
  • Author: Changkun Ou
  • Link: https://changkun.de/blog/posts/%E6%A8%A1%E7%89%88%E9%93%BE%E6%A0%88%E5%AE%9E%E7%8E%B0/
  • License: All articles in this blog are licensed under CC BY-NC-ND 4.0 unless stating additionally.
模版链队实现
从圆锥体积谈起
  • TOC
  • Overview
Changkun Ou

Changkun Ou

Stop Talking. Just Coding.

276 Blogs
165 Tags
Homepage GitHub Email YouTube Twitter Zhihu
Friends
    Frimin ZZZero march1993 qcrao maiyang Xargin Muniao
© 2008 - 2024 Changkun Ou. All rights reserved. | PV/UV: /
0%