顺序表

1.java实现的java

package com.sjx.test;

import java.util.Scanner;

class DATA
{
    String key;
    String name;
    int age;
}

class SLType
{
    static final int MAXLEN=100;
    DATA[] ListData=new DATA[MAXLEN+1];
    int ListLen;
    
    //将Listen初始化为0
    void SLInit(SLType SL)
    {
        SL.ListLen=0;
    }
    
    //获得Listen的值
    int SLLength(SLType SL)
    {
        return(SL.ListLen);
    }
    
    
    int SLInsert(SLType SL, int n, DATA data)
    {
        int i;
        if(SL.ListLen>=MAXLEN)
        {
            System.out.print("顺序表已满,不能插入结点!
");
            return 0;
        }
        if(n<1 || n>SL.ListLen-1)
        {
            System.out.print("插入元素序号错误,不能插入元素!
");
            return 0;
        }
        for(i=SL.ListLen; i>=n; i--)
        {
            SL.ListData[i+1]=SL.ListData[i];
        }
        SL.ListData[n]=data;
        SL.ListLen++;
        return 1;
    }
    
    int SLAdd(SLType SL, DATA data)
    {
        if(SL.ListLen>=MAXLEN)
        {
            System.out.print("顺序表已满,不能再添加结点了!
");
            return 0;
        }
        SL.ListData[++SL.ListLen]=data;
        return 1;
    }
    
    int SLDelete(SLType SL, int n)
    {
        int i;
        if(n<1 || n>SL.ListLen+1)
        {
            System.out.print("删除结点序号错误,不能删除结点!
");
            return 0;
        }
        for(i=n; i<SL.ListLen; i++)
        {
            SL.ListData[i]=SL.ListData[i+1];
        }
        SL.ListLen--;
        return 1;
    }
    
    DATA SLFindByNum(SLType SL, int n)
    {
        if(n<1 || n>SL.ListLen+1)
        {
            System.out.print("结点序号错误,不能返回结点!
");
            return null;
        }
        return SL.ListData[n];
    }
    
    int SLFindByCont(SLType SL, String key)
    {
        int i;
        for(i=1; i<=SL.ListLen; i++)
        {
            if(SL.ListData[i].key.compareTo(key)==0)
            {
                return i;
            }
        }
        return 0;
    }
    
    int SLAII(SLType SL)
    {
        int i;
        for(i=1; i<=SL.ListLen; i++)
        {
            System.out.printf("(%s,%s,%d)
",SL.ListData[i].key, SL.ListData[i].name, SL.ListData[i].age );
        }
        return 0;
    }
}


public class Text1
{
    public static void main(String[] args)
    {
        int i;
        SLType SL=new SLType();
        DATA pdata;
        String key;
        
        System.out.print("顺序表操作演示!
");
        
        SL.SLInit(SL);
        System.out.print("初始化顺序表完成!
");
        
        Scanner input=new Scanner(System.in);
        
        do
        {
            System.out.print("输入添加的结点(学号 姓名 年龄):");
            DATA data=new DATA();
            data.key=input.next();
            data.name=input.next();
            data.age=input.nextInt();
            
            if(data.age!=0)
            {
                if(SL.SLAdd(SL, data)==0)
                    {
                    break;
                    }
            }
            else
            {
                break;
            }
        }while(true);
        System.out.print("
顺序表中的结点顺寻为:
");
        SL.SLAII(SL);
        
        
        System.out.print("
要取出结点的序号:");
        i=input.nextInt();
        pdata=SL.SLFindByNum(SL, i);
        if(pdata!=null)
        {
            System.out.printf("第%d个节点为:(%s, %s, %d)
", i, pdata.key, pdata.name, pdata.age);
        }
        
        System.out.print("
要查找结点的关键字:");
        key=input.next();
        i=SL.SLFindByCont(SL, key);
        pdata=SL.SLFindByNum(SL, i);
        if(pdata!=null)
        {
            System.out.printf("第%d个节点为:(%s, %s, %d)
", i, pdata.key, pdata.name, pdata.age);
        }
    }
}
原文地址:https://www.cnblogs.com/sjxbg/p/5875538.html