顺序表
#include<iostream.h>
const int MaxSize=100;
class SeqList
{
public:
SeqList(){length=0;} //建立空的顺序表
SeqList(int a[],int n); //建立长度为n的顺序表
~SeqList(){} //析构函数
int Length(){return length;} //求线性表的长度
int Get(int i);
void Insert (int i,int x); //在位置i插入x
int Delete(int i);
void Print();
private:
int data[MaxSize]; //存放数据元素的数组
int length; //线性表的长度
};
SeqList::SeqList(int a[],int n)
{ int i;
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];
}
void SeqList::Insert(int i,int x)
{ int j;
if(length>=MaxSize)throw"上溢";
if(i<1||i>length+1)throw"位置非法";
for(j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
int SeqList::Delete(int i) //删除
{ int x,j;
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::Print()
{
int i;
for(i=0;i<length;i++)
cout<<"第"<<i+1<<"位学生成绩:"<<data[i]<<endl;
cout<<endl;
}
void main()
{int i;
intE[5]={87,89,90,99,97};
SeqList List(E,5);
cout<<"已有同学成绩:"<<endl;
List.Print();
cout<<"在第3位置插入85"<<endl;
List.Insert(3,88);
cout<<"插入同学成绩后的成绩:"<<endl;
List.Print();
cout<<"第5位同学的成绩:"<<endl;
cout<<List.Get(5)<<endl;
cout<<"删除第一位同学的成绩"<<endl;
List.Delete(1);
cout<<"删除后的成绩为:"<<endl;
List.Print();
}
静态链表
#include <iostream.h>
const int MaxSize=100;
struct SNode
{
int data;
int next;
}SList[MaxSize];
class Student
{
public:
Student();
Student(int a[],int n);
~Student();
int Length();
int Locate(int x);
void Insert(int i,int x);
int Delete(int i);
void Print();
private:
int first,avail;
};
Student::Student()
{
for(int i=0;i<MaxSize-1;i++)
SList[i].next=i+1;
SList[MaxSize-1].next=0;
}
Student::Student(int a[],int n)
{
if(n<0||n>MaxSize)
throw"不存在此操作";
for(int i=0;i<MaxSize;i++)
{
SList[i].next=i+1;
}
SList[MaxSize-1].next=-1;
first=0;
avail=1;
SList[first].next=-1;
for(int j=0;j<n;j++)
{
int s=avail;
avail=SList[avail].next;
SList[s].data=a[j];
SList[s].next=SList[first].next;
SList[first].next=s;
}
}
Student::~Student()
{}
int Student::Length()
{
if(SList[1].next==0)
return 0;
int i=1;
int count=-1;
while(i!=0)
{
count++;
i=SList[i].next;
}
return count;
}
intStudent::Locate(int x)
{ if(SList[first].next==-1)
throw"注意!空的成绩!";
int p=first,count=0;
while(SList[p].next!=-1)
{if(SList[p].data==x) return count;
p=SList[p].next;
count++; } return 0; }
voidStudent::Insert(int i,int x)
{
if(i<0||i>MaxSize) throw"参数非法";
int s=avail;
int p=first;
if(p==-1)
throw"参数非法";
for(int count=0;count<i-1;count++)
{
p=SList[p].next;
}
avail=SList[avail].next;
SList[s].data=x;
SList[s].next=SList[p].next;
SList[p].next=s;
}
int Student::Delete(int i)
{if(i<0||i>MaxSize)
throw"参数非法";
int p=first;
if(p==-1)
throw"参数非法";
for(int count=0;count<i-1;count++)
{
p=SList[p].next;
}
int q=SList[p].next;
SList[p].next=SList[q].next;
SList[q].next=avail;
avail=q;
return 0; }
void Student::Print()
{int p=SList[first].next;
while(p!=-1)
{cout<<"第"<<p<<"个学生的成绩:"<<SList[p].data<<endl;
p=SList[p].next; }
cout<<endl; }
void main()
{inti;
inte[5]={43,21,89,90,100};
Student List(e,5);
cout<<"已有同学的成绩:"<<endl;
List.Print();
cout<<endl;
cout<<"在第2个位置插入72"<<endl;
List.Insert(2,72);
cout<<"插入后学生成绩为:"<<endl;
List.Print();
cout<<endl;
cout<<"学生成绩为90的位置:"<<endl;
cout<<List.Locate(90)<<endl;
cout<<"删除第一个学生成绩"<<endl;
List.Delete(1);
cout<<"删除后学生成绩为:"<<endl;
List.Print();}
单链表
#include <iostream.h>
const int MaxSize=100;
struct Node
{
int data;
Node *next;
};
class LinkList
{
public:
LinkList();
LinkList(int a[],int n);
~LinkList();
int Length();
int Get(int i);
void Input(int i,int x);
int Delete(int i);
void Print();
private:
Node *first;
};
LinkList::LinkList()
{
first=new Node;
first->next=NULL;
}
LinkList::LinkList(int a[],int n)
{
Node *r;
Node *s;
int i;
first=new Node;
r=first;
for(i=0;i<n;i++)
{
s=new Node;s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}
LinkList::~LinkList()
{
while(first!=NULL)
{
Node *q;
q=first;
first=first->next;
delete q;
}
}
int LinkList::Length()
{
Node *p;
int count;
p=first->next; count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}
int LinkList::Get(int i)
{
Node *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;
}
void LinkList::Input(int i,int x)
{
Node *p;
int count;
p=first;count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL) throw"位置";
else
{
Node *s;
s=new Node;s->data=x;
s->next=p->next;p->next=s;
}
}
intLinkList::Delete(int i)
{
Node *p;
Node *q;
int x;
int count;
p=first;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;
}
}
voidLinkList::Print()
{
Node *p;
int i=0;
p=first->next;
while(p!=NULL)
{
i=i+1;
cout<<"第"<<i<<"个学生成绩:"<<p->data<<endl;
p=p->next;
}
}
void main()
{
int E[5]={43,55,90,93,89};
LinkList List(E,5);
cout<<"已有同学成绩:"<<endl;
List.Print();
cout<<"在第4位置插入88"<<endl;
List.Input(4,88);
cout<<"插入同学成绩后的成绩:"<<endl;
List.Print();
cout<<"第4位同学的成绩:"<<endl;
cout<<List.Get(4)<<endl;
cout<<"删除第一位同学的成绩"<<endl;
List.Delete(1);
cout<<"删除后的成绩为:"<<endl;
List.Print();
}
双链表
#include <iostream>
using namespace std;
const int M=100;
struct Node
{
intdata;
Node *next,*prior;
};
class LinkList
{
public:
LinkList();
LinkList(int a[],int n);
~LinkList();
intLength();
intGet(int i);
void Input(int i,int x);
intDelete(int i);
void Print();
private:
Node *first;
};
LinkList::LinkList()
{
first=new Node;
first->next=NULL;
}
LinkList::LinkList(int a[],int n)
{
Node *r;
Node *s;
int i;
first=new Node;
r=first;
for(i=0;i<n;i++)
{
s=new Node;s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}
LinkList::~LinkList()
{
while(first!=NULL)
{
Node *q;
q=first;
first=first->next;
delete q;
}
}
int LinkList::Length()
{
Node *p;
int count;
p=first->next; count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
return count;
}
int LinkList::Get(int i)
{
Node *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;
}
void LinkList::Input(int i,int x)
{
Node *p;
int count;
p=first;count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL) throw"位置";
else
{
Node *s;
s=new Node;s->data=x;
s->prior= p;
s->next = p->next;
p->next->prior = s;
p->next = s;
}
}
intLinkList::Delete(int i)
{
Node *p;
Node *q;
int x;
int count=0;
p=first;q=NULL;
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;
(q->next)->prior = p->prior;
delete q;
return x;
}
}
voidLinkList::Print()
{
Node *p;
int i=0;
p=first->next;
while(p!=NULL)
{
i=i+1;
cout<<"第"<<i<<"个同学的成绩:"<<p->data<<endl;
p=p->next;
}
}
voidmain()
{
int E[5]={76,75,98,83,88};
LinkList List(E,5);
cout<<"已有同学成绩:"<<endl;
List.Print();
cout<<"在第4位置插入88"<<endl;
List.Input(4,88);
cout<<"插入同学成绩后的成绩:"<<endl;
List.Print();
cout<<"第4位同学的成绩:"<<endl;
cout<<List.Get(4)<<endl;
cout<<"删除第一位同学的成绩"<<endl;
List.Delete(1);
cout<<"删除后的成绩为:"<<endl;
List.Print();
}
间接寻址
#include<iostream.h>
const int Maxsize = 100;
template<class E>
struct Node {
Edata;
Node<E> *next;
};
template<class E>
class inA{
public:
inA();
inA(E score[],int n);
virtual ~inA();
void print();
E get(int i);
int Locate(E x);
void insert(int i,E x);
E Delete(int i);
bool changeList(int i, E x);
private:
Node<E> *first;
int length;
Node<E> *address[Maxsize];
};
template<class E>
inA<E>::inA()
{
first=new Node<E>;
first->next=NULL;
}
template<class E>
inA<E>::inA(E score[],int n)
{
if (n > Maxsize) throw("溢出");
Node<E> *s;
first = new Node<E>;first->next=NULL;
for(int i=n-1;i>=0;i--)
{
s=new Node<E>;s->data=score[i];
s->next=first->next;first->next=s;
}
}
template<class E>
inA<E>::~inA()
{
Node<E> *q;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template<class E>
void inA<E>::insert(int i,E x)
{
Node<E>*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<E>;s->data=x;
s->next=p->next;
p->next=s;
length++;
}
template<class E>
E inA<E>::Delete(int i)
{
Node<E> *q,*p;
Ex;
intcount;
p=first;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 E>
E inA<E>::get(int i)
{
Node<E>*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 E>
int inA<E>::Locate(E x)
{
Node<E>*p;int count =1;
p=first->next;
while(p!=NULL)
{
if(p->data==x)returncount;
p=p->next;
count++;
}
return 0;
}
template<class E>
void inA<E>::print()
{
Node<E>*p;
p=first->next;
while(p!=NULL)
{cout<<p->data<<" ";;
p=p->next;
}
}
void main()
{
float e[5]={55,56,89,88,99};
inA<float>student(e,5);
cout<<"学生数据结构成绩"<<endl;
student.print();
cout<<endl<<endl<<"在位置4插入成绩67,结果如下:"<<endl;
student.insert(3,99);
student.print();
cout<<endl<<endl<<"在位置3删除成绩为:"<<student.Delete(3)<<endl<<"删除后结果如下:"<<endl;
student.print();
cout<<endl<<endl<<"位置5的成绩为:"<<student.get(5)<<endl;
cout<<endl<<endl<<"成绩88所在位置为:"<<student.Locate(88)<<endl;
}