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)
- Java实现数据结构与算法-顺序表
- Java数据结构与算法 顺序表的实现
- Java实现数据结构与算法-顺序栈与链表栈
- (4) 数据结构与算法 ---- 线性表 及Java实现 顺序表、链表、栈、队列
- 数据结构与算法 ---- 线性表 及Java实现 顺序表、链表、栈、队列
- 数据结构与算法Java版——线性顺序表的实现
- 数据结构与算法--顺序表
- 《数据结构、算法与应用》9.(C++实现顺序表)
- java实现顺序表数据结构
- 数据结构Java实现【2】----线性表与顺序表
- 数据结构Java实现02----线性表与顺序表
- 数据结构Java实现02----线性表与顺序表
- 数据结构Java实现02----线性表与顺序表
- 数据结构算法实现-顺序表基本操作
- 数据结构与算法(Java描述)-6、栈的基本概念以及顺序栈的实现
- 数据结构与算法系列---顺序表
- 算法与数据结构之一------顺序表
- 【数据结构与算法分析】顺序表
- 如何让 height:100%; 起作用
- POJ 3728 && POJ 1986
- 几种常见排序算法的java实现
- hdu1232
- linux设备驱动开发详解第3版
- Java实现数据结构与算法-顺序表
- [综合面试] 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- 找工作---面试
- Android Animation动画详解(二): 组合动画特效
- 初识redis
- Linux内核探秘:深入解析文件系统和设备驱动的架构与设计
- 跨站脚本攻击详解
- MongoDB数据表基本操作
- ubuntu修改/etc/passwd用户名无法登陆