间接寻址的基本及其应用(实验2.4)

来源:互联网 发布:淘宝客单页模板 编辑:程序博客网 时间:2024/05/20 09:05

一.实验目的
巩固间接寻址的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。

二. 实验内容
建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。用间接寻址来实现,分别输出结果。
为了以后的对代码的修改、优化和复用,这里采用了C++中的模板来实现,下面是模板的实现。

IndirectAddress.h

#ifndef IndirectAddress_h#define IndirectAddress_h#include <iostream>using namespace std;const int MaxSize = 100;template<class DataType>struct Node {    DataType data;    Node<DataType> *next;};template<class DataType>class IndirectAddress {public:    // 无参构造函数,建立只有头结点的空链表    IndirectAddress();    // 有参构造函数,建立有n个元素的单链表    IndirectAddress(DataType a[], int n);    // 析构函数    ~IndirectAddress();    // 求单链表长度    int Length();    // 按位查找    DataType Get(int i);    // 按值查找    DataType Locate(DataType x);    // 插入操作    void Insert(int i, DataType x);    // 删除操作    DataType Delete(int i);    // 遍历操作    void PrintList();private:    // 头指针    Node<DataType> *first;    // 节点数量    int length = 0;    // 节点指针数组    Node<DataType> *address[MaxSize];};#endif /* IndirectAddress_h */

IndirectAddress.cpp

#include "IndirectAddress.h"template<class DataType>IndirectAddress<DataType>::IndirectAddress(){    first = new Node<DataType>;    first->next = NULL;}template<class DataType>// 尾插法IndirectAddress<DataType>::IndirectAddress(DataType a[], int n){    Node<DataType> *r, *s;    first = new Node<DataType>;    // 生成头结点    r = first;                     // 尾指针初始化    for (int i = 0; i < n; i++)    {        s = new Node<DataType>;        s -> data = a[i];          // 为每个数组元素建立一个结点        r -> next = s;        r = s;                     // 将结点s插入到终端结点之后        address[i] = s;            // 将指针保存在数组中        length++;    }    r -> next = NULL;              // 单链表建立完毕,将终端结点的指针域置空}template<class DataType>int IndirectAddress<DataType>::Length(){    return length;}template<class DataType>DataType IndirectAddress<DataType>::Get(int i){    if (i >= length) {        throw("位置错误");    }    return address[i - 1] -> data;}template<class DataType>DataType IndirectAddress<DataType>::Locate(DataType x){    Node<DataType> *p;    p = first -> next;    int count = 1;    while (p != NULL)    {        if (p -> data == x)        {            return count;        }        p = p -> next;        count++;    }    return -1;}template <class DataType>void IndirectAddress<DataType>::Insert(int i, DataType x){    // 单链表的插入    Node<DataType> *p = first;    for (int k = 1; k <= i - 1; k++)    {        p = p -> next;    }    Node<DataType> *tempNode ;    tempNode = new Node<DataType>;    tempNode -> data = x;    tempNode -> next = p -> next;    p -> next = tempNode;    length++;    // 顺序表的插入    if (length >= MaxSize) {        throw "溢出";    }    for (int j = length - 1; j > i - 1; j--) {        address[j] = address[j - 1];    }    address[i - 1] = tempNode;}template<class DataType>DataType IndirectAddress<DataType>::Delete(int i){    DataType x;    // 单链表操作    Node<DataType> *p;    p = first;    for (int k = 1; k < i; k++)    {        p = p -> next;    }    Node<DataType> *tempNode;    tempNode = new Node<DataType>;    tempNode = p -> next;    x = tempNode -> data;    p -> next= tempNode -> next;    delete tempNode;    length--;    // 顺序表操作    address[i - 1] = NULL;    for (int j = i - 1; j <= length; j++) {        address[j] = address[j + 1];    }    return x;}template<class DataType>void IndirectAddress<DataType>::PrintList(){    for (int i = 0; i < Length(); i++) {        cout << address[i] -> data << " ";    }    cout << endl;}template<typename DataType>IndirectAddress<DataType>::~IndirectAddress(){    while (first != NULL)    {        Node<DataType> *q = first;        first = first->next;        delete q;            }    length = 0;}

间接寻址模板借鉴了前面单链表模板和顺序表模板,将二者合在了一起。接下来就是测试模板,这里依然是用实验题目的成绩表基本操作。

main.cpp

#include <iostream>#include "IndirectAddress.cpp"int main(int argc, const char * argv[]) {    float score[5] = {66.5, 81.5, 80, 95.5, 100};    IndirectAddress<float> list(score, 5);    cout << "遍历学生分数" << endl;    list.PrintList();    cout << "读取第三个学生的分数" << endl;    cout << list.Get(3) << endl;    cout << "在第二个位置插入学生分数 95" << endl;    list.Insert(2, 95);    list.PrintList();    cout << "80分在分数表中的位置为: " << list.Locate(80) << endl;    cout << "该分数表长度为: " << list.Length() << endl;    cout << "删除第3个分数" << endl;    list.Delete(3);    list.PrintList();    return 0;}

运行结果如下:

控制台输出

三. 总结和心得
在写了链表和顺序表的模板后,写间接寻址的模板比前面轻松了许多,主要是将两种模板的有点结合在一起。在按位查找时省力很多,但是牺牲了插入和删除这两个小功能的性能,代码也相对比较多。
顺序表的实现这里就不重复了,在上一次实验中我写了顺序表的模板,这个实验题目只需要使用上次的模板,修改一下数据即可。用C++来实现线性表这五种方法,主要是写模板,模板写好了之后,在以后的复用中就十分方便。
在编码的时候在适合的地方还是需要适当加点注释,时间过久了,阅读起来还是有点吃力的。毕竟现在代码并不难写,在平时敲代码时候,应该是在保证代码的阅读顺带实现一下功能,这样在以后的维护和优化中起到很好的帮助。

简书传送门:http://www.jianshu.com/u/1bed5943be92

原创粉丝点击