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;//失败
}
}
}
/**
* 数组线性表的一些操作基本操作
* @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;//失败
}
}
}