线性表:是N个相同数据元素的有序系列。
链表是线性表的链接存储结构
附件:程序源代码
template
struct Node
{
T data;
Node*next;
};
template
class LinkList
{
public:
LinkList();
LinkList(T a[], int n);
~LinkList();
int Length();
T Get(int i);
int Locate(T x);
void Update(int i,T x);
void Insert(int i, T x);
T Delete(int i);
void PrinList();
private:
Node*first;
};
template
LinkList::LinkList()
{
first = new Node; first->next = NULL;
}
template
LinkList::LinkList(T a[], int n)
{
first = new Node;
Node *r, *s;
r = first;
for (int i = 0; i
using namespace std;
class Student
{
public:
Student(){} //构造函数
Student(string sname, int iage){ name = sname; age = iage; }
~Student(){} //析构函数
friend ostream& operator << (ostream &oo, const Student &stu);
friend bool operator != (const Student &stu1, const Student &stu2);
private:
string name;
int age;
};
ostream& operator << (ostream &oo, const Student &stu);
bool operator != (const Student &stu1, const Student &stu2);
ostream& operator << (ostream &oo, const Student &stu)
{
oo << “姓名: ” << stu.name << “, ” << “年龄: ” << stu.age;
return oo;
}
bool operator != (const Student &stu1, const Student &stu2)
{
return ((stu1.name != stu2.name) || (stu1.age != stu2.age));
}
using namespace std;
int main()
{
Student studengArray[] = { Student(“陈折江”, 19), Student(“程淼”, 20), Student(“陈曾汉”, 21), Student(“戴鑫”, 22), Student(“陈鹏”, 23)};
LinkListb(studengArray, 5);
cout << "执行插入前单链表b为:" << endl;
b.PrinList();
cout << "插入前单链表b的长度为:" << b.Length() << endl;
cout << endl;
Student mochou = Student("几斤",22);
try
{
b.Insert(3, mochou);
}
catch (char*wrong)
{
cout << wrong;
}
cout << "执行插入后单链表b为:" << endl;
b.PrinList();
cout << "执行插入后单链表b的长度为:" << b.Length()<< endl;
cout << endl;
cout << "按位置查找第二个元素,该元素信息为:" << b.Get(2) << endl;
cout << endl;
cout << "按值\"几斤\"查找,\"几斤\"在单链表的位置为:" << b.Locate(mochou) << endl;
cout << endl;
try
{
if (b.Length()){
cout << "执行删除第一个元素的操作" << endl;
b.Delete(1);
cout << "删除第一个元素后单链表的长度为:" << b.Length() << endl;
}
else{
cout << "顺序表b长度为0" << endl;
}
}
catch (char*wrong)
{
cout << wrong;
}
cout << "执行删除操作后单链表b为:" << endl;
b.PrinList();
cout << endl;
cout << "执行修改操作" << endl;
cout << "修改前单链表b第三个元素是:" <
}
树是n个点的有限集合。
二叉树:是n个结点的有限集合,该集合(空集)称为空二叉树,或者由一个根节点和两块互不相交、分别称为根节点的左子树和右子树的二叉树组成。
满二叉树,完全二叉树,二叉排序树,平衡二叉树。
应用有哈夫曼树,
双向列表的增删改查
双向列表的模板
templat
struct Dulnode
{
DataType data;
DulNode*prior,*next;
};
template
class sLinkList
{
public:
LinkList();
LinkList(T a[], int n);
~LinkList();
int Length();
T Get(int i);
int Locate(T x);
void Update(int i,T x);
void Insert(int i, T x);
T Delete(int i);
void PrinList();
private:
Node*first;
};
双向链表插入
template
void Link:: Insert(int i; T x)
{
Node *p; int j;
p = first; j = 0;
while(p!=Null && j