資料結構沒學好,多花了不少時間
使用純C實作很不方便,我直接使用C++風格的寫法(透過destructor回收資源比較方便)
其實若借用stl可以寫的更簡單
不過樓主你現在需要學習的應該是指標怎麼指跟記憶體怎麼分配吧?
所以我用純指標實作
這個link list是singly link list
雖然樓主的要求應該是double link list
但是只要會用指標和分配記憶體,要改成double link list應該不難
本資料結構作的非常不完善,僅供參考
(this data structure is cheap and incomplete, just a reference)
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 179 180 181 182 183 184 185 186 187 188 189
|
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
template<typename T> class linkIterator;
template<typename T> class linkList;
template<typename T>
class linkNode
{
friend class linkIterator<T>;
friend class linkList<T>;
private:
T m_data;
linkNode* m_next;
};
template<typename T>
class linkIterator
{
public:
explicit linkIterator();
void operator=(linkNode<T>* node)
{
m_node = node;
}
T& operator*()
{
if(m_node)
return m_node->m_data;
}
void operator++()
{
if(m_node)
m_node = m_node->m_next;
}
void operator++(int)
{
if(m_node)
m_node = m_node->m_next;
}
bool operator!=(linkNode<T>* node)
{
return node != m_node;
}
bool operator==(linkNode<T>* node)
{
return node == m_node;
}
private:
linkNode<T>* m_node;
};
template<typename T>
linkIterator<T>::linkIterator()
:m_node(0)
{}
template<typename T>
class linkList
{
public:
linkList();
~linkList();
linkNode<T>* begin()
{
if(m_root)
return m_root;
}
linkNode<T>* end()
{
return 0;
}
void insert(T newData, size_t const POSITION);
void push(T newData);
void pop();
size_t size()
{
return m_size;
}
private:
size_t m_size;
linkNode<T>* m_root;
linkNode<T>* m_lastNode;
};
template<typename T>
linkList<T>::linkList()
:m_size(0),
m_root(0),
m_lastNode(0)
{}
template<typename T>
linkList<T>::~linkList()
{
while(m_root)
pop();
}
template<typename T>
void linkList<T>::insert(T newData, size_t const POSITION)
{
if(!POSITION)
{
linkNode<T> *newNode = new linkNode<T>;
newNode->m_data = m_root->m_data;
newNode->m_next = m_root->m_next;
m_root->m_next = newNode;
m_root->m_data = newData;
return;
}
if(POSITION < m_size)
{
linkNode<T>* currentNode = m_root;
size_t const INPOS = POSITION - 1;
for(size_t i = 0; i < INPOS; ++i)
currentNode = currentNode->m_next;
if(currentNode != m_lastNode)
{
linkNode<T> *newNode = new linkNode<T>;
newNode->m_data = newData;
newNode->m_next = currentNode->m_next;
currentNode->m_next = newNode;
}
else
push(newData);
}
}
template<typename T>
void linkList<T>::push(T newData)
{
linkNode<T>* node = new linkNode<T>;
node->m_data = newData;
node->m_next = 0;
if(m_size)
{
m_lastNode->m_next = node;
m_lastNode = node;
}
else
{
m_root = node;
m_lastNode = node;
}
++m_size;
}
template<typename T>
void linkList<T>::pop()
{
if(m_root)
{
if(!m_root->m_next)
{
delete m_root;
m_root = 0;
}
else
{
linkNode<T>* temp = m_root;
while(temp->m_next != 0 && temp->m_next != m_lastNode)
{
temp = temp->m_next;
}
delete m_lastNode;
temp->m_next = 0;
m_lastNode = temp;
}
m_size = (m_size == 0 ? 0 : --m_size);
}
}
#endif // LINKLIST_H_INCLUDED
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void testLinkList()
{
linkList<size_t> haha;
haha.push(0);
haha.push(1);
haha.push(2);
haha.push(3);
haha.push(4);
haha.insert(5, 1);
haha.insert(6, 1);
haha.insert(7, 0);
linkIterator<size_t> it;
for(it = haha.begin(); it != haha.end(); ++it)
cout<<*it<<"\n";
}
|
有什麼問題歡迎請教
其實,如果沒有什麼特別的要求,直接用std::list就好了
1 2 3 4 5 6 7 8 9 10
|
void testList()
{
std::list<size_t> haha;
haha.resize(10);
std::iota(haha.begin(), haha.end(), 0);
std::list<size_t>::iterator it = haha.begin();
std::advance(it, 5);
haha.insert(it, 33);
std::copy(haha.begin(), haha.end(), std::ostream_iterator<size_t>(cout, "\n") );
}
|
stl和generic programming是C++的兩大神兵,學C++卻不碰這兩者
會失去很多的樂趣及學習的機會
也將無法發揮出C++的性能
用心學習stl和generic programming不但能提高編程效率
還能讓學習的人學到另一種寫程式的方法和思考方式
I don't know how to make the iterator act like the iterator of stl
like
|
std::vector<T>::const_iterator it;
|
do anyone know how to do it?
Thank you very much