Java算法(顺序表操作实例)

来源:互联网 发布:前端工程师是程序员吗 编辑:程序博客网 时间:2024/04/30 04:26

Java算法(顺序表操作实例)


源代码如下:

package com.xu.main;


import java.util.Scanner;


public class P2_1 {


/**
* @功能:顺序表操作实例
* @作者:
* @日期:2012-10-15
*/
public static void main(String[] args) {


int i;
SLType SL = new SLType(); // 定义顺序表变量
DATA pdata; // 定义结点保存引用变量
String key; // 保存关键字


System.out.println("顺序表操作演示!");


SL.SLInit(SL); // 初始化顺序表
System.out.println("初始化顺序表完成");


Scanner input = new Scanner(System.in);


do {
System.out.println("输入添加的结点(学好     姓名     年龄):");
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 // 若年龄为0
{
break; // 退出死循环
}


} while (true);
System.out.println("顺序表中的结点顺序为:");
SL.SLAll(SL); // 显示所有结点数据


System.out.println("要取出结点的序号:");
i = input.nextInt(); // 输入结点的序号
pdata = SL.SLFindByNum(SL, i); // 按序号查找结点
if (pdata != null) {
System.out.printf("第%d个结点为:(%s,%s,%d)\n", i, pdata.key, pdata.name,
pdata.age);
}


System.out.println("要查找的结点的关键字:");
key = input.next(); // 输入关键字
i = SL.SLFindByCount(SL, key); // 按关键字查找
pdata = SL.SLFindByNum(SL, i); // 按序号查找,返回结点引用
if (pdata != null) {
System.out.printf("第%d个结点为:(%s,%s,%d)\n", i, pdata.key, pdata.name,
pdata.age);
}
}
}


class DATA {
String key; // 节点的关键字
String name;
int age;
}


class SLType // 定义顺序表的结构数组
{
static final int MAXLEN = 100;
DATA[] ListData = new DATA[MAXLEN + 1]; // 保存顺序表的结构数组
int ListLen; // 顺序表已存节点的数量


void SLInit(SLType SL) // 初始化顺序表
{
SL.ListLen = 0; // 初始化为空表
}


int SLLength(SLType SL) {
return SL.ListLen; // 返回顺序表的元素数量
}


int SLInsert(SLType SL, int n, DATA 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++; // 顺序表结点数量增加1
return 1; // 成功插入,返回1
}


int SLAdd(SLType SL, DATA 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 + 1) // 删除节点序号不正确
{
System.out.println("删除结点序号错误,不能删除元素!");
return 0;
}
for (i = n; i < SL.ListLen; i++) // 将顺序表中的数据向前移动
{
SL.ListData[i] = SL.ListData[i + 1];
}
SL.ListLen--; // 顺序元素数量减1
return 1; // 成功删除,返回1
}


DATA SLFindByNum(SLType SL, int n) // 根据序号返回数据元素
{
if (n < 1 || n > SL.ListLen + 1) {
System.out.println("结点序号错误,不能返回结点!");
return null;
}
return SL.ListData[n];


}


int SLFindByCount(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; // 搜索整个表后仍没有找到,则返回0
}


int SLAll(SLType SL) // 显示顺序表中的所有结点
{
int i;
for (i = 1; i <= SL.ListLen; i++) {
System.out.printf("(%s,%s,%d)\n", SL.ListData[i].key,
SL.ListData[i].name, SL.ListData[i].age);
}
return 0;
}
}


运行结果:


原创粉丝点击