class Program
{
static void Main(string[] args)
{
int a=0;//用来存储所取元素
int b = 0;//用来存储删除的元素
int c=0;//用c来存储元素4的位置
int d = 0;//用d来存储元素4的前驱
int f = 0;//用f来存储元素4的后继
SqList La = new SqList();//创建线性表实例
//La.Length=0将线性表置
La.List_Empty(La);//判断表是不是空表
La.GetElem(La, 3, ref a);//取线性表中的第三个元素并用a来存储
La.Delete_SqList(La, 3, ref b);//用b存储删除线性表中的第三个元素
La.Insert_SqList(La, 4, 5);//在线性表第四个元素前插入元素5
c = La.Locate_SqList(La, 4);//用c来存储元素4的位置
La.PriorElem(La, 4, ref d);//用d来存储元素4的前驱
La.NextElem(La, 4, ref f);//用f来存储元素4的后驱
Console.WriteLine("c=" + c);
Console.WriteLine("d=" + d);
Console.WriteLine("f=" + f);
Console.Write("线性表中的元素:");
for (int i = 0; i < La.Length; i++)
Console.Write( La.elem[i]+" ");
}
}
class SqList
{
public int[] elem;
public int Length;
int Listsize;
public SqList()
{
string s = " ";
Console.WriteLine("请输入线性表的长度/n");
s = Console.ReadLine();
Length = Int32.Parse(s);
elem = new int[Length];
Console.WriteLine("请输入线性表的元素/n");
for (int i = 0; i < Length; i++)
{
s = Console.ReadLine();
elem[i] = Int32.Parse(s);
}
}//初始化线性表
public void List_Empty(SqList L)
{
if (L.Length == 0) Console.WriteLine("该表为空表");
else Console.WriteLine("该表非空");
}//判断表是不是空表
public void GetElem(SqList L, int i, ref int e)
{
if (i < 1 || i > L.Length) Console.WriteLine("error");
e = L.elem[i - 1];
}//取线性表中的第i个元素
public void Delete_SqList(SqList L,int i,ref int e)
{
if(i<1||i>L.Length) Console.WriteLine("error1");//判断删除位置是否正确
e=L.elem[i-1];
for(int j=i-1;j<L.Length-1;j++)
{
L.elem[j] = L.elem[j+1];
}
L.Length--;
}//删除线性表中第i元素
public void Insert_SqList(SqList L,int i,int e)
{
if(i<1||i>L.Length) Console.WriteLine("error2");
for (int j = L.Length; j >= i - 1; j--)
L.elem[j ] = L.elem[j-1];
L.elem[i-1]=e;
L.Length++;
}//把元素e插入到位置i前
public int Locate_SqList (SqList L,int e)
{
for( int i=0;i<L.Length;i++)
{
if(L.elem[i]==e) return i+1;
}
return 0;
}//定位操作(找到与e相等元素的位置)
public void PriorElem(SqList L,int cur_e,ref int pre_e)
{
int k=Locate_SqList(L,cur_e);
if(k==0||k==1) Console.WriteLine("无前驱");
else
pre_e=L.elem[k-2];
}//求前驱(找到cur_e的前驱,用pre_e存储)
public void NextElem(SqList L,int cur_e,ref int next_e)
{
int k=Locate_SqList(L,cur_e);
if(k<0||k>L.Length-1) Console.WriteLine("无后继");
else
next_e=L.elem[k];
}//求后继
}
}