实验二线性表的综合实验5
来源:互联网 发布:go并发编程实战完整版 编辑:程序博客网 时间:2024/06/06 02:34
一.实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二..实验内容
1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
要求如下:
2)用单链表来实现。
三、源代码
#include<iostream>using namespace std;template< typename T>class Node {public: T data; Node *next; };const int MaxSize = 100; template<typename T>class LinkList{public: LinkList(T a[], int n); ~LinkList(); int Length(); T Get(int i); int Locate(T x); bool Insert(int i, T x); bool Delete(int i); bool InsertHead(T x); bool InsertTail(T x); void ListTraverse(); bool changeList(int i, T x); private: Node<T> *first; int m_Length; Node<T> *address[MaxSize]; };template<typename T>LinkList<T>::LinkList(T a[], int n) { if (n > MaxSize) { throw string("数组长度长于链长度,录入失败"); exit(1); } first = new Node<T>; first->next = NULL; for (int i = 0; i<n; i++) { Node<T> *s = new Node<T>; s->data = a[i]; s->next = first->next; first->next = s; } m_Length = n; Node<T> *p =first; for (int j = 0; j < m_Length;j++) { p = p->next; address[j] = p; } for (int i = 0; i < n; i++) { InsertHead(a[i]); }}template<typename T>LinkList::LinkList(T a[], int n) { Node<T> *first = new NOde<T>; Node<T> *r = first; for (int i = 0; i < n; i++) { s = new Node; s->data = a[i]; r->next = s; r = s; } r->next = NULL; }template<typename T>bool LinkList<T>::InsertHead(T x) { if (m_Length>=MaxSize) { throw string("链满,无法插入数据"); } Node<T> *Temp = first->next; Node<T> *s = new Node<T>; if (s == NULL) { return false; } s->data = x; s->next = Temp; first->next = s; m_Length++; for (int i = m_Length-1; i > 0;i--) { address[i] = address[i - 1]; } address[0] = first->next; return true; }template<typename T>bool LinkList<T>::InsertTail(T x) { if (m_Length >= MaxSize) { throw string("链满,无法插入数据"); } Node<T> *p = first; Node<T> *s = new Node<T>; if (s == NULL) { return false; } s->data = x; while (p->next != NULL) { p = p->next; } p->next = s; s->next = NULL; m_Length++; address[m_Length - 1] = s; return true; }template<typename T>LinkList<T>::~LinkList() { while (first != NULL) { Node<T> *q = first; first = first->next; delete q; } m_Length = 0;}template<typename T>int LinkList<T>::Length() { int num=0; Node<T> *p = first->next; while (p!= NULL) { p = p->next; num++; } return m_Length; }template<typename T>T LinkList<T>::Get(int i) { if (i > m_Length) { throw string("位置大于当前链长度"); } else if(i<=0){ throw string("位置小于等于0,无效!"); } Node<T> *p = address[i - 1]; return p->data; Node<T> *p = first->next; int count = 1; while (p != NULL&&count < i) { p = p->next; count++; } if (p == NULL) { throw"位置"; } else { return p->data; }}template<typename T>int LinkList<T>::Locate(T x) { Node<T> *p = first->next; int count = 1; while (p != NULL) { if (p->data == x) { return count; } p = p->next; count++; } return -1; }template<typename T>bool LinkList<T>::Insert(int i, T x) { if (i>m_Length) { throw string("位置大于当前链表长度!"); } else if (i<=0) { throw string("位置小于等于0,无效!"); } else if (m_Length>=MaxSize) { throw string("链满"); } Node<T> *p = first; int count = 0; int num = i - 1; while (p != NULL&&count <num) { p = p->next; count++; } if (p == NULL) { return false; } else { Node<T> *s = new Node<T>; s->data = x; s->next = p->next; p->next = s; m_Length++; for (int j = m_Length-1; j >= i; j--) { address[j] = address[j - 1]; } address[i] = s; return true; }}template<typename T>void LinkList<T>::ListTraverse(){ Node<T> *p = first->next; while (p != NULL) { cout << p->data << ","; p = p->next; }}template<typename T>bool LinkList<T>::Delete(int i){ if (i>m_Length||i<=0) { throw string("位置无效!"); } Node<T> *p = first; int count = 0; while (p != NULL&&count < i - 1) { p = p->next; count++; } if (p == NULL) { return false; } else { Node<T> *q; q = p->next; p->next = q->next; delete q; m_Length--; for (int j = i; j <= m_Length; j++) { address[j-1] = address[j]; } return true; }}template<typename T>bool LinkList<T>::changeList(int i,T x) { if (i>m_Length||i<=0) { throw string("位置有误"); } Node<T> *p = address[i - 1]; p->data = x; return true;}int main() { string a[5] = { "plus","plus","c","world","hello" }; try { LinkList<string> MyList(a, 5); string L = MyList.Get(1); cout << "第一个节点位置的元素为:" << L << endl; cout <<"当前链长度为:"<< MyList.Length() << endl; int Lo = MyList.Locate("hello"); if (Lo == -1) { cout << "未找到" << endl; } else { cout << "元素hello所在的位置为:" << MyList.Locate("hello") << endl; } MyList.ListTraverse(); cout << endl; cout << "尾插法插入元素(插在最后):"; MyList.InsertTail("where"); MyList.ListTraverse(); cout << endl; cout << "头插法插入元素,插在第一个:"; MyList.InsertHead("areyou"); MyList.ListTraverse(); cout << endl; cout << "插入元素到指定位置:"; MyList.Insert(3, "thereismiddle"); MyList.ListTraverse(); cout << endl; cout << "删除指定位置的元素:"; MyList.Delete(3); MyList.ListTraverse(); cout << endl; cout << "改变指定位置元素的值:"; MyList.changeList(1, "ppp"); MyList.ListTraverse(); } catch (string& aval) { cout << aval << endl; } return 0;}四、实验结果
阅读全文
0 0
- 实验二线性表的综合实验5
- 《数据结构》实验二 线性表综合实验
- 实验二线性表综合实验
- 实验二 线性表综合实验(1)
- 实验二 线性表综合实验
- 实验二线性表综合实验3
- 实验二线性表综合实验2
- 实验二线性表综合实验-单链表
- 实验二线性表综合实验-双链表
- 实验二线性表综合实验4
- 实验二 线性表的综合实验(c++)
- 实验二 线性表的综合实验(c++)
- 实验二 线性表的综合实验(c++)
- 实验二 线性表综合实验之顺序表
- 实验二 线性表综合实验之静态链表
- 实验二 线性表综合实验(循环链表)
- 实验二 线性表综合实验(静态链表)
- 数据结构:实验二线性表综合实验①顺序表
- logistic回归算法详细分析与Python代码实现注释
- codeforces 863B
- Oc 正则表达式~详解 02 (方法名总结)
- JVM原理之内存分配参数
- SDIO接口WiFi驱动浅析
- 实验二线性表的综合实验5
- LiteOS之任务管理
- bzoj1426 [收集邮票] 期望概率DP进阶
- 在 SSM 框架整合 Shiro 的相关案例
- poj2229动态规划
- datagird实现混有数字的字符串排序
- 10月30日(第1天_字符串练习)
- mysql密码的重置,数据库的备份和还原;
- HTML、CSS学习-标签(一)