Java实现数据结构与算法-顺序表

来源:互联网 发布:图片上传到数据库 编辑:程序博客网 时间:2024/04/29 03:03

顺序表

按照顺序方式存储的线性表,在计算机中存放为一组连续的存储单元。只要知道首地址和长度,很容易计算出每一个元素的位置。

/** * @author Hash.Zhang * */public class SequentialList {static final int MAXLEN=100;Data []ListData=new Data[MAXLEN+1];int ListLen;/** * SLInit: Initiate the sequential list as an empty list. */public void SLInit(SequentialList SL){SL.ListLen=0;}/** * SLLength: Get the length of the sequential list. * @return the length of the seqential list */public int SLLength(SequentialList SL){return SL.ListLen;}/** * SLInsert: Insert a new node with data to SL at index n * @return true for successfully inserting; false for not successfully inserting */public boolean SLInsert(SequentialList SL,int n,Data data){if(SL.ListLen>=MAXLEN){System.out.println("The list if full! Cannot insert more nodes!");return false;}if(n<0||n>SL.ListLen-1){System.out.println("Wrong Index! Cannot insert node to that position!");return false;}for(int i=SL.ListLen;i>=n;i--){SL.ListData[i+1]=SL.ListData[i];}SL.ListData[n]=data;SL.ListLen++;return true;}/** * SLAdd: Add a new node with data at the end of the list * @return true for successfully inserting; false for not successfully inserting */public boolean SLAdd(SequentialList SL, Data data){if(SL.ListLen>=MAXLEN){System.out.println("The list if full! Cannot insert more nodes!");return false;}SL.ListData[SL.ListLen++]=data;return true;}/** * SLDelete: delete the node with same ID as key * @param key */public boolean SLDelete(SequentialList SL, String key){int position=SLFindByID(SL,key);if(position==-1){System.out.println("Key not found, please check!");return false;}for(int i=position;i<SL.ListLen-1;i++){SL.ListData[i]=SL.ListData[i+1];}SL.ListLen--;return true;}/** * SLDelete: delete the node at position n * @param n */public boolean SLDelete(SequentialList SL, int n){if(n<0||n>SL.ListLen-1){System.out.println("Wrong Index! Cannot delete the node at that position!");return false;}for(int i=n;i<SL.ListLen-1;i++){SL.ListData[i]=SL.ListData[i+1];}SL.ListLen--;return true;}/** * SLFindByNum: get the node at position n */public Data SLFindByNum(SequentialList SL,int n){if(n<0||n>SL.ListLen-1){System.out.println("Wrong Index! Cannot access the node at that position!");return null;}return SL.ListData[n];}/** * SLFindByNum: get the Index of the node with same ID as key  * @return -1 for not finding the key */public int SLFindByID(SequentialList SL, String key){for(int i=0;i<SL.ListLen;i++){if(SL.ListData[i].ID.equals(key)){return i;}}return -1;}/** * Print the content of the whole list */public void PrintSL(SequentialList SL){System.out.println("Net nodes: "+SL.ListLen);for(int i=0;i<SL.ListLen;i++){System.out.printf("(ID:%s,Sequence No.%d) ",SL.ListData[i].ID,SL.ListData[i].SequenceNo);}System.out.println();}/** * Main method to test */public static void main(String[] args) {// TODO Auto-generated method stubSequentialList SL=new SequentialList();for(int i=0;i<10;i++){SL.SLAdd(SL, new Data(i,"Hash"+i));}SL.PrintSL(SL);SL.SLInsert(SL, 5, new Data(19,"Hash"+19));SL.PrintSL(SL);SL.SLDelete(SL,1);SL.PrintSL(SL);SL.SLDelete(SL,"Hash19");SL.PrintSL(SL);}}
public class Data {<span style="white-space:pre"></span>int SequenceNo;<span style="white-space:pre"></span>String ID;<span style="white-space:pre"></span>public Data()<span style="white-space:pre"></span>{}<span style="white-space:pre"></span>public Data(int a)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>SequenceNo=a;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public Data(String ch)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>ID=ch;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public Data(int a,String ch)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>SequenceNo=a;<span style="white-space:pre"></span>ID=ch;<span style="white-space:pre"></span>}}

输出结果:

Net nodes: 10
(ID:Hash0,Sequence No.0) (ID:Hash1,Sequence No.1) (ID:Hash2,Sequence No.2) (ID:Hash3,Sequence No.3) (ID:Hash4,Sequence No.4) (ID:Hash5,Sequence No.5) (ID:Hash6,Sequence No.6) (ID:Hash7,Sequence No.7) (ID:Hash8,Sequence No.8) (ID:Hash9,Sequence No.9) 
Net nodes: 11
(ID:Hash0,Sequence No.0) (ID:Hash1,Sequence No.1) (ID:Hash2,Sequence No.2) (ID:Hash3,Sequence No.3) (ID:Hash4,Sequence No.4) (ID:Hash19,Sequence No.19) (ID:Hash5,Sequence No.5) (ID:Hash6,Sequence No.6) (ID:Hash7,Sequence No.7) (ID:Hash8,Sequence No.8) (ID:Hash9,Sequence No.9) 
Net nodes: 10
(ID:Hash0,Sequence No.0) (ID:Hash2,Sequence No.2) (ID:Hash3,Sequence No.3) (ID:Hash4,Sequence No.4) (ID:Hash19,Sequence No.19) (ID:Hash5,Sequence No.5) (ID:Hash6,Sequence No.6) (ID:Hash7,Sequence No.7) (ID:Hash8,Sequence No.8) (ID:Hash9,Sequence No.9) 
Net nodes: 9
(ID:Hash0,Sequence No.0) (ID:Hash2,Sequence No.2) (ID:Hash3,Sequence No.3) (ID:Hash4,Sequence No.4) (ID:Hash5,Sequence No.5) (ID:Hash6,Sequence No.6) (ID:Hash7,Sequence No.7) (ID:Hash8,Sequence No.8) (ID:Hash9,Sequence No.9) 

0 0
原创粉丝点击