实验二:线性表实验
来源:互联网 发布:单片机ds18b20 编辑:程序博客网 时间:2024/06/05 15:22
.建立一个N个学生成绩的顺序表,对表进行插入、删除、查找等操作。分别输出结果。要求如下:
1)用顺序表来实现。
2)用单链表来实现。
(1)顺序表
①头文件SeqList建立
#ifndef SeqList_H#define SeqList_Hconst int Maxsize = 60;class SeqList{public:SeqList(){length = 0;}SeqList(int a[],int n); ~SeqList(){ }int Length(){return length;}int Get(int i);int Locate(int x);void Insert(int i,int x);Delete(int i);void PrintList();private:int data[Maxsize];int length;int i,j,x;};#endif
②源程序文件SeqList.cpp建立
#include <iostream>using namespace std;#include "SeqList.h"SeqList::SeqList(int a[],int n){if(n > Maxsize) throw"参数非法";for(i = 0;i < n;i++)data[i] = a[i];length = n;}int SeqList::Get(int i){if(i < 1 && i > length) throw"查找位置非法";else return data[i - 1];}int SeqList::Locate(int x){for(i = 0; i < length;i++)if(data[i] == x)return i + 1;return 0;}void SeqList::Insert(int i,int x){if(length >= Maxsize)throw"上溢";if(i < 1||i > length + 1)throw"位置";for(j = length;j >= i;j--)data[j] = data[j - 1];}int SeqList::Delete(int i){if(length == 0)throw"下溢";if(i < 1||i > length)throw"位置";x = data[i - 1];for(j = i;j < length;j++)data[j - 1] = data[j];length--;return x;}void SeqList::PrintList(){for(i = 0;i < length;i++)cout<<data[i]<<" ";cout<<endl;}
③源程序文件SeqList_main.cpp的建立
#include <iostream>using namespace std;#include "SeqList.h"void main(){int i,Grade;int GradeList[10] = {89,78,98,78,67,83,98,67,87,90};SeqList L(GradeList,10);cout<<"学生成绩如下:"<<endl;L.PrintList();try{L.Insert(8,76);}catch(char *s){cout<<s<<endl;}cout<<"插入操作后的学生成绩为:"<<endl;L.PrintList();cout<<"值为98的元素为:";cout<<L.Locate(98)<<endl;cout<<"执行删除第9位学生的成绩,删除前数据为:"<<endl;L.PrintList();try{L.Delete(9);}catch (char *s){cout<<s<<endl;}cout<<"删除后数据为:"<<endl;L.PrintList();}
运行结果
可见程序运行存在缺陷,如值为98的元素只能找到位置3而不能找到位置7,还有插入操作后最后一个数据丢失的问题。
(2)单链表
①头文件LinkList.h的建立
#ifndef LinkList_H#define LinkList_Htemplate <class DT>struct Node{DT data;Node<DT> *next;};template <class DT>class LinkList{public:LinkList();LinkList(DT a[],int n);~LinkList();int Locate(DT x);void Insert(int i,DT x);DT Delete(int i);void PrintList();private:Node<DT> *first;};#endif
②源程序文件LinkList.cpp
#include<iostream>using namespace std;#include "LinkList.h"template <class DT>LinkList<DT>::LinkList(){first = new Node<DT>;first -> next = NULL;}template <class DT>LinkList<DT>::LinkList(DT a[],int n){Node<DT> *r,*s;first = new Node<DT>;r = first;for(int i = 0;i < n;i++){s = new Node<DT>;s -> data = a[i];r -> next = s;r = s;}r -> next = NULL;}template <class DT>LinkList<DT>::~LinkList(){Node<DT> *q = NULL;while (first != NULL){q = first;first = first -> next;delete q;}}template <class DT>void LinkList<DT>::Insert(int i, DT x){Node<DT> *p = first,*s = NULL;int count = 0;while(p != NULL && count < i - 1){p = p -> next;count++;}if(p == NULL)throw "位置";else{s = new Node<DT>;s -> data = x;s -> next = p -> next;p -> next = s;}}template <class DT>DT LinkList<DT>::Delete(int i){Node<DT> *p = first, *q = NULL;DT x;int count = 0;while(p != NULL && count < i - 1){p = p -> next;count++;}if(p == NULL||p -> next == NULL)throw"位置";else{q = p -> next; x = q -> data;p -> next = q -> next;delete q;return x;}}template <class DT>int LinkList<DT>::Locate(DT x){Node<DT> *p = first -> next;int count = 1;while (p != NULL){if(p -> data == x)return count;p = p -> next;count++;}return 0;}template <class DT>void LinkList<DT>::PrintList(){Node<DT> *p = first -> next;while(p != NULL){cout<<p -> data<<" ";p = p -> next;}cout<<endl;}
③源程序文件LinkList_main.cpp的建立
#include <iostream>using namespace std;#include "LinkList.cpp"void main(){int GradeList[20] = {71,78,98,78,67,76,97,78,67,90};LinkList<int> L(GradeList,10);cout<<"学生成绩为:"<<endl;L.PrintList();try{L.Insert(9,80);}catch(char *s){cout<<s<<endl;}cout<<"执行插入操作后的学生成绩为:"<<endl;L.PrintList();cout<<"值为78的成绩位置为:";cout<<L.Locate(78)<<endl;cout<<"执行删除操作前的学生成绩为:"<<endl;L.PrintList();try{L.Delete(1);}catch(char *s){cout<<s<<endl;}cout<<"执行删除操作后学生成绩为:"<<endl;L.PrintList();}
运行结果
这次插入和删除操作没有问题,但是值为78的成绩依然只能找到一个而不能找到第二个,望老师解答。
0 0
- 实验二:线性表实验
- 实验二:线性表实验
- 实验二:线性表实验
- 实验二线性表实验
- 实验二 线性表实验
- 实验二、 线性表实验
- 《数据结构》实验二:线性表的实验
- 《数据结构》实验二:线性表实验
- 《数据结构》实验二: 线性表实验
- 《数据结构》实验二 线性表的实验
- 实验二:线性表的实验
- 《数据结构》实验二:线性表实验
- 《数据结构》实验二:线性表的实验
- 实验二:线性表实验(1)
- 数据结构实验二 :线性表的实验
- 《数据结构》实验二 线性表的实验
- 《数据结构》实验二:线性表实验(1)
- 数据结构实验二 线性表实验
- mongodb 批量更新 操作文档的数组键
- vijos P1071
- 添加远程库
- xelatex乱码问题解决
- CURL模拟登陆
- 实验二:线性表实验
- 程序设计作业报告项目六——本月有几天
- struts域驱动模式的实现
- BZOJ 3207 花神的嘲讽计划I Hash+可持久化线段树/划分树
- 安装和配置fedora19要做的那些事,超值经验
- 递归实现二叉树遍历JAVA
- 使用sysbench对Oracle做基准测试(三) THREADS
- wamp出现You don’t have permission to access/on this server提示
- zoj2610 Puzzle 数组转置,...