Linear Structure of data structures

Source: Internet
Author: User

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;    }}

 

Linear Structure of data structures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.