数据结构实验2.1(单链表)

来源:互联网 发布:西安行知中学初中部 编辑:程序博客网 时间:2024/06/13 09:12
一.实验目的
     巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
 
二..实验内容
  建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
(用单链表来实现)。

三..源代码
#include<iostream>using namespace std;template<class T>struct Node{T data;Node<T> *next;};template<class T>class LinkList{public:LinkList(); //无参构造函数,建立只有头结点的空链表LinkList(T a[], int n); //有参构造函数,建立有n个元素的单链表~LinkList() {}; //析构函数int Length(); //求单链表长度T Get(int i); //按位查找int Locate(T x); //按值查找void Insert(int i, T x); //插入操作T Delete(int i); //删除操作void PrintList(); //遍历操作private:Node<T> *first; //头指针};template<class T>void LinkList<T>::PrintList(){Node<T> *p = NULL;p = first->next;while (p != NULL){cout << p->data <<endl;p = p->next;}}template<class T>int LinkList<T>::Length(){Node<T> *p = NULL;p = first->next;int count = 0;while (p != NULL){p = p->next;count++;}return count;}template<class T>LinkList<T>::LinkList(){first = new Node<T>;first->next = NULL;}template<class T>LinkList<T>::LinkList(T a[], int n){Node<T> *r, *s;first = new Node<T>;                    //生成头结点  r = first;                            //尾指针初始化  for (int i = 0; i < n; i++){s = new Node<T>; s->data = a[i];        //为每个数组元素建立一个结点  r->next = s; r = s;                 //将结点s插入到终端结点之后  }r->next = NULL;        //单链表建立完毕,将终端结点的指针域置空  }template<class T>T LinkList<T>::Get(int i){Node<T> *p=first->next;for (int count = 1; count < i; count++){count++;p = p->next;}if (p == NULL)throw("异常");elsereturn p->data;}template<class T>int LinkList<T>::Locate(T x){Node<T> *p;p = first->next;int count = 1;while (p != NULL){if (p->data == x){return count;}p = p->next;count++;}return 0;}template <class T>void LinkList<T>::Insert(int i, T x){Node<T> *p = first;for (int k = 1; k <= i - 1; k++){p = p->next;}Node<T> *tempNode ;tempNode = new Node<T>;tempNode->data = x;tempNode->next = p->next;p->next = tempNode;}template<class T>T LinkList<T>::Delete(int i){int x;Node<T> *p;p = first;for (int k = 1; k < i; k++){p = p->next;}Node<T> *tempNode;tempNode = new Node<T>;tempNode = p->next;x = tempNode->data;p->next= tempNode->next;delete tempNode;return x;}

#include"LinkList.h"#include<iostream>using namespace std;int main(void){int Stu_score[5] = { 99,98,97,96,95 };LinkList<int> List1(Stu_score, 5);List1.PrintList();  //遍历学生分数cout << "------------" << endl;cout<<List1.Get(2)<<endl; //取第二个位置的学生分数cout << "------------" << endl;List1.Insert(3, 100); //在第三个位置插入学生分数cout << "------------" << endl;List1.PrintList();  //再次遍历cout << "------------" << endl;cout << List1.Length() << endl; //获取分数表长度cout << "------------" << endl;cout << List1.Locate(96) << endl;  //获取分数为96的位置cout << "------------" << endl;cout<<List1.Delete(1)<<endl; //删除第一个分数cout << "------------" << endl;List1.PrintList(); //再次遍历cout << "------------" << endl;system("pause");return 0;}


四.实验截图.

如截图所示:第一次遍历为:99 98 97 96 95
取第二个位置的学生分数为:98
在第三个位置插入学生分数,再次遍历为:99 98 100 97 96 95
此时分数表的长度为:6
获取分数为96的位置为:5
删除第一个分数并输出为:99
再次遍历为:98 100 97 96 95
实验结果正确。

五..实验心得
通过这次实验,我基本掌握了单链表的操作,实现对数据的增删查输出等操作,尽管这敲代码时出现了不少错误,但还是通过网上搜索找到了答案,也希望以后自己多敲码,更加熟练,做得更好。

原创粉丝点击