public interface IListDS<T>{
int GetLength();
void Clear();
bool IsEmpty();
void Append(T item);
void Insert(T item,int i);
T Delete(int i);
T GetElem(int i);
int Locate(T value);
void Reverse();
}
public class SeqList<T>:IListDS<T>{
private int maxsize;
private T[] data;
private int last;
public T this[int index]{
get{
return data[index];
}
set{
data[index]=value;
}
}
public int Last{
get{
return last;
}
}
public int Maxsize{
get{return maxsize;}
set{maxsize=value;}
}
public SeqList(int size){
data=new T[size];
maxsize=size;
last=-1;
}
public int GetLength(){
return last+1;
}
public void Clear(){
last=-1;
}
public bool IsEmpty(){
if(last==-1){
return true;
}else{
return false;
}
}
public bool IsFull(){
if(last==maxsize-1){
return true;
}else{
return false;
}
}
public void Append(T item){
if(IsFull){
Console.WriteLine("List is full");
return;
}
data[++last]=item;
}
public void Insert(T item,int i){
if(IsFull){
Console.WriteLine("List is full");
return;
}
if(i<1 || i>last+2){
Console.WriteLine("Position is error");
return;
}
if(i==last+2){
data[last+1]=item;
}
else{
for(int j=last;j>i-1;--j){
data[j+1]=data[j];
}
data[j-1]=item;
}
++last;
}
public T Delete(int i){
T tmp=default(T);
if(IsEmpty()){
Console.WriteLine("List is empty");
return tmp;
}
if(i<1 || i>last+1){
Console.WriteLine("Position is error");
return tmp;
}
if(i==last+1){
tmp=data[last--];
}else{
tmp=data[i-1];
for(int j=i;j<=last;++j){
data[j]=data[j+1];
}
}
--last;
return tmp;
}
public T GetElem(int i){
if(IsEmpty() || (i<1) || (i>last+1)){
Console.WriteLine("List is empty or Position is error");
return default(T);
}
return data[i-1];
}
public int Locate(T value){
if(IsEmpty()){
Console.WriteLine("List is empty");
return -1;
}
int i=0;
for(i;i<=last;++i){
if(value.Equals(data[i])){
break;
}
}
if(i>last){
return -1;
}
return i;
}
public void Reverse(){
T tmp=Default(T);
int len=GetLength();
for(int i=0;i<len/2;++i){
tmp=data[i];
data[i]=data[len-i];
data[len-i]=tmp;
}
}
}
//头变成尾的倒置,第一个与最后一个交换
public void ReverSeqList(SeqList<int> L){
int tmp=0;
int len=L.GetLength();
for(int i=0;i<=len/2;i++){
tmp=L[i];
L[i]=L[len-i];
L[len-i]=tmp;
}
}
//任何线性表都可以进行倒置操作
public SeqList<int> Merge(SeqList<int> La,SeqList<int> Lb){
SeqList<int> Lc=new SeqList<int>(La.Maxsize+Lb.Maxsize)'
int i=0;
int j=0;
int k=0;
while((i<=(La.GetLength()-1))&& (j<=(Lb.GetLength()-1))){
if(La[i]<Lb[j]){
Lc.Append(La[i++]);
}
else{
Lc.Append(Lb[j++]);
}
}
while(i<=(La.GetLength()-1)){
Lc.Append(La[i++]);
}
while(j<=(Lb.GetLength()-1)){
Lc.Append(Lb[j++]);
}
return Lc;
}
public SeqList<int> Purge(SeqList<int> La){
SeqList<int> Lb=new SeqList<int>(La.Maxsize);
Lb.Append(La[0]);
for(int i=1;i<=La.GetLength()-1;i++){
int j=0;
for(j;j<=Lb.GetLength()-1;++j){
if(La[i].CompareTo(Lb[j])==0){
break;
}
}
if(j>Lb.GetLength()-1){
Lb.Append(La[i]);
}
}
return Lb;
}
c#线性表
最新推荐文章于 2022-04-21 10:43:09 发布