源代码
#include
using namespace std;
const int MaxSize = 100; //自定义表长
template
class Node {
public:
T data;
int next; //存储指向下一个节点的数组的下标
};
template
class staticLink {
public:
staticLink();
staticLink(T a[],int n);
~staticLink(){};
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:
int first;
int avail;
int length;
Node
SList[MaxSize];
};
template
staticLink
::staticLink() { first=0; avail=1; sList[0].next=-1; for(int i=1;i
staticLink
::staticLink(T a[],int n) { for (int i = 0; i < MaxSize; i++) { SList[i].next =i+1; } length = 0; SList[MaxSize-1].next=-1; avail = 2; first = 1; SList[first].next = -1; //利用头插法插入元素 for (int j = 0; j < n;j++) //判断链中是否已满 { if (avail==-1) { break; } int s = avail; //空链后移 avail = SList[avail].next; //新链上值 SList[s].data = a[j]; //上链 SList[s].next = SList[first].next; SList[first].next = s; length++; } } template
T staticLink
::Get(int i) { if (i <= 0 || i > length) { throw"location error"; } int s = first; for (int j = 0; j < i; j++) { s = SList[s].next; } return SList[s].data; } template
int staticLink
::Locate(T x) { int count = 0; int s = first; while (count
bool staticLink
::Insert(int i, T x) { if (SList[avail].next==-1) { return false; } int s = first; int temp = avail; SList[temp].data = x; //空链头针后移 avail = SList[avail].next; int count=0; while (count < i-1 && count < length) { s = SList[s].next; count++; } SList[temp].next = SList[s].next; SList[s].next = temp; length++; return true; } template
bool staticLink
::Delete(int i) { if (i <= 0 || i > length) { return false; } int count = 0; int s = first; while (count < i-1 && count < length) { s = SList[s].next; count++; } int q = SList[s].next; SList[s].next = SList[q].next; SList[q].next = avail; avail = q; length--; return true; } template
void staticLink
::PrintList() { int s = SList[first].next; for (int i = 1; i <= length; i++) { cout << SList[s].data << " "; s = SList[s].next; } } 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 }; staticLink
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,查找第四个位置的成绩
输入3,查找成绩87所在的位置
输入4,在第四个位置插入成绩90
输入5,删除第八个位置的成绩