#include<iostream>
using namespace std;
const int MaxSzie = 100;
template<class T>
struct Node
{
T data;
Node *next;
};
template<class T>
class IndirectList
{
public:
IndirectList();
IndirectList(T b[], int n);
~IndirectList();
int Length();
T Get(int i);
int Locate(T x);
bool Insert(int i, T x);
bool Delete(int i);
bool InsertHead(T x);
bool InsertTail(T x);
void ListTraverse();
bool changeList(int i, T x);
private:
Node<T> *first;
int m_Length;
Node<T> *a[MaxSzie];
};
template<class T>
IndirectList<T>::IndirectList()
{
for (int i = 0; i < MaxSzie; i++)
{
a[i] = null;
m_Length = 0;
}
}
template<class T>
IndirectList<T>::IndirectList(T b[], int n)
{
if (n > MaxSzie)
{
throw("溢出");
}
first = new Node<T>;
first->next = NULL;
for (int i = n-1; i>=0; i--)
{
Node<T> *s = new Node<T>;
s->data = b[i];
s->next = first->next;
first->next = s;
}
m_Length = n;
Node<T> *p = first;
for (int i = 0; i < m_Length; i++)
{
p = p->next;
a[i] = p;
}
}
template<class T>
IndirectList<T>::~IndirectList()
{
while (first != NULL)
{
Node<T> *q = first;
first = first->next;
delete q;
}
m_Length = 0;
}
template<class T>
int IndirectList<T>::Length()
{
return m_Length;
}
template<class T>
T IndirectList<T>::Get(int i)
{
if (i > m_Length || i < 1)
{
throw("错误!");
}
Node<T> *p = a[i - 1];
return p->data;
}
template<class T>
int IndirectList<T>::Locate(T x)
{
Node<T> *p = first->next;
int count = 1;
while (p != NULL)
{
if (p->data == x)
{
return count;
}
p = p->next;
count++;
}
return -1;
}
template<class T>
bool IndirectList<T>::Insert(int i, T x)
{
if (i > m_Length || i < 1)
{
return false;
}
Node<T> *p = first;
int count = 0;
while (p != NULL&& count<i-1)
{
p = p->next;
count++;
}
if (p == NULL)
{
return false;
}
Node<T> *s = new Node<T>;
s->data = x;
s->next = p->next;
p->next = s;
m_Length++;
for (int j = m_Length - 1; j >= i; j--)
{
a[j] = a[j - 1];
}
a[i] = s;
return true;
}
template<class T>
bool IndirectList<T>::Delete(int i)
{
if (i > m_Length || i < 1)
{
return false;
}
Node<T> *p = first;
int count = 0;
while (p != NULL&& count<i - 1)
{
p = p->next;
count++;
}
if (p == NULL)
{
return false;
}
Node<T> *q;
q = p->next;
p->next = q->next;
delete q;
m_Length--;
for (int j = i; j <= m_Length; j++)
{
a[j - 1] = a[j];
}
return true;
}
template<class T>
bool IndirectList<T>::InsertHead(T x)
{
if (m_Length > MaxSzie)
return false;
Node<T> *s = new Node<T>;
if (s == NULL)
{
return false;
}
s->data = x;
s->next = first->next;
first->next = s;
m_Length++;
for (int i = m_Length - 1; i > 0; i--)
{
a[i] = a[i - 1];
}
a[0] = first->next;
return true;
}
template<class T>
bool IndirectList<T>::InsertTail(T x)
{
if (m_Length > MaxSzie)
return false;
Node<T> *p = first;
Node<T> *s = new Node<T>;
if (s == NULL)
{
return false;
}
while (p->next != NULL)
{
p = p->next;
}
s->data = x;
p->next = s;
s->next = NULL;
m_Length++;
a[m_Length - 1] = s;
return true;
}
template<class T>
void IndirectList<T>::ListTraverse()
{
Node<T> *p = first->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
template<class T>
bool IndirectList<T>::changeList(int i, T x)
{
if (i > m_Length || i < 1)
return false;
Node<T> *p = a[i - 1];
p->data = x;
return true;
}
using namespace std;
const int MaxSzie = 100;
template<class T>
struct Node
{
T data;
Node *next;
};
template<class T>
class IndirectList
{
public:
IndirectList();
IndirectList(T b[], int n);
~IndirectList();
int Length();
T Get(int i);
int Locate(T x);
bool Insert(int i, T x);
bool Delete(int i);
bool InsertHead(T x);
bool InsertTail(T x);
void ListTraverse();
bool changeList(int i, T x);
private:
Node<T> *first;
int m_Length;
Node<T> *a[MaxSzie];
};
template<class T>
IndirectList<T>::IndirectList()
{
for (int i = 0; i < MaxSzie; i++)
{
a[i] = null;
m_Length = 0;
}
}
template<class T>
IndirectList<T>::IndirectList(T b[], int n)
{
if (n > MaxSzie)
{
throw("溢出");
}
first = new Node<T>;
first->next = NULL;
for (int i = n-1; i>=0; i--)
{
Node<T> *s = new Node<T>;
s->data = b[i];
s->next = first->next;
first->next = s;
}
m_Length = n;
Node<T> *p = first;
for (int i = 0; i < m_Length; i++)
{
p = p->next;
a[i] = p;
}
}
template<class T>
IndirectList<T>::~IndirectList()
{
while (first != NULL)
{
Node<T> *q = first;
first = first->next;
delete q;
}
m_Length = 0;
}
template<class T>
int IndirectList<T>::Length()
{
return m_Length;
}
template<class T>
T IndirectList<T>::Get(int i)
{
if (i > m_Length || i < 1)
{
throw("错误!");
}
Node<T> *p = a[i - 1];
return p->data;
}
template<class T>
int IndirectList<T>::Locate(T x)
{
Node<T> *p = first->next;
int count = 1;
while (p != NULL)
{
if (p->data == x)
{
return count;
}
p = p->next;
count++;
}
return -1;
}
template<class T>
bool IndirectList<T>::Insert(int i, T x)
{
if (i > m_Length || i < 1)
{
return false;
}
Node<T> *p = first;
int count = 0;
while (p != NULL&& count<i-1)
{
p = p->next;
count++;
}
if (p == NULL)
{
return false;
}
Node<T> *s = new Node<T>;
s->data = x;
s->next = p->next;
p->next = s;
m_Length++;
for (int j = m_Length - 1; j >= i; j--)
{
a[j] = a[j - 1];
}
a[i] = s;
return true;
}
template<class T>
bool IndirectList<T>::Delete(int i)
{
if (i > m_Length || i < 1)
{
return false;
}
Node<T> *p = first;
int count = 0;
while (p != NULL&& count<i - 1)
{
p = p->next;
count++;
}
if (p == NULL)
{
return false;
}
Node<T> *q;
q = p->next;
p->next = q->next;
delete q;
m_Length--;
for (int j = i; j <= m_Length; j++)
{
a[j - 1] = a[j];
}
return true;
}
template<class T>
bool IndirectList<T>::InsertHead(T x)
{
if (m_Length > MaxSzie)
return false;
Node<T> *s = new Node<T>;
if (s == NULL)
{
return false;
}
s->data = x;
s->next = first->next;
first->next = s;
m_Length++;
for (int i = m_Length - 1; i > 0; i--)
{
a[i] = a[i - 1];
}
a[0] = first->next;
return true;
}
template<class T>
bool IndirectList<T>::InsertTail(T x)
{
if (m_Length > MaxSzie)
return false;
Node<T> *p = first;
Node<T> *s = new Node<T>;
if (s == NULL)
{
return false;
}
while (p->next != NULL)
{
p = p->next;
}
s->data = x;
p->next = s;
s->next = NULL;
m_Length++;
a[m_Length - 1] = s;
return true;
}
template<class T>
void IndirectList<T>::ListTraverse()
{
Node<T> *p = first->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
template<class T>
bool IndirectList<T>::changeList(int i, T x)
{
if (i > m_Length || i < 1)
return false;
Node<T> *p = a[i - 1];
p->data = x;
return true;
}
#include"IndirectList.h"
#include<string>
#include<iostream>
using namespace std;
int main(void)
{
float score[5] = { 95,94.5,80.5,74,58 };
IndirectList<float> l1(score, 5);
l1.ListTraverse();
cout << "-----------------------------------------------" << endl;
cout << "第二个成绩为:" << l1.Get(2) << endl;
l1.Insert(3, 90);
l1.ListTraverse();
cout << "-----------------------------------------------" << endl;
cout << "成绩为77.5所在的位置为:" << l1.Locate(77.5) << endl;
l1.InsertHead(100);
l1.InsertTail(55.5);
l1.ListTraverse();
cout << "-----------------------------------------------" << endl;
cout << "成绩链长度为:" << l1.Length() << endl;
l1.changeList(2, 99);
l1.Delete(8);
l1.ListTraverse();
cout << "-----------------------------------------------" << endl;
system("pause");
return 0;
}