數據結構開發(9):循環鏈表與雙向鏈表
阿新 • • 發佈:2018-12-16
n-1 重復 exce 不想 temp 14. 後繼 特殊 mov
0.目錄
1.循環鏈表的實現
2.雙向鏈表的實現
3.小結
1.循環鏈表的實現
什麽是循環鏈表?
- 概念上
- 任意數據元素都有一個前驅和一個後繼
- 所有的數據元素的關系構成一個邏輯上的環
- 實現上
- 循環鏈表是一種特殊的單鏈表
- 尾結點的指針域保存了首結點的地址
循環鏈表的邏輯構成:
循環鏈表的繼承層次結構:
循環鏈表的實現思路:
- 通過模板定義CircleList類,繼承自LinkList類
- 定義內部函數 last_to_first(),用於將單鏈表首尾相連
- 特殊處理:首元素的插入操作和刪除操作
- 重新實現:清空操作和遍歷操作
循環鏈表的實現要點:
- 插入位置為 0 時:
- 頭結點和尾結點均指向新結點
- 新結點成為首結點插入鏈表
- 刪除位置為 0 時:
- 頭結點和尾結點指向位置為 1 的結點
- 安全銷毀首結點
實現循環鏈表(CircleList.h):
需要將父類LinkList.h中相關的函數聲明為虛函數
#ifndef LINKLIST_H #define LINKLIST_H #include "List.h" #include "Exception.h" namespace StLib { template <typename T> class LinkList : public List<T> { protected: struct Node : public Object { T value; Node* next; }; mutable struct : public Object { char reserved[sizeof(T)]; Node* next; } m_header; int m_length; int m_step; Node* m_current; Node* position(int i) const { Node* ret = reinterpret_cast<Node*>(&m_header); for(int p=0; p<i; p++) { ret = ret->next; } return ret; } virtual Node* create() { return new Node(); } virtual void destroy(Node* pn) { delete pn; } public: LinkList() { m_header.next = NULL; m_length = 0; m_step = 1; m_current = NULL; } bool insert(const T& e) { return insert(m_length, e); } bool insert(int i, const T& e) { bool ret = ((0 <= i) && (i <= m_length)); if( ret ) { Node* node = create(); if( node != NULL ) { Node* current = position(i); node->value = e; node->next = current->next; current->next = node; m_length++; } else { THROW_EXCEPTION(NoEnoughMemoryException, "No memory to insert new element ..."); } } return ret; } bool remove(int i) { bool ret = ((0 <= i) && (i < m_length)); if( ret ) { Node* current = position(i); Node* toDel = current->next; if( m_current == toDel ) { m_current = toDel->next; } current->next = toDel->next; m_length--; destroy(toDel); } return ret; } bool set(int i, const T& e) { bool ret = ((0 <= i) && (i < m_length)); if( ret ) { position(i)->next->value = e; } return ret; } virtual T get(int i) const { T ret; if( get(i, ret) ) { return ret; } else { THROW_EXCEPTION(IndexOutOfBoundsException, "Invalid parameter i to get element ..."); } return ret; } bool get(int i, T& e) const { bool ret = ((0 <= i) && (i < m_length)); if( ret ) { e = position(i)->next->value; } return ret; } int find(const T& e) const { int ret = -1; int i = 0; Node* node = m_header.next; while ( node ) { if( node->value == e ) { ret = i; break; } else { node = node->next; i++; } } return ret; } int length() const { return m_length; } void clear() { while ( m_header.next ) { Node* toDel = m_header.next; m_header.next = toDel->next; m_length--; destroy(toDel); } } virtual bool move(int i, int step = 1) { bool ret = (0 <= i) && (i < m_length) && (step > 0); if( ret ) { m_current = position(i)->next; m_step = step; } return ret; } virtual bool end() { return (m_current == NULL); } virtual T current() { if( !end() ) { return m_current->value; } else { THROW_EXCEPTION(InvalidOperationException, "No value at current position ..."); } } virtual bool next() { int i = 0; while( (i < m_step) && !end() ) { m_current = m_current->next; i++; } return (i == m_step); } ~LinkList() { clear(); } }; } #endif // LINKLIST_H
實現CircleList.h
#ifndef CIRCLELIST_H #define CIRCLELIST_H #include "LinkList.h" namespace StLib { template <typename T> class CircleList : public LinkList<T> { protected: typedef typename LinkList<T>::Node Node; int mod(int i) const { return (this->m_length == 0) ? 0 : (i % this->m_length); } Node* last() const { return this->position(this->m_length-1)->next; } void last_to_first() const { last()->next = this->m_header.next; } public: bool insert(const T& e) { return insert(this->m_length, e); } bool insert(int i, const T& e) { bool ret; i = i % (this->m_length + 1); ret = LinkList::insert(i, e); if( ret && (i == 0) ) { last_to_first(); } return ret; } bool remove(int i) { bool ret; i = mod(i); if( i == 0 ) { Node* toDel = this->m_header.next; if( toDel != NULL ) { this->m_header.next = toDel->next; this->m_length--; if( this->m_length > 0 ) { last_to_first(); if( this->m_current == toDel ) { this->m_current = toDel->next; } } else { this->m_header.next = NULL; this->m_current = NULL; } this->destroy(toDel); } else { ret = false; } } else { ret = LinkList<T>::remove(i); } return ret; } bool set(int i, const T& e) { return LinkList<T>::set(mod(i), e); } T get(int i) const { return LinkList<T>::get(mod(i)); } T get(int i, const T& e) const { return LinkList<T>::get(mod(i), e); } int find(const T& e) const { int ret = -1; Node* slider = this->m_header.next; for(int i=0; i<this->m_length; i++) { if( slider->value == e ) { ret = i; break; } slider = slider->next; } return ret; } void clear() { while( this->m_length > 1 ) { remove(1); } if( this->m_length == 1 ) { Node* toDel = this->m_header.next; this->m_header.next = NULL; this->m_length = 0; this->m_current = NULL; this->destroy(toDel); } } bool move(int i, int step) { return LinkList<T>::move(mod(i), step); } bool end() { return (this->m_length == 0) || (this->m_current == NULL); } ~CircleList() { clear(); } }; } #endif // CIRCLELIST_H
循環鏈表的應用——約瑟夫環問題:
已知 n 個人( 以編號 0,1,2,3,... ,n-1 分別表示 )圍坐在一張圓桌周圍。從編號為 k 的人開始報數,數到 m 的那個人出列;他的下一個人又從 1 開始報數,數到 m 的那個人又出列;依此規律重復下去,直到圓桌周圍的人全部出列。
小故事:
在羅馬人占領喬塔帕特後,39個猶太人與 Josephus 及他的朋友躲到一個洞中,39個猶太人決定寧願死也不要被敵人抓到,於是決定了一個自殺方式,41個人排成一個圓圈,由第1個人開始報數,每報數到第3人該人就必須自殺,然後再由下一個重新報數,直到所有人都自殺身亡為止。然而 Josephus 和他的朋友並不想遵從。那麽,一開始要站在什麽地方才能避免被處決?
main.cpp解決約瑟夫環問題,測試CircleList.h:
#include <iostream>
#include "CircleList.h"
using namespace std;
using namespace StLib;
void josephus(int n, int s, int m)
{
CircleList<int> c1;
for(int i=1; i<=n; i++)
{
c1.insert(i);
}
c1.move(s-1, m-1);
while( c1.length() > 0 )
{
c1.next();
cout << c1.current() << ", ";
c1.remove(c1.find(c1.current()));
}
cout << endl;
}
int main()
{
josephus(41, 1, 3);
return 0;
}
運行結果為:
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 1, 5, 10, 14, 19, 23, 28, 32, 37, 41, 7, 13, 20, 26, 34, 40, 8, 17, 29, 38, 11, 25, 2, 22, 4, 35, 16, 31,
2.雙向鏈表的實現
單鏈表的另一個缺陷:
- 單向性
- 只能從頭結點開始高效訪問鏈表中的數據元素
- 缺陷
- 如果需要逆向訪問單鏈表中的數據元素將極其低效
新的線性表
- 設計思路:
- 在“單鏈表”的結點中增加一個指針 pre,用於指向當前結點的前驅結點。
雙向鏈表的繼承層次結構:
DualLinkList 的定義:
實現DualLinkList.h:
#ifndef DUALLINKLIST_H
#define DUALLINKLIST_H
#include "List.h"
#include "Exception.h"
namespace StLib
{
template <typename T>
class DualLinkList : public List<T>
{
protected:
struct Node : public Object
{
T value;
Node* next;
Node* pre;
};
mutable struct : public Object
{
char reserved[sizeof(T)];
Node* next;
Node* pre;
} m_header;
int m_length;
int m_step;
Node* m_current;
Node* position(int i) const
{
Node* ret = reinterpret_cast<Node*>(&m_header);
for(int p=0; p<i; p++)
{
ret = ret->next;
}
return ret;
}
virtual Node* create()
{
return new Node();
}
virtual void destroy(Node* pn)
{
delete pn;
}
public:
DualLinkList()
{
m_header.next = NULL;
m_header.pre = NULL;
m_length = 0;
m_step = 1;
m_current = NULL;
}
bool insert(const T& e)
{
return insert(m_length, e);
}
bool insert(int i, const T& e)
{
bool ret = ((0 <= i) && (i <= m_length));
if( ret )
{
Node* node = create();
if( node != NULL )
{
Node* current = position(i);
Node* next = current->next;
node->value = e;
node->next = next;
current->next = node;
if( current != reinterpret_cast<Node*>(&m_header) )
{
node->pre = current;
}
else
{
node->pre = NULL;
}
if( next != NULL )
{
next->pre = node;
}
m_length++;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to insert new element ...");
}
}
return ret;
}
bool remove(int i)
{
bool ret = ((0 <= i) && (i < m_length));
if( ret )
{
Node* current = position(i);
Node* toDel = current->next;
Node* next = toDel->next;
if( m_current == toDel )
{
m_current = next;
}
current->next = next;
if( next != NULL )
{
next->pre = toDel->pre;
}
m_length--;
destroy(toDel);
}
return ret;
}
bool set(int i, const T& e)
{
bool ret = ((0 <= i) && (i < m_length));
if( ret )
{
position(i)->next->value = e;
}
return ret;
}
virtual T get(int i) const
{
T ret;
if( get(i, ret) )
{
return ret;
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Invalid parameter i to get element ...");
}
return ret;
}
bool get(int i, T& e) const
{
bool ret = ((0 <= i) && (i < m_length));
if( ret )
{
e = position(i)->next->value;
}
return ret;
}
int find(const T& e) const
{
int ret = -1;
int i = 0;
Node* node = m_header.next;
while ( node )
{
if( node->value == e )
{
ret = i;
break;
}
else
{
node = node->next;
i++;
}
}
return ret;
}
int length() const
{
return m_length;
}
void clear()
{
while ( m_length > 0 )
{
remove(0);
}
}
virtual bool move(int i, int step = 1)
{
bool ret = (0 <= i) && (i < m_length) && (step > 0);
if( ret )
{
m_current = position(i)->next;
m_step = step;
}
return ret;
}
virtual bool end()
{
return (m_current == NULL);
}
virtual T current()
{
if( !end() )
{
return m_current->value;
}
else
{
THROW_EXCEPTION(InvalidOperationException, "No value at current position ...");
}
}
virtual bool next()
{
int i = 0;
while( (i < m_step) && !end() )
{
m_current = m_current->next;
i++;
}
return (i == m_step);
}
virtual bool pre()
{
int i = 0;
while( (i < m_step) && !end() )
{
m_current = m_current->pre;
i++;
}
return (i == m_step);
}
~DualLinkList()
{
clear();
}
};
}
#endif // DUALLINKLIST_H
main.cpp測試
#include <iostream>
#include "DualLinkList.h"
using namespace std;
using namespace StLib;
int main()
{
DualLinkList<int> d1;
for(int i=0; i<5; i++)
{
d1.insert(0, i);
d1.insert(0, 5);
}
cout << "begin" << endl;
d1.move(d1.length()-1);
while( !d1.end() )
{
if( d1.current() == 5 )
{
cout << d1.current() << endl;
d1.remove(d1.find(d1.current()));
}
else
{
d1.pre();
}
}
cout << "end" << endl;
for(d1.move(d1.length()-1); !d1.end(); d1.pre())
{
cout << d1.current() << endl;
}
return 0;
}
運行結果為:
begin
5
5
5
5
5
end
0
1
2
3
4
深度思考——開放性問題:
- DualLinkList 和 LinkList 中存在很多完全一樣的代碼,如何進行重構降低代碼的冗余性?冗余代碼的出現是否意味著 DualLinkList 和 LinkList 之間應該是繼承關系?
擴展練習——雙向鏈表的子類:
3.小結
- 循環鏈表是一種特殊的單鏈表
- 尾結點的指針域保存了首結點的地址
- 特殊處理首元素的插入操作和刪除操作
- 重新實現清空操作和遍歷操作
- 雙向鏈表是為了彌補單鏈表的缺陷而重新設計的
- 在概念上,雙向鏈表不是單鏈表,沒有繼承關系
- 雙向鏈表中的遊標能夠直接訪問當前結點的前驅和後繼
- 雙向鏈表是線性表概念的最終實現( 更貼近理論上的線性表 )
數據結構開發(9):循環鏈表與雙向鏈表