基于类模板
#include<iostream>
using namespace std;
template<class T>
struct Node
{
T data; //数据域
Node<T> * next; //指针域
};
template <class T>
class LinkList {
Node<T> * 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 <class T>//无参函数
LinkList<T>::LinkList()
{
head = new Node<T>;
head->next = NULL;
}
template<class T>//有参函数
LinkList<T>::LinkList(T a[],int n){
head = new Node<T>;//生成头结点
Node<T>*rear = head;
for (int i = 0 ; i < n; i++) {
Node<T>*s = new Node<T>;
s->data = a[i];//指针s用于指向新节点
rear->next = s;
rear = s;
}
rear->next = NULL;
}
template<class T>//析构函数
LinkList<T>::~LinkList() {
Node<T>*p = head;
while (p) {
Node<T>* q = p;
p = p->next;
delete q;
}
head = NULL;
}
template<class T>//求单链表的长度
int LinkList<T>::ListLength()
{
int num = 0;
Node<T>*p;
p = head->next;
while (p) {
p = p->next;
num++;
}
return num;
}
template<class T>//按位查找
T LinkList<T>::Get(int pos){
Node<T>* 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<class T>//按值查找
int LinkList<T>::Locate(T item) {
Node<T>* p;
p = head->next;
int j = 1;
while (p && p->data != item) {
p = p->next;
j++;
}
if (p)
return j;
else
return 0;
}
template<class T>//遍历
void LinkList<T>::PrintLinkList() {
Node<T>* p;
p = head->next;
while (p) {
cout << p->data <<" ";
p = p->next;
}
cout << endl;
}
template<class T>//插入
void LinkList<T>::Insert(int i,T item) {
Node<T>* p = head;
int j = 0;
while (p&&j < i - 1) {
p = p->next;
j++;
}
if (!p) {
cerr << "插入位置非法";
exit(1);
}
else {
Node<T>* s = new Node<T>;
s->data = item;
s->next = p->next;
p->next = s;
}
}
template<class T>//删除
T LinkList<T>::Delete(int i) {
Node<T>* p = head;
int j = 0;
while (p && j < i - 1) {
p = p->next;
j++;
}
if (!p||!p->next) {
cerr << "删除位置非法";
exit(1);
}
else {
Node<T>* q;
T x;
q = p->next;
x = p->data;
p->next = q->next;
delete q;
return x;
}
}
template<class T>//菜单
void LinkList<T>::showMenu() {
cout << "-------------菜单------------" << endl;
cout << "| 1.插入 |" << endl;
cout << "| 2.删除 |" << endl;
cout << "| 3.按位查找 |" << endl;
cout << "| 4.按值查找 |" << endl;
cout << "| 5.求单链表的长度 |" << endl;
cout << "| 6.退出菜单 |" << endl;
cout << "-----------------------------" << endl;
}
template<class T>//控制菜单
void LinkList<T>::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<int> s1(a,4);
cout << "新建数值:";
s1.PrintLinkList();
s1.showMenu();
s1.ProcessMenu();
s1.~LinkList();
return 0;
}