資料結構實驗三間接定址學生資訊
阿新 • • 發佈:2018-11-21
#include<iostream>
using namespace std;
const int Maxsize = 100;
template<typename T>
struct Node {
T data;
Node<T> *next;
};
template<typename T>
class inadd{
public:
inadd();//無參建構函式
inadd(T score[],int n);//有參建構函式
virtual ~inadd();//解構函式
void print();//遍歷操作
T get(int i);//按位查詢操作
int Locate(T x);//按值查詢操作
void insert(int i,T x);//插入操作
T Delete(int i);//刪除操作
bool changeList(int i, T x); //改變某一結點的值 i為節點的位置,x為替換的值
private:
Node<T> *first; //頭指標
int length; //結點數量
Node<T> *address[Maxsize]; //結點指標陣列
};
template<typename T>
inadd<T>::inadd()
{
first=new Node<T>;
first->next=NULL;
}
template<typename T>
inadd<T>::inadd(T score[],int n)
{
if (n > Maxsize) throw("溢位");
Node<T> *s;
first = new Node<T>;first->next=NULL; //初始化一個空連結串列
for(int i=n-1;i>=0;i--)
{
s=new Node<T>;s->data=score[i]; //為每個陣列元素建立一個結點
s->next=first->next;first->next=s; //將結點s插入頭結點之後
}
}
template<typename T>
inadd<T>::~inadd() //解構函式
{
Node<T> *q;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template<typename T>
void inadd<T>::insert(int i,T x)
{
Node<T>*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<T>;s->data=x;
s->next=p->next;
p->next=s;
length++;
}
template<typename T>
T inadd<T>::Delete(int i)
{
Node<T> *q,*p; T 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<typename T>
T inadd<T>::get(int i)
{
Node<T>*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<typename T>
int inadd<T>::Locate(T x)
{
Node<T>*p;int count =1;
p=first->next;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template<typename T>
void inadd<T>::print()
{
Node<T>*p;
p=first->next;
while(p!=NULL)
{cout<<p->data<<" ";;
p=p->next;
}
}
void main()
{
double score[5]={69.3,52.6,99,85.7,76.9};
inadd<double>student(score,5); //建立物件
cout<<" 學生資料結構成績"<<endl;
student.print();
cout<<endl<<endl<<"在位置3插入成績66,結果如下:"<<endl;
student.insert(3,66);
student.print();
cout<<endl<<endl<<"在位置2刪除成績為:"<<student.Delete(2)<<endl<<"刪除後結果如下:"<<endl;
student.print();
cout<<endl<<endl<<"位置3的成績為:"<<student.get(3)<<endl;
cout<<endl<<endl<<"成績76.9所在位置為:"<<student.Locate(76.9)<<endl;
}
using namespace std;
const int Maxsize = 100;
template<typename T>
struct Node {
T data;
Node<T> *next;
};
template<typename T>
class inadd{
public:
inadd();//無參建構函式
inadd(T score[],int n);//有參建構函式
virtual ~inadd();//解構函式
void print();//遍歷操作
T get(int i);//按位查詢操作
int Locate(T x);//按值查詢操作
void insert(int i,T x);//插入操作
T Delete(int i);//刪除操作
bool changeList(int i, T x); //改變某一結點的值 i為節點的位置,x為替換的值
private:
Node<T> *first; //頭指標
int length; //結點數量
Node<T> *address[Maxsize]; //結點指標陣列
};
template<typename T>
inadd<T>::inadd()
{
first=new Node<T>;
first->next=NULL;
}
template<typename T>
inadd<T>::inadd(T score[],int n)
{
if (n > Maxsize) throw("溢位");
Node<T> *s;
first = new Node<T>;first->next=NULL; //初始化一個空連結串列
for(int i=n-1;i>=0;i--)
{
s=new Node<T>;s->data=score[i]; //為每個陣列元素建立一個結點
s->next=first->next;first->next=s; //將結點s插入頭結點之後
}
}
template<typename T>
inadd<T>::~inadd() //解構函式
{
Node<T> *q;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template<typename T>
void inadd<T>::insert(int i,T x)
{
Node<T>*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<T>;s->data=x;
s->next=p->next;
p->next=s;
length++;
}
template<typename T>
T inadd<T>::Delete(int i)
{
Node<T> *q,*p; T 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<typename T>
T inadd<T>::get(int i)
{
Node<T>*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<typename T>
int inadd<T>::Locate(T x)
{
Node<T>*p;int count =1;
p=first->next;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template<typename T>
void inadd<T>::print()
{
Node<T>*p;
p=first->next;
while(p!=NULL)
{cout<<p->data<<" ";;
p=p->next;
}
}
void main()
{
double score[5]={69.3,52.6,99,85.7,76.9};
inadd<double>student(score,5); //建立物件
cout<<" 學生資料結構成績"<<endl;
student.print();
cout<<endl<<endl<<"在位置3插入成績66,結果如下:"<<endl;
student.insert(3,66);
student.print();
cout<<endl<<endl<<"在位置2刪除成績為:"<<student.Delete(2)<<endl<<"刪除後結果如下:"<<endl;
student.print();
cout<<endl<<endl<<"位置3的成績為:"<<student.get(3)<<endl;
cout<<endl<<endl<<"成績76.9所在位置為:"<<student.Locate(76.9)<<endl;
}