JAVA顺序表的简单实现

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;			//顺序表已存结点的数量
	
	
	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++;
		return 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--;
		return 1;
	}
	
	//查找节点
	DATA SLFindByNum(SLType SL,int n){
		if(n<1||n>SL.ListLen+1){
			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 SequentialList {
		public static void main(String[] args) {
			int i;
		    SLType SL=new SLType();         		//定义顺序表变量 
//		    DATA data=new DATA();       			//定义结点保存数据类型变量
			DATA pdata;				//定义结点保存指针变量 
		    String key;           		//保存关键字
			
			System.out.print("顺序表操作演示!\n"); 
			
		    SL.SLInit(SL);       			//初始化顺序表 
			System.out.print("初始化顺序表完成!\n");
			
			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)               //若年龄不为0 
		        {
		            if(SL.SLAdd(SL,data)==0)   //若添加结点失败 
					{
		                break;            //退出死循环 
					}
		        }
			   else   				//若年龄为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);  
			}
		  	

		}



}

  

原文地址:https://www.cnblogs.com/elleniou/p/3096478.html