标签:操作 parameter cout 构造 函数 深度 需求量 复制 编译错误
顺序存储结构线性表的最大问题是:
链式存储的定义:
链式存储逻辑结构:
专业术语的统一:
链表中的基本概念:
单链表中的结点定义:
单链表中的内部结构:
头结点在单链表中的意义是:辅助数据元素的定位,方便插入和删除操作;因此,头结点不存储实际的数据元素。
在目标位置处插入数据元素:
node->value = e;
node->next = current->next;
current->next = node;
在目标位置处删除数据元素:
toDel = current->next;
current->next = toDel->next;
delete toDel;
本节目标:
LinkList 设计要点:
LinkList的定义:
链表的实现 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 Node m_header;
int m_length;
public:
LinkList()
{
m_header.next = NULL;
m_length = 0;
}
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 = new Node();
if( node != NULL )
{
Node* current = &m_header;
for(int p=0; p<i; p++)
{
current = current->next;
}
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 = &m_header;
for(int p=0; p<i; p++)
{
current = current->next;
}
Node* toDel = current->next;
current->next = toDel->next;
delete toDel;
m_length--;
}
return ret;
}
bool set(int i, const T& e)
{
bool ret = ((0 <= i) && (i < m_length));
if( ret )
{
Node* current = &m_header;
for(int p=0; p<i; p++)
{
current = current->next;
}
current->next->value = e;
}
return ret;
}
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 )
{
Node* current = &m_header;
for(int p=0; p<i; p++)
{
current = current->next;
}
e = current->next->value;
}
return ret;
}
int length() const
{
return m_length;
}
void clear()
{
while ( m_header.next )
{
Node* toDel = m_header.next;
m_header.next = toDel->next;
delete toDel;
}
m_length = 0;
}
~LinkList()
{
clear();
}
};
}
#endif // LINKLIST_H
main.cpp测试
#include <iostream>
#include "LinkList.h"
using namespace std;
using namespace StLib;
int main()
{
LinkList<int> list;
for(int i=0; i<5; i++)
{
list.insert(0, i);
list.set(0, i*i);
}
for(int i=0; i<list.length(); i++)
{
cout << list.get(i) << endl;
}
cout << endl;
list.remove(2);
for(int i=0; i<list.length(); i++)
{
cout << list.get(i) << endl;
}
cout << endl;
list.clear();
for(int i=0; i<list.length(); i++)
{
cout << list.get(i) << endl;
}
return 0;
}
运行结果为:
16
9
4
1
0
16
9
1
0
问题:
头结点的隐患:
代码优化:
代码优化(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;
Node* position(int i) const
{
Node* ret = reinterpret_cast<Node*>(&m_header);
for(int p=0; p<i; p++)
{
ret = ret->next;
}
return ret;
}
public:
LinkList()
{
m_header.next = NULL;
m_length = 0;
}
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 = new Node();
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;
current->next = toDel->next;
delete toDel;
m_length--;
}
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;
}
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 length() const
{
return m_length;
}
void clear()
{
while ( m_header.next )
{
Node* toDel = m_header.next;
m_header.next = toDel->next;
delete toDel;
}
m_length = 0;
}
~LinkList()
{
clear();
}
};
}
#endif // LINKLIST_H
问题
遗失的操作——find:
=0:数据元素在线性表中第一次出现的位置
数据元素查找示例:
实现查找find函数:
在List.h中加入
virtual int find(const T& e) const = 0;
在SeqList.h中加入
int find(const T& e) const
{
int ret = -1;
for(int i=0; i<m_length; i++)
{
if( m_array[i] == e )
{
ret = i;
break;
}
}
return ret;
}
在LinkList.h中加入
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;
}
但是若用类对象来进行测试,会有严重的bug:
#include <iostream>
#include "LinkList.h"
using namespace std;
using namespace StLib;
class Test
{
int i;
public:
Test(int v = 0)
{
i = v;
}
};
int main()
{
Test t1;
Test t2;
Test t3;
LinkList<Test> list;
return 0;
}
编译错误信息:
error C2678: 二进制“==”: 没有找到接受“Test”类型的左操作数的运算符(或没有可接受的转换)
于是应该在顶层父类Object中实现重载比较操作符
Object.h
bool operator == (const Object& obj);
bool operator != (const Object& obj);
Object.cpp
bool Object::operator == (const Object& obj)
{
return (this == &obj);
}
bool Object::operator != (const Object& obj)
{
return (this != &obj);
}
main.cpp再测试
#include <iostream>
#include "LinkList.h"
using namespace std;
using namespace StLib;
class Test : public Object
{
int i;
public:
Test(int v = 0)
{
i = v;
}
bool operator == (const Test& t)
{
return (i == t.i);
}
};
int main()
{
Test t1(1);
Test t2(2);
Test t3(3);
LinkList<Test> list;
list.insert(t1);
list.insert(t2);
list.insert(t3);
cout << list.find(t2) << endl;
return 0;
}
运行结果为:
1
时间复杂度对比分析:
有趣的问题:
效率的深度分析:
工程开发中的选择:
标签:操作 parameter cout 构造 函数 深度 需求量 复制 编译错误
原文地址:https://www.cnblogs.com/PyLearn/p/10116981.html