基于类模板
#includeusing namespace std; template struct Node { T data; //数据域 Node * next; //指针域 }; template class linkList { Node * head; public: linkList();//无参函数 linkList(T a[],int n);//有参函数 ~linkList();//析构函数 int ListLength();//求单链表的长度 T Get(int pos);//按位查找 int Locate(T item);//按值查找 void PrintlinkList();//遍历 void Insert(int n, T item);//插入 T Delete(int i);//删除 void showMenu();//菜单 void ProcessMenu();//控制菜单 };
template//无参函数 linkList ::linkList() { head = new Node ; head->next = NULL; }
template//有参函数 linkList ::linkList(T a[],int n){ head = new Node ;//生成头结点 Node *rear = head; for (int i = 0 ; i < n; i++) { Node *s = new Node ; s->data = a[i];//指针s用于指向新节点 rear->next = s; rear = s; } rear->next = NULL; }
template//析构函数 linkList ::~linkList() { Node *p = head; while (p) { Node * q = p; p = p->next; delete q; } head = NULL; }
template//求单链表的长度 int linkList ::ListLength() { int num = 0; Node *p; p = head->next; while (p) { p = p->next; num++; } return num; }
template//按位查找 T linkList ::Get(int pos){ Node * p = head->next; int j = 1; while (p&&j < pos) { p = p->next; j++; } if (!p || j > pos) { cerr << "查找位置非法"; exit(1); } else return p->data; }
template//按值查找 int linkList ::Locate(T item) { Node * p; p = head->next; int j = 1; while (p && p->data != item) { p = p->next; j++; } if (p) return j; else return 0; }
template//遍历 void linkList ::PrintlinkList() { Node * p; p = head->next; while (p) { cout << p->data <<" "; p = p->next; } cout << endl; }
template//插入 void linkList ::Insert(int i,T item) { Node * p = head; int j = 0; while (p&&j < i - 1) { p = p->next; j++; } if (!p) { cerr << "插入位置非法"; exit(1); } else { Node * s = new Node ; s->data = item; s->next = p->next; p->next = s; } }
template//删除 T linkList ::Delete(int i) { Node * p = head; int j = 0; while (p && j < i - 1) { p = p->next; j++; } if (!p||!p->next) { cerr << "删除位置非法"; exit(1); } else { Node * q; T x; q = p->next; x = p->data; p->next = q->next; delete q; return x; } }
template//菜单 void linkList ::showMenu() { cout << "-------------菜单------------" << endl; cout << "| 1.插入 |" << endl; cout << "| 2.删除 |" << endl; cout << "| 3.按位查找 |" << endl; cout << "| 4.按值查找 |" << endl; cout << "| 5.求单链表的长度 |" << endl; cout << "| 6.退出菜单 |" << endl; cout << "-----------------------------" << endl; }
template//控制菜单 void linkList ::ProcessMenu() { while (1) { cout << endl; cout << "请输入你选择的菜单功能:"; int menuchioce; cin >> menuchioce; switch (menuchioce) { case 1: int insertNum; T insertValue; cout << "输入要插入的位置:"; cin >> insertNum; cout << "输入插入的值:"; cin >> insertValue; Insert(insertNum, insertValue); cout << "插入后的值:"; PrintlinkList(); break; case 2: int deletevalue; cout << "输入要删除的数值所在序号:"; cin >> deletevalue; Delete(deletevalue); cout << "删除后的数值:"; PrintlinkList(); break; case 3: int getValue; cout << "输入要查找元素的序号:"; cin >> getValue; cout << "该序号对应的数值:" << Get(getValue) << endl;; break; case 4: cout << "输入要查找的数值:"; T locatevalue; cin >> locatevalue; cout << "该元素的序号:" << Locate(locatevalue) << endl; break; case 5: cout << "线性表的长度:" << ListLength() << endl; break; case 6: cout << "成功退出菜单"; exit(0);//正常运行导致退出程序 break; } } }
主函数:
int main() {
int a[4] = { 7,3,6,8 };
linkList s1(a,4);
cout << "新建数值:";
s1.PrintlinkList();
s1.showMenu();
s1.ProcessMenu();
s1.~linkList();
return 0;
}



