C++间接寻址实现


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

二. 实验内容  
建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。用间接寻址来实现,分别输出结果。 


现在用间接寻址方法实现

也是数组的一种,有点类似静态链表


//

//  main.cpp

//  间接寻址

扫描二维码关注公众号,回复: 4234269 查看本文章

//

//  Created by 梁华建 on 2017/10/26.

//  Copyright © 2017 梁华建. All rights reserved.

//


#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();

    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];

};

//无参构造

template<class DataType>

IndirectAddress<DataType>::IndirectAddress()

{

    first=new Node<DataType>;

    first->next=NULL;

}

//有参构造  尾插法

template<class DataType>

 IndirectAddress<DataType>::IndirectAddress(DataType a[], int n)

{

    if (n<=0&&n>MaxSize)throw "插入数据错误";

    Node<DataType> *r,*s;

    first=new Node<DataType>;//初始化头指针

    r=first;          //初始化

    for (int j=0; j<n; j++) {

        s=new Node<DataType>;

        s->data=a[j];    //s赋值

        r->next=s;       //把开头开头指针指向s

        r=s;

        address[j] = s;    //saddress

        length++;

    }

    r->next=NULL; //创建结束,把指针下一位置空

}


template<class DataType>

int IndirectAddress<DataType>::Length()

{

    return length;

}


template<class DataType>

DataType IndirectAddress<DataType>::Get(int i)

{

    return address[i]->data;

}


template <class DataType>

void IndirectAddress<DataType>::Insert(int i, DataType x)

{

    

    Node<DataType> *p = first;

    //先找到节点

    for (int j=1;j <= i - 1; j++)

    {

        p = p -> next;

    }

    Node<DataType> *TN ;

    TN = new Node<DataType>;

    TN -> data = x;

    TN -> next = p -> next;

    p -> next = TN;

    length++;

    // 顺序表的插入

    if (length >= MaxSize) {

        throw "溢出";

    }

    

    for (int b = length - 1; b > i - 1; b--) {

        address[b]=address[b-1];

    }

    address[i-1] =TN;

}


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> *TN;

    TN = new Node<DataType>;

    TN = p -> next;

    x = TN -> data;

    p -> next= TN -> next;

    delete TN;

    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++) {

        std::cout<<address[i]->data<<" ";

    }

    std::cout<<"\n";

}


template<typename DataType>

IndirectAddress<DataType>::~IndirectAddress()

{

    while (first != NULL)

    {

        Node<DataType> *q = first;

        first = first->next;

        delete q;

    }

    length = 0;

}

int main(int argc, const char * argv[]) {

    int a[5]={10,20,30,40,50};

    IndirectAddress<int> Student(a,5);

    cout<<"所有学生信息为";

    Student.PrintList();

    std::cout<<"输出第三位学生成绩"<<Student.Get(3)<<"\n";

    std::cout<<"插入成绩为70的学生在第三位";

    Student.Insert(3, 70);

    cout<<"所有学生信息为";

    Student.PrintList();

    std::cout<<"删除第三位学生 成绩为"<<Student.Delete(3);

    cout<<"所有学生信息为";

    Student.PrintList();

    cout<<"第五个学生的成绩"<<Student.Get(4)<<"\n";

    

    return 0;

}




实验总结:这次的间接寻址其实跟静态链表觉得差不多,也是以数组为存储模拟出指针的样子,这次实现起来还不错,不过代码写的时候还是要翻一下前面的内容模板,要多加练习以后。继续加油

猜你喜欢

转载自blog.csdn.net/MChuajian/article/details/78389314