[数据结构]数据结构——链表法

来源:互联网 发布:网络的3类地址 编辑:程序博客网 时间:2024/05/16 12:57

本博文系GDOU信管第一节实验的实验报告

一、链结的定义

template<typename T>struct Node{T data;Node<T> *next;};
此处定义了结构体Node,里面分为数据部分和指针部分

二、模板类定义

template<typename T>class LinkList {public:LinkList();//无参LinkList(T a[], int n);//含参构造,部署a[],创建n个链结~LinkList();int Length();//显示链表长度T Get(int i);//按位查找,找到第i位并且输出;int Locate(T x);//按值查找,找到匹配的值输出位置与值void insert(int i, T x);//插入;在第i位插入xT Delete(int i);//删除;删除第i个链结void PrintList();private:Node<T> *first;};


三、具体模板类代码的实现

template <typename T>LinkList<T>::LinkList() {first = new Node<T>;first->next = NULL;}template <typename T>LinkList<T>::LinkList(T a[], int n) {first = new Node<T>;first->next = NULL;Node<T> *p;Node<T> *q = first;for (int i = 0; i < n; i++) {//Node p = new Node; p->data = a[i];//p->next = first->next;//first->next=p;p = new Node<T>; p->data = a[i];q->next = p; q = p;}p->next = NULL;}template<typename T>LinkList<T>::~LinkList() {Node<T>* p = first;first = first->next;delete p;}template<typename T>int LinkList<T>::Length() {int count = 0; Node<T> *p = first->next;while (p != NULL) {p = p->next;count++;}return count;}template<typename T>T LinkList<T>::Get(int i) {Node<T> *p = first->next; int count = 1;while (p != NULL&&count < i) {p = p->next;count++;}if (p == NULL)throw "locate";else return p->data;}template <typename T>int LinkList<T>::Locate(T x) {Node<T> *p = first; int count = 0;while (p != NULL) {if (p->data == x)return count;p = p->next;count++;}if (p == NULL)throw "locate";else return count;}template<typename T>void LinkList<T>::insert(int i, T x) {Node<int>* p = first; int count = 0;while (p != NULL&&count < i - 1) {p = p->next;count++;}if (p == NULL)throw"locate";else {Node<int>* q = new Node<T>;q->data = x;q->next = p->next;p->next = q;}}template<typename T>T LinkList<T>::Delete(int i) {Node<T>* p = first; int count = 0;int x = 0;while (p != NULL&&count < i - 1) {p = p->next;count++;}if (p == NULL) throw"locate";else {Node<int>* q = p->next;x = q->data;p->next = q->next;delete q;cout << "删除成功";return x;}}template<typename T>void LinkList<T>::PrintList() {Node<int>* p = first->next;if (p == NULL)throw"locate";else {while (p != NULL) {cout << p->data << endl;////////////////??????p = p->next;}}}

四、主函数与简单界面

void menu() {cout << "1.构建头函数\n";cout << "2.建立含有n个元素的链表\n";cout << "3.析构链表\n";cout << "4.显示链表长度\n";cout << "5.按位查找\n";cout << "6.按值查找\n";cout << "7.插入\n";cout << "8.删除\n";cout << "9.输出\n";cout << "请输入序号\n";}int main() {LinkList<int> x;int a;menu();int i;do {cin >> a;switch (a){case 1:x.LinkList<int>::LinkList();break;case 2:int a[10];cout << "输入十个整形\n";for (int i = 0; i < 10; i++) {cin >> a[i];}x.LinkList<int>::LinkList(a, 10);break;case 3:cout << "析构\n";x.LinkList<int>::~LinkList();break;case 4:cout << "输出长度\n";cout << x.LinkList<int>::Length();break;case 5:cout << "按位查找,输入第几位\n";cin >> i;cout << x.LinkList<int>::Get(i);break;case 6:cout << "按值查找,输入值\n";cin >> i;cout << x.LinkList<int>::Locate(i);break;case 7:int h;cout << "insert";cout << "插入到第几个\n";cin >> i;cout << "插入的是\n";cin >> h;x.LinkList<int>::insert(i, h);break;case 8:cout << "删除第几个\n";cin >> i;cout << x.LinkList<int>::Delete(i);break;case 9:x.LinkList<int>::PrintList();break;default:break;}} while (a != 0);}



原创粉丝点击