当前位置:
文档之家› 第 七 讲 单链表、循环链表、双向链表 10 23
第 七 讲 单链表、循环链表、双向链表 10 23
// 删除元素
StatusCode Insert(int position, const ElemType &e);
// 插入元素
LinkList(const LinkList<ElemType> ©);
// 复制构造函数
LinkList<ElemType> &operator =(const
SimpleCircLinkList();
// 无参数的构造函数
virtual ~SimpleCircLinkList(); // 析构函数
int Length() const;
// 求线性表长度
bool Empty() const; // 判断线性表是否为空
void Clear();
// 将线性表清空
4
本节课学习目标
1、循环表的定义 2、双向表的定义 3、线性表的应用
5
数据的逻辑结构可归结为以下四类
线性结构 树形结构 图状结构 集合结构
6
第2章线性表
7
循环链表 (Circular List)
8
循环链表是单链表的变形 唯一一个区别:
循环链表最后一个结点的next指针不为 0 (NULL),而是指向了表的前端。 增加一个标记:
}
17
双向链表 (Doubly Linked List)
双向链表是指在前驱和后继方向都能游历(遍历)的线 性链表。
双向链表每个结点结构:
back data next (左链指针) (数据) (右链指针)
前驱方向
后继方向
双向链表通常采用带表头结点的循环链表形式。
18
双向循环链表示例
… …
a 非空双向链
人,令其出列。然后再从下一个人开始,从
1顺时针报数,报到第m个人,再令其出
列,…,如此下去, 直到圆圈中只剩一个人 为止。此人即为优胜者。
例如 n = 8 m = 3
14
15
// 文件路径名:s2_5\alg.h
void Josephus(int n, int m)
// 操作结果:n个人围成一个圆圈,首先第1个人从1开始一
DbNode<ElemType > *linknext) }
20
双向链表类模板
Template<class ElemType> Class DbLinkList { Protected:
DbNode<ElemType> *head; DbNode<ElemYype> *GetElemPtr(int pos); Public: DbLinkList(); ~DbLinkList(); int Length() const; Bool Empty()const;
// 报数到的人在链表中序号
int out, winer;
for (int k = 1; k <= n; k++) la.Insert(k, k);
// 建立数据域为1,2,..,n的循环链表
16
cout << "出列者:";
for (int i = 1; i < n; i++)
{ // 循环n-1次,让n-1个个出列
void SetZero();
// 将多项式置为0
void Display();
// 显示多项式
void InsItem( const PolyItem &item);// 插入一项
Polynomial operator +(const Polynomial &p) const;
// 加法运算符重载
// 个人一个人顺时针报数,报到第m个人,令其出列。
// 然后再从下一个人开始,从1顺时针报数报到第m
// 个人,再令其出列,…,如此下去, 直到圆圈中只
// 剩一个人为止。此人即为优胜者
{
SimpleCircLinkList<int> la; // 定义空循环链表
int position = 0;
但是对于形如
S(x) = 1 + 3x10000 – 2x20000
的多项式,上述表示方法是否合适?
29
一般情况下的一元稀疏多项式可写成
Pn(x) = p1xe1 + p2xe2 + ┄ + pmxem 其中:pi 是指数为ei 的项的非零系数,
0≤ e1 < e2 < ┄ < em = n
可以下列线性表表示:
// 无参数的构造函数
virtual ~LinkList();
// 析构函数
int Length() const;
// 求线性表长度
bool Empty() const; // 判断线性表是否为空
void Clear();
// 将线性表清空
void Traverse(void (*Visit)( const ElemType &))
为简化操作,在循环链表中往往加入表头结点。 循 环 表 特 点:
循环链表的特点是:只要知道表中某一结点的 地址,就可搜寻到所有其他结点的地址。
9
循环链表示例
head
a1
… an
head
(a)非空循环链表 (b)空循环链表
10
// 简单循环链表类 template <class ElemType> class SimpleCircLinkList { protected: // 循环链表实现的数据成员:
循 环 链 表、双向链表
时 间:2013 – 10 - 23
1
劝学语
玩 物 丧 志,玩 人 丧 德。 ——《尚书》
读 书须用意,一字值千金。 ——《 弟 子 规 》
2
只要有了积极主动的态度, 没有什么目标是不能达到的。 —— 李 开 复
3
第 七 次上课内容
1、 循环链表 P 39 2、双向链表 P 43 3、线性表的应用 P55
LinkList<ElemType> ©);
// 赋值语句重载
};
27
重写 GetElemPtr函数
Node<ElemType> *LinkList::GetElemPtr(int pos) const { if(curPos > pos) { curPose =0; curPtr = head; }
class LinkList
{
protected:
// 链表实现的数据成员:
Node<ElemType> *head; // 头结点指针
mutable int curPosition; // 当前位置的序号
mutable Node<ElemType> * curPtr;
// 指向当前位置的指针
int count;
void Traverse(void (*Visit)(const ElemType &))
const;
// 遍历线性表
StatusCode GetElem(int position, ElemType &e)
const;
// 求指定位置的元素
StatusCode SetElem(int position, const ElemType
23
Mutable修饰符
mutable 可以用来指出,即使结构或者类变量为 const,其某个成员也可以被修改 。 例 如: Class A { const int data; bool Set(int da) const { data =da; }
24
// 线性链表类
template <class ElemType>
21
Bool Empty() const; Void Clear(); bool GetElem(int pos,ElemType &e); bool SetElem(int pos ,ElemType e); bool Delete(int pos ,ElemType &e); bool Insert(int pos ,ElemType &e); };
// 复制构造函数 SimpleCircLinkList<ElemType> &operator =(const
SimpleCircLinkList<ElemType> ©); // 赋值语句重载 };
13
用循环链表求解约瑟夫问题
约瑟夫问题的提法
n 个人围成一个圆圈,首先第1个人从1开 始一个人一个人顺时针报数, 报到第m 个
b 空双向链
19
双向链表的实现
一、数据结点类模板 template<class ElemType> struct DbNode { ElemType data; DbNode<ElemType > *back; DbNode<ElemType> *next; DbNode (); DbNode(); DbNode (DbNode<ElemType > *linkback,
((p1, e1), (p2, e2), ┄, (pm,em) )
30
例 如: P999(x) = 7x3 - 2x12 - 8x999
可用线性表 ( (7, 3), (-2, 12), (-8, 999) )
表示
31
// 多项式类
class Polynomial
{
protected: