码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构之线性结构

时间:2014-10-27 22:34:35      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   java   for   sp   数据   div   

import java.util.*;
class SLType{

    public static void main(String args[]){
        int i;
        SL sl = new SL();
        DATA pdata;
        String key;
        
        System.out.println("the show of sequence list");
        
        sl.SLlnit(sl);
        System.out.println("initialise sequence list is end");
        
        Scanner input = new Scanner(System.in);
        do{
            System.out.println("please input the data");
            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{
                break;
            }
        }while(true);
            
            System.out.println("the list‘s data is :");
            sl.SLAll(sl);    
    }
}

class DATA{                    //用来模拟数据
    String key;
    String name;
    int age;
}

class SL{                   //定义线性表
    static final int MAXLEN = 100;
    DATA[] ListData=new DATA[MAXLEN+1];
    int Listlen;
    
    void SLlnit(SL sl){
        sl.Listlen=0;
    }
    int SLLength(SL sl){
        return sl.Listlen;
    }
    //add a new data
    int SLInsert(SL sl,int n,DATA data){
        int i;
        if(sl.Listlen>=MAXLEN){
            System.out.println("The SL is full;don‘t add new");
            return 0;
        }
        if(n<1||n>sl.Listlen-1){
            System.out.println("n is error,don‘t add data");
            return 0;
        }
        for(i=sl.Listlen;i>=n;i--){
            sl.ListData[i]=sl.ListData[i+1];
        }
        sl.ListData[n]=data;
        sl.Listlen++;
        return 1;
    }
    //add the new data in the list‘s end
    int SLAdd(SL sl,DATA data){
        if(sl.Listlen>=MAXLEN){
            System.out.println("the list is full;");
            return 0;
        }
        sl.ListData[++sl.Listlen]=data;
        return 1;
    }
    //delete the list‘s anyone data
    int SLDelete(SL sl,int n){
        int i;
        if(n<1||n>sl.Listlen+1){
            System.out.println("the node is error where you want to delete!");
            return 0;
        }
        for(i=n;i<sl.Listlen;i++){
            sl.ListData[i]=sl.ListData[i+1];
        }
        sl.Listlen--;
        return 1;
    }
    //find the one where it‘s number is n
    DATA SLFind(SL sl,int n){
        if(n<1||n>sl.Listlen){
            System.out.println("the node is error where you want to find");
        }
        return sl.ListData[n];
    }
    //find the one where it has the special key
    int SLFind(SL 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(SL sl){
        for(int i=1;i<=sl.Listlen;i++){
        DATA data = sl.ListData[i];
            System.out.print(data.key + "  "+data.name +"  "+ data.key+"\n");
        }
        return 0;
    }
}

 

数据结构之线性结构

标签:style   blog   color   ar   java   for   sp   数据   div   

原文地址:http://www.cnblogs.com/zrlhss/p/4055098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!