用JAVA实现顺序表

来源:互联网 发布:老a淘宝工具箱 编辑:程序博客网 时间:2024/05/21 21:02

顺序表演示

package date;import java.util.Scanner;//顺序表数据元素类Dateclass Date {            // 模拟一个班级的学生记录    String key;                 String name;    int age;}class SLType {    static final int MAXLEN = 100;              //定义顺序表长度    Date[] listData = new Date[MAXLEN+1];       //废弃0索引,从1开始    int listLen;                                 // 顺序表已存结点的数量    //初始化顺序表    void SLInit(SLType sl) {            sl.listLen = 0;    }    //计算顺序表长度    int SLLength(SLType sl) {        return (sl.listLen);    }    // 插入节点,返回0表示插入失败,返回1表示插入成功    int SLInsert(SLType SL, int n, Date data) {        int i;        if (SL.listLen >= MAXLEN) {            System.out.println("顺序表已满,不能插入节点");            return 0;        }        if (n < 1 || n > SL.listLen - 1) {            System.out.println("插入序号有误,不能插入节点");            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, Date data) {        if (SL.listLen >= MAXLEN) {            System.out.println("顺序表已满,不能插入节点");            return 0;        }        SL.listData[++SL.listLen] = data;        return 1;    }    // 删除节点    int SLDelete(SLType SL, int n) {        int i;        if (n < 1 || n > SL.listLen) {            System.out.println("序号输入有误,不能删除节点");            return 0;        }        for (i = n; i <SL.listLen; i++) {            SL.listData[i] = SL.listData[i + 1];        }        SL.listLen--;//n=SL.listLen时直接执行此处        return 1;    }    // 查找节点    Date SLFindByNum(SLType SL, int n) {        if (n < 1 || n > SL.listLen) {            System.out.println("序号输入有误,不能插入节点");            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 SLAll(SLType SL) {        int i;        for (i = 1; i <= SL.listLen; i++) {            System.out.println(SL.listData[i].key + "#" + SL.listData[i].name + "#" + SL.listData[i].age);        }        return 0;    }}public class Shunxu {    public static void main(String[] args) {        int i;        SLType SL = new SLType(); // 定义顺序表变量        Date pdata; // 定义结点保存指针变量        String key; // 保存关键字        System.out.print("顺序表操作演示!\n");        SL.SLInit(SL); // 初始化顺序表        System.out.print("初始化顺序表完成!\n");        Scanner input = new Scanner(System.in);        do {        // 循环添加结点数据            System.out.print("输入添加的结点(学号 姓名 年龄):");            Date data = new Date();            data.key = input.next();            data.name = input.next();            data.age = input.nextInt();            SL.SLAdd(SL, data);            if (data.age == 0) // 设置退出循环的条件,若年龄为0退出死循环            {                break;             }        } while (true);        System.out.print("\n顺序表中的结点顺序为:\n");        SL.SLAll(SL); // 显示所有结点数据        System.out.print("\n要取出结点的序号:");        i = input.nextInt(); // 输入结占点序号        pdata = SL.SLFindByNum(SL, i); // 按序号查找结点        if (pdata != null) // 若返回的结点指针不为NULL        {            System.out.printf("第%d个结点为:(%s,%s,%d)\n", i, pdata.key, pdata.name, pdata.age);        }        System.out.print("\n要查找结点的关键字:");        key = input.next(); // 输入关键字        i = SL.SLFindByCont(SL, key); // 按关键字查找 ,返回结点序号        pdata = SL.SLFindByNum(SL, i); // 按序号查询,返回结点指针        if (pdata != null) // 若结点指针不为NULL        {            System.out.printf("第%d个结点为:(%s,%s,%d)\n", i, pdata.key, pdata.name, pdata.age);        }        System.out.print("\n删除结点的序号:");        i = input.nextInt(); // 输入结占点序号        int k = SL.SLDelete(SL, i); // 按序号查找结点        if (k==1) // 若返回的结点指针不为NULL        {            System.out.printf("删除成功");        }        System.out.print("\n删除后顺序表中的结点顺序为:\n");        SL.SLAll(SL); // 显示所有结点数据    }}
顺序表操作演示!初始化顺序表完成!输入添加的结点(学号 姓名 年龄):01 huge 33输入添加的结点(学号 姓名 年龄):02 allen 34输入添加的结点(学号 姓名 年龄):03 tangyan 30输入添加的结点(学号 姓名 年龄):04 shiliu 28输入添加的结点(学号 姓名 年龄):0 0 0顺序表中的结点顺序为:01#huge#3302#allen#3403#tangyan#3004#shiliu#280#0#0要取出结点的序号:5第5个结点为:(0,0,0)要查找结点的关键字:03第3个结点为:(03,tangyan,30)删除结点的序号:4删除成功删除后顺序表中的结点顺序为:01#huge#3302#allen#3403#tangyan#300#0#0
0 0
原创粉丝点击