数据结构线性数组的基本操作

package com.stu;
/**
 * 数组线性表的一些操作基本操作
 * @author Administrator
 *
 */
public class SeqList {


class DATA{
String stuid; //学号
String name;//姓名
int age;//年龄
}

class SLType{//定义顺序表结构
static final int MAXLEN=100;
DATA[] ListData = new DATA[MAXLEN+1];
int ListLen;//顺序表已存节点数量
void SLInin(SLType SL){
SL.ListLen=0; //初始化为空表
}
int SLLength(SLType SL){
return SL.ListLen; //返回顺序表元素数量
}
int SLInsert(SLType SL,int n,DATA data){ //插入元素
if(SL.ListLen>=MAXLEN){
System.out.println("顺序表已满,不能插入节点!");
return 0;//返回0插入不成功
}
if(n<1||n>SL.ListLen-1){
System.out.println("插入元素序号错误,不能插入元素");
return 0;
}
for(int i=SL.ListLen;i>n;i--){//将顺序表中的元素向后移动
SL.ListData[i+1]=SL.ListData[i];
}
SL.ListData[n]=data;
SL.ListLen++;
return 1;//返回1插入成功
}
int SLAdd(SLType SL,DATA data){ //添加元素到尾部
if(SL.ListLen>=MAXLEN){
System.out.println("顺序表已满,不能插入节点!");
return 0;//返回0插入不成功
}
SL.ListData[++SL.ListLen]=data;
return 1;
}
int SLDelete(SLType SL,int n,DATA data){
if(n<1||n>SL.ListLen+1){
System.out.println("删除的序号不正确");
return 0;
}
for(int i=0;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.println("返回序号不存在");
}
return SL.ListData[n];
}
int SLFindByCont(SLType SL,String key){ //根据关键字查询节点
for(int i=0;i<SL.ListLen;i++){
if(SL.ListData[i].key.compareTo(key)==0){
return i;
}
}
return 0;
}
int SLAll(SLType SL){//查询所有节点
for(int i=0;i<SL.ListLen;i++){
System.out.println("学号"+SL.ListData[i].key+"...");
}
return 0;//失败
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值