实验3:间接寻址

一、实验目的

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

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

三、实验步骤

1、依据实验内容分别说明实验程序中用到的数据类型的定义:

InAdd();//无参构造函数 

        InAdd(DataType score[],int n);//有参构造函数 

        ~InAdd();//析构函数 

        void Print();//遍历操作 

        DataType get(int i);//按位查找操作 

        int Locate(DataType x);//按值查找操作 

        void insert(int i,DataTypex);//插入操作 

        DataType Delete(int i);//删除操作

2、相关操作的算法表达

定义间接寻址类的数据类型,包括插入、删除、查找、输出等基本操作。

源程序如下

#ifndef InAdd_H

#define InAdd_H

const int MaxSize=10;   

template<class DataType> 

struct Node

    DataType data; 

    Node<DataType> *next; 

}; 

 

template<class DataType> 

class InAdd{ 

    public: 

        InAdd();//无参构造函数 

        InAdd(DataType score[],intn);//有参构造函数 

        ~InAdd();//析构函数 

        void Print();//遍历操作 

        DataType get(int i);//按位查找操作 

        int Locate(DataType x);//按值查找操作 

        void Insert(int i,DataTypex);//插入操作 

        DataType Delete(int i);//删除操作 

    private: 

        Node<DataType> *first;//头指针 

        int length; //结点数量 

        Node<DataType>*address[MaxSize];  //结点指针数组 

};

#endif 

#include<iostream>

#include "InAdd.h"

using namespace std;

template<class DataType> 

InAdd<DataType>::InAdd() 

    first=newNode<DataType>; 

    first->next=NULL; 

 

template<class DataType>  

InAdd<DataType>::InAdd(DataType score[],int n) 

    if (n > MaxSize) throw("溢出"); 

    Node<DataType> *s; 

    first = newNode<DataType>;

    first->next=NULL; //初始化一个空链表   

    for(int i=n-1;i>=0;i--)   

    {   

        s=new Node<DataType>;

        s->data=score[i];  //为每个数组元素建立一个结点   

        s->next=first->next;

        first->next=s;  //将结点s插入头结点之后   

    } 

 

template<class DataType>

InAdd<DataType>::~InAdd()                 //析构函数   

    Node<DataType> *q; 

    while(first!=NULL) 

    { 

        q=first; 

        first=first->next; 

        delete q; 

    } 

}   

 

template<class DataType>    

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

{   

    Node<DataType>*p,*s;

    int count;   

    p=first;

    count=0;   

   while(p!=NULL&&count<i-1)   

   {   

        p=p->next;   

        count++;   

    }   

    if(p==NULL)throw"位置非法"; 

    s=new Node<DataType>;

    s->data=x; 

    s->next=p->next; 

    p->next=s; 

    length++; 

 

template<class DataType>     

DataType InAdd<DataType>::Delete(int i) 

    Node<DataType> *q,*p;

    DataType x;

    int count;   

    p=first;

    count=0; //注意P指针要指向头结点   

   while(p!=NULL&&count<i-1)  //此操作目的是找到i-1个结点 

    { 

        p=p->next; 

        count++;   

    } 

   if(p==NULL||p->next==NULL)throw"位置";  //结点p不存在或p后继结点不存在 

    else

    { 

        q=p->next;

        x=q->data;  //暂存被删结点   

        p->next=q->next;   

        delete q;   

        return x; 

    } 

}   

   

template<class DataType>  

DataType InAdd<DataType>::get(int i)   

    Node<DataType>*p;

    int count; 

    p=first->next;count=1; 

   while(p!=NULL&&count<i) 

    {p=p->next;count++;} 

    if(p==NULL)throw"位置非法"; 

    else

        return p->data; 

}   

   

template<class DataType>    

int InAdd<DataType>::Locate(DataType x)   

        {   

            Node<DataType>*p;

            int count =1;   

            p=first->next;   

            while(p!=NULL)   

            {   

                if(p->data==x)

                   return count;   

                p=p->next;   

                count++;   

            }   

            return 0;   

        }   

   

template<class DataType>     

void InAdd<DataType>::Print()   

        {   

           Node<DataType>*p;   

            p=first->next;   

            while(p!=NULL)   

           {cout<<p->data<<" ";;   

            p=p->next;   

            }   

        } 

 

#include<iostream>

using namespace std;

#include "InAdd.cpp"

int main()

{

 int score[5] ={90,88,75,68,73}; 

    InAdd<int>student(score,5); 

    cout << "学  生  成  绩"<<endl; 

    student.Print();

    cout<<endl;

    cout<< "在位置2插入成绩81";

    student.Insert(2,81); 

    cout<<endl;

    cout<<"插入操作后学生成绩为:"<<endl; 

    student.Print();

    cout<<endl;

    cout << "位置4的成绩为:" << student.get(4) << endl; 

    cout << "成绩90所在位置为:" << student.Locate(90) << endl;

    cout<< "删除位置3的学生成绩" <<endl;

    student.Delete(3);

    cout<< "执行删除操作后的学生成绩为:"<< endl;

    student.Print();

    cout<<endl;

    return 0;

}

运行结果如图


猜你喜欢

转载自blog.csdn.net/Ulong_/article/details/80220033