数据结构实验二之单链表
来源:互联网 发布:java支付系统开发 编辑:程序博客网 时间:2024/06/06 00:26
1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。------用单链表来实现。
#include<iostream>using namespace std;struct Node{double data;Node*next;};class stu{private:Node*first;int count;public:stu(double a[], int n);~stu();void Insert(int i, double x);double Get(int i);int Locate(double x);double Delete(int i);};stu::stu(double a[], int n){int i;first = new Node; first->next = NULL;for (i = 0; i < n; i++){Node*s;s = new Node; s->data = a[i];s->next = first->next; first->next = s;}}stu::~stu(){while (first != NULL){Node*q;q = first;first = first->next;delete q;}}void stu::Insert(int i, double x){Node*p;p = first; count = 0;while (p != NULL&&count < i - 1){p = p->next;count++;}if (p == NULL) throw"位置";else{Node*s;s = new Node; s->data = x;s->next = p->next; p->next = s;}}double stu::Get(int i){Node*p;p = first->next; count = 1;while (p != NULL&&count < i){p = p->next;count++;}if (p == NULL) throw"位置";else return p->data;}int stu::Locate(double x){Node*p;p = first->next; count = 1;while (p != NULL){if (p->data == x) return 7-count;p = p->next;count++;}return 0;}double stu::Delete(int i){Node*p;p = first; count = 0;while (p != NULL&&count < i - 1){p = p->next;count++;}if (p == NULL || p->next == NULL) throw"位置";else{Node*q; double x;q = p->next; x = q->data; p->next = q->next;delete q;return x;}}int main(){double a[10] = { 91,92,93,94,95,96 };stu student(a, 6);student.Insert(2, 100);student.Delete(6);cout<<student.Get(2)<<endl;cout<<student.Locate(96)<<endl;}
阅读全文
0 0
- 数据结构实验二之单链表
- <数据结构> 实验二 线性表的实验之二
- 数据结构实验二(单链表)
- 数据结构实验之数组二:稀疏矩阵
- 数据结构实验之排序二:交换排序
- 数据结构实验之排序二:交换排序
- 数据结构实验之串二:字符串匹配
- 数据结构实验之排序二:交换排序
- 数据结构实验之数组二:稀疏矩阵
- 数据结构实验之串二:字符串匹配
- 数据结构实验之串二:字符串匹配
- 数据结构实验之串二:字符串匹配
- 数据结构实验之串二:字符串匹配
- 数据结构实验之串二:字符串匹配
- 数据结构实验之数组二:稀疏矩阵
- 数据结构实验之数组二:稀疏矩阵
- 数据结构实验之数组二:稀疏矩阵
- 数据结构实验之串二:字符串匹配
- 2017年10月8日训练笔记
- Hello World!
- 类图中的聚合关系(aggregation)和组合关系(composition)比较
- HashMap总结
- flex弹性布局
- 数据结构实验二之单链表
- Java虚拟机实现安全特性(二)
- String是最基本的数据类型吗?
- java实现二分查找-两种方式
- 数据结构实验之链表五:单链表的拆分
- vue.js开发外卖App项目的组件拆分总结(五)
- 第七章(字典)
- Python(4)list和tuple(类似js里的数组)
- JavaWeb路径问题汇总