@[(常用的数据结构(c++实现))]
线性表
定义
线性表:
零个或多个数据元素的有限序列,线性表包括顺序表和链表。
顺序表(其实就是数组)里面的元素地址是连续的。
链表里面的节点的地址是离散的,是通过指针连起来的。
下面给出顺序表的实现:
#include <iostream>
#include <string>
#define MaxSize 100
template <class DataType>
class SeqList
{
public:
SeqList();
SeqList(DataType* a, int n);
DataType Get(int pos);
int Locate(DataType value);
void Insert(int pos, DataType value);
DataType Delete(int pos);
void PrintList();
private:
DataType data[MaxSize];
int length;
};
template<class DataType>
SeqList<DataType>::SeqList()
{
length = 0;
}
template <class DataType>
SeqList<DataType>::SeqList(DataType* a, int n)
{
length = 0;
if(n > MaxSize) throw "wrong parameter";
for(int i = 0; i < n; i++)
{
data[i] = a[i];
}
length = n;
}
/**
* @brief 按位查找 时间复杂度O(1)
*/
template <class DataType>
DataType SeqList<DataType>::Get(int pos)
{
if(pos < 0 || pos > length)
{
throw "wrong Location";
}
else
{
return data[pos-1];
}
}
template <class DataType>
int SeqList<DataType>::Locate(DataType value)
{
for(int i = 0; i < length; i++)
{
if(data[i] == value)
{
return i+1;
}
}
return 0;
}
template <class DataType>
void SeqList<DataType>::Insert(int pos, DataType value)
{
if(length >= MaxSize)
{
throw "OverFlow";
}
if(pos < 1 || pos > MaxSize)
{
throw "bad location";
}
for(int j = length; j >= pos; j--)
{
data[j] = data[j-1];
}
data[pos-1] = value;
length++;
}
template <class DataType>
DataType SeqList<DataType>::Delete(int pos)
{
DataType x;
if(length == 0)
{
throw "Underflow";
}
if(pos < 1 || pos > length)
{
throw "Location";
}
x = data[pos-1];
for(int j = pos; j < length; j++)
{
data[j-1] = data[j];
}
length--;
return x;
}
template <class DataType>
void SeqList<DataType>::PrintList()
{
for(int i = 0; i < length; i++)
{
std::cout << data[i] << std::endl;
}
}
class student
{
public:
std::string m_name;
int m_age;
student() {}
student(const std::string& name, const int& age)
:m_name(name), m_age(age){}
bool operator==(const student& st)
{
return this->m_name == st.m_name && this->m_age == st.m_age;
}
friend std::ostream& operator<<(std::ostream& os, const student& st)
{
os << "学生姓名:" << st.m_name << " 学生年龄:" << st.m_age;
return os;
}
student& operator=(const student& st)
{
this->m_name = st.m_name;
this->m_age = st.m_age;
return *this;
}
};
int main()
{
student a("aa", 1);
student b("bb", 2);
SeqList<student> seq;
seq.Insert(1, a);
seq.Insert(2, b);
seq.PrintList();
std::cout << seq.Get(1) << std::endl;
std::cout << seq.Locate(b) << std::endl;
seq.Delete(1);
seq.PrintList();
}
下面是链表的实现:
#include<iostream>
using namespace std;
template<class DataType>
struct Node
{
DataType data;
Node<DataType> *next;
};
template<class DataType>
class LinkList
{
public:
LinkList();
~LinkList();
int Length();
DataType Get(int i);
int Locate(const DataType& value);
void Insert(int i, const DataType& x);
DataType Delete(int i);
void PrintList();
Node<DataType> *first;
private:
};
template<class DataType>
LinkList<DataType>::LinkList()
{
first = new Node<DataType>();
first->next = NULL;
}
template<class DataType>
LinkList<DataType>::~LinkList()
{
while (first != NULL)
{
Node<DataType>* q = first;
first = first->next;
delete q;
}
}
template<class DataType>
int LinkList<DataType>::Length()
{
Node<DataType>* p = first->next;
int count = 0;
while (p != NULL)
{
p = p->next;
count++;
}
return count;
}
template<class DataType>
DataType LinkList<DataType>::Get(int i)
{
Node<DataType>* p = first->next;
int count = 1;
while (p != NULL && count < i)
{
p = p->next;
count++;
}
if (p == NULL)
{
throw "Location";
}
else
{
return p->data;
}
}
template<class DataType>
int LinkList<DataType>::Locate(const DataType& value)
{
Node<DataType> *p = first->next;
int count = 1;
while (p != NULL)
{
if (p->data == value)
{
return count;
}
p = p->next;
count++;
}
return 0;
}
template<class DataType>
void LinkList<DataType>::Insert(int i, const DataType& value)
{
Node<DataType> *p = first;
int count = 0;
while (p != NULL && count < i - 1)
{
p = p->next;
count++;
}
if (p == NULL)
{
throw "Location";
}
else
{
Node<DataType> *s = new Node<DataType>;
s->data = value;
s->next = p->next;
p->next = s;
}
}
template<class DataType>
DataType LinkList<DataType>::Delete(int i)
{
Node<DataType> *p = first;
int count = 1;
while (p != NULL && count < i)
{
p = p->next;
count++;
}
if (p == NULL)
{
throw "Location";
}
else
{
Node<DataType> *q = p->next;
DataType value = q->data;
p->next = q->next;
delete q;
return value;
}
}
template<class DataType>
void LinkList<DataType>::PrintList()
{
Node<DataType> *p = first->next;
while (p != NULL)
{
cout << p->data << endl;
p = p->next;
}
}
class student
{
public:
std::string m_name;
int m_age;
student() {}
student(const std::string& name, const int& age)
:m_name(name), m_age(age){}
bool operator==(const student& st)
{
return this->m_name == st.m_name && this->m_age == st.m_age;
}
friend std::ostream& operator<<(std::ostream& os, const student& st)
{
os << "学生姓名:" << st.m_name << " 学生年龄:" << st.m_age;
return os;
}
student& operator=(const student& st)
{
this->m_name = st.m_name;
this->m_age = st.m_age;
return *this;
}
};
int main()
{
LinkList<student> p;
student a("aa", 1);
student b("bb", 2);
student c("cc", 3);
p.Insert(1, a);
p.Insert(2, b);
p.Insert(3, c);
p.PrintList();
std::cout << p.Locate(b) << std::endl;
std::cout << p.Get(3) << std::endl;
p.Delete(1);
p.PrintList();
return 0;
}
双向链表的实现:
#include<iostream>
using namespace std;
template<class DataType>
struct Node
{
DataType data;
Node<DataType>* next;
Node<DataType>* pre;
};
template<class DataType>
class LinkList
{
public:
LinkList();
~LinkList();
int Length();
DataType Get(int i);
int Locate(const DataType& value);
void Insert(int i, const DataType& x);
DataType Delete(int i);
void PrintList();
Node<DataType> *first;
private:
};
template<class DataType>
LinkList<DataType>::LinkList()
{
first = new Node<DataType>();
first->next = NULL;
}
template<class DataType>
LinkList<DataType>::~LinkList()
{
while (first != NULL)
{
Node<DataType>* q = first;
first = first->next;
delete q;
}
}
template<class DataType>
int LinkList<DataType>::Length()
{
Node<DataType>* p = first->next;
int count = 0;
while (p != NULL)
{
p = p->next;
count++;
}
return count;
}
template<class DataType>
DataType LinkList<DataType>::Get(int i)
{
Node<DataType>* p = first->next;
int count = 1;
while (p != NULL && count < i)
{
p = p->next;
count++;
}
if (p == NULL)
{
throw "Location";
}
else
{
return p->data;
}
}
template<class DataType>
int LinkList<DataType>::Locate(const DataType& value)
{
Node<DataType> *p = first->next;
int count = 1;
while (p != NULL)
{
if (p->data == value)
{
return count;
}
p = p->next;
count++;
}
return 0;
}
template<class DataType>
void LinkList<DataType>::Insert(int i, const DataType& value)
{
Node<DataType> *p = first;
int count = 0;
while (p != NULL && count < i - 1)
{
p = p->next;
count++;
}
if (p == NULL)
{
throw "Location";
}
else
{
Node<DataType> *s = new Node<DataType>;
s->data = value;
s->pre = p;
s->next = p->next;
p->next = s;
}
}
template<class DataType>
DataType LinkList<DataType>::Delete(int i)
{
Node<DataType> *p = first;
int count = 1;
while (p != NULL && count < i)
{
p = p->next;
count++;
}
if (p == NULL)
{
throw "Location";
}
else
{
Node<DataType> *q = p->next;
DataType value = q->data;
p->next = q->next;
if(q->next != nullptr)
{
q->next->pre = p;
}
delete q;
return value;
}
}
template<class DataType>
void LinkList<DataType>::PrintList()
{
Node<DataType> *p = first->next;
while (p != NULL)
{
cout << p->data << endl;
p = p->next;
}
}
class student
{
public:
std::string m_name;
int m_age;
student() {}
student(const std::string& name, const int& age)
:m_name(name), m_age(age){}
bool operator==(const student& st)
{
return this->m_name == st.m_name && this->m_age == st.m_age;
}
friend std::ostream& operator<<(std::ostream& os, const student& st)
{
os << "学生姓名:" << st.m_name << " 学生年龄:" << st.m_age;
return os;
}
student& operator=(const student& st)
{
this->m_name = st.m_name;
this->m_age = st.m_age;
return *this;
}
};
int main()
{
LinkList<student> p;
student a("aa", 1);
student b("bb", 2);
student c("cc", 3);
p.Insert(1, a);
p.Insert(2, b);
p.Insert(3, c);
p.PrintList();
std::cout << p.Locate(b) << std::endl;
std::cout << p.Get(3) << std::endl;
p.Delete(1);
p.PrintList();
return 0;
}
参考博客:
https://blog.youkuaiyun.com/qq_30611601/article/details/79516986