源代码
#include
using namespace std;
template< class T>
class Node {
public:
T data; //数据存储域
Node *next; //指针域
};
const int MaxSize = 100; //常量指定数组长度
template
class indirect
{
public:
indirect();
indirect(T a[], int n); //有参构造函数 使用了头插法
~indirect(); //析构函数
int Length(){return length;} //返回单链表的长度
T Get(int i); //按位查找,查找第i个节点的元素
int Locate(T x); //按值查找,查找链表中第一个值为x的元素,并返回序号
bool Insert(int i, T x); //插入元素,在第i个位置插入值x
bool Delete(int i); //删除节点,删除第i个节点
void PrintList(); //遍历节点
private:
Node
*first;
int length;
Node
*address[MaxSize]; //Node指针型的数组
};
template
indirect
::indirect() { for(int i=0;i
indirect
::indirect(T a[], int n) //有参构造函数, 使用头插法(注意点:头插法是将元素放在头结点的后面) { if (n > MaxSize) { throw "location"; } first = new Node
; //空链表的初始化,同无参构造函数 first->next = NULL; for (int i = 0; i
*s = new Node
; s->data = a[i]; s->next = first->next; first->next = s; } length = n; Node
*p =first; /*临时指针*/ for (int j = 0; j < length;j++) /*将指针地址存入数组*/ { p = p->next; address[j] = p; } } template
indirect
::~indirect() //析构函数 { while (first != NULL) { Node
*q = first; //遍历删除头指针指向的节点,将头指针暂存 first = first->next; //将头指针后移 delete q; //从链表中脱离出来的指针删除,释放内存 } length = 0; } template
T indirect
::Get(int i) //按位查找,返回第i个节点的元素 { if (i > length) { throw"location"; } else if(i<=0) { throw"location"; } Node
*p = address[i - 1]; return p->data; } template
int indirect
::Locate(T x) //按值查找,返回d第一个匹配值的序号 { Node
*p = first->next; int count = 1; while (p != NULL) { if (p->data == x) { return count; } p = p->next; count++; } return -1; //返回-1表示没有找到 } template
bool indirect
::Insert(int i, T x) //往链表中插入元素,i为要插入的位置,x为要插入的值 { if (i>length) { throw"location"; } else if (i<=0) { throw"location"; } else if (length>=MaxSize) { throw"error"; } Node
*p = first; int count = 0; int num = i - 1; while (p != NULL && count
next; count++; } if (p == NULL) { return false; } else { Node
*s = new Node
; s->data = x; s->next = p->next; p->next = s; length++; for (int j = length-1; j >= i; j--) /*间接寻址数组地址的改变(增加地址)*/ { address[j] = address[j - 1]; } address[i] = s; //新地址 return true; } } template
void indirect
::PrintList() { Node
*p = first->next; while (p != NULL) { cout << p->data << " "; p = p->next; //遍历的指针的后移,注意不能写成p++,因为这是节点 } } template
bool indirect
::Delete(int i) { if (i>length||i<=0) //相当于顺序表中的判空判满 { throw"location"; } Node
*p = first; int count = 0; while (p != NULL && count < i - 1) { p = p->next; count++; } if (p == NULL) { return false; } else { Node
*q; q = p->next; p->next = q->next; delete q; length--; for (int j = i; j <= length; j++) //间接寻址地址改变 { address[j-1] = address[j]; } return true; } } int main() { cout<<"\t ****************学生成绩间接寻址的实现**************\n"; cout<<"\t ****************************************************\n"; cout<<"\t *------------------------------------------*********\n"; cout<<"\t *****************[1]——输出表长********************\n"; cout<<"\t *****************[2]——按位查找********************\n"; cout<<"\t *****************[3]——按值查找********************\n"; cout<<"\t *****************[4]——插入************************\n"; cout<<"\t *****************[5]——删除************************\n"; cout<<"\t *****************[6]——遍历************************\n"; cout<<"\t *****************[7]——输出主菜单******************\n"; cout<<"\t *****************[8]——退出************************\n"; cout<<"\t *------------------------------------------*********\n"; cout<<"\t ****************************************************\n"; int a[8] = {78,87,89,82,80,91,98,94 }; indirect
sList(a, 8); int flag,i,x,t; flag=0; while(flag==0) { cout<<"please input the command(1~8):"<
>t; switch(t) { case 1: cout<<"the length is:"<
<
>i; x=sList.Get(i); cout<<"the number is:"<
<
>x; i=sList.Locate(x); cout<<"the location is:"<
<
>i; cout<<"the insert number is:"; cin>>x; sList.Insert(i,x); cout<<"insert successfully!"<
>i; sList.Delete(i); cout<<"delete successfully!"<
运行结果
运行程序,进入主界面
输入1,输出表长
输入6,遍历
输入2,查找第6个位置的成绩
输入3,查找成绩91所在的位置 ![](https://img-blog.csdn.net/20171010230715692)
输入4,在第六个位置插入85
![](https://img-blog.csdn.net/20171010231035512)
输入5,删除第二个位置的成绩
![](https://img-blog.csdn.net/20171010231150190)