java建立顺序表及增删改查的实现



//package seqlist;


public class Seqlist {


public static void main(String[] args) {
// TODO Auto-generated method stub
seqList L=new seqList();
seqList s=new seqList();
seqList M=new seqList();
L.insert(L.a);
System.out.print("原数组为:");
    L.printList();
    System.out.println();
    L.add(70, 5);
    System.out.print("增加后的数组为:");
    L.printList();
    System.out.println();
    L.delList(8);
    System.out.print("删除后的数组为:");
    L.printList();
    System.out.println();
    L.chaList(6, 100);
    System.out.print("修改后的数组为:");
    L.printList();
    System.out.println();
    System.out.print("要查找的内容为:");
    L.serList(1);
    System.out.println();
    L.sort();
    System.out.print("升序后的数组为:");
    L.printList();
    System.out.println();
    s.insert(s.a);
    L.lixiaoya(M,s);
    System.out.print("合并后的数组为:");
    M.printList();
    }
}
class seqList
{
int lenlist;
int a[] = new int[30];

public void insert(int a[])//数组赋初值
{
int i=0,j;
for(j=0;j<10;j++,i++)
{
this.a[j]=i+3;
this.lenlist++;
}
}
public void add(int x,int n)//在第n个位置上插入值为x的元素
{
int i;
if(n>this.lenlist||n<0)
{
System.out.println("Insert error!");
}
else
{
for(i=this.lenlist;i>n-1;i--)
{
this.a[i]=this.a[i-1];
}
this.a[n-1]=x;
this.lenlist++;
}
}
public void printList()//输出数组函数
{
int i;
if(this.lenlist==0)
{
System.out.print("ERROR:Empty");
}
for(i=0;i<this.lenlist;i++)
{
System.out.print(this.a[i]+" ");
}
}


public void delList(int x)//删除第X位置的数据
{
int i;
if(x<0||x>this.lenlist)
{
System.out.println("Delete error!");
}
else{
for(i=x;i<this.lenlist;i++)
{
this.a[i-1]=this.a[i];
}
this.lenlist--;
}
}
public void chaList(int x,int b)//修改x位置上的数据为b
{
int i=0;
if(x<0||x>this.lenlist)
{
System.out.println("Change error!");
}
else{
for(i=0;i<10;i++)
{
if(i==x-1)
{
this.a[i]=b;
}
}
}
}
public void serList(int x)//查找x位置上的数据
{
int i=0;
if(x<0||x>this.lenlist)
{
System.out.println("Search error!");
}
else{
for(i=0;i<this.lenlist;i++)
{
if(i==x-1)
System.out.print(this.a[i]);
}
       }
}
public  void lixiaoya(seqList a,seqList s)//合并函数
{
if(a.lenlist==0&&s.lenlist==0)
{
System.out.print("ERROR:Empty");
}
int i=0,j;
for(j=0;j<this.lenlist;j++)
{
a.a[i++]=this.a[j];
a.lenlist++;
}
for(j=0;j<s.lenlist;j++)
{
a.a[i++]=s.a[j];
a.lenlist++;
}
}
public void sort()//升序函数
{
int i,j,x;
if(this.lenlist==0)
{
System.out.print("ERROR:Empty");
}
for(i=0;i<this.lenlist;i++)
{
for(j=i+1;j<this.lenlist;j++)
{
if(this.a[j]>this.a[i])
{
x=this.a[j];
this.a[j]=this.a[i];
this.a[i]=x;
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值