数据结构--顺序线性表
来源:互联网 发布:怎么给淘宝设置密码 编辑:程序博客网 时间:2024/04/30 00:01
最近学习java,用这门语言温习一下数据结构。
public class List{ private int length; private int[] array; public List() { length = 0; array = new int[20]; } public boolean Insert(int value) { int j; if(length >= 20) { return false; } j = length - 1; while(j >= 0 && array[j] > value) { array[j+1] = array[j]; j--; } array[j+1] = value; length++; return true; } public boolean Delete(int index) { if(0 == length) { return false; } if(index < 1 && index > length) { return false; } int i = index - 1; while(i < (length - 1)) { array[i] = array[i+1]; i++; } length--; return true; } public void Clear() { length = 0; } public int ListLength() { return length; } public boolean IsEmpty() { if(length > 0) { return false; } else { return true; } } public int GetItem(int index) { return array[index-1]; }}
还以为java函数中基本类型变量作为参数都是引用形式的,原来和C++一样。
线性表的使用要注意数组下标。
线性表的链式实现:
class List{ int data; List next; List(int val,List L) { data = val; next = L; } List(int val) { data = val; next = null; } void SetNext(List L) { next = L; } void SetData(int val) { data = val; } int GetData() { return data; } List GetNext() { return next; }}//测试代码public class test { public static void main(String []args){ List head = new List(0,null); List temp = head; for(int i=0;i<=9;i++) { List node = new List(i,null); temp.SetNext(node); temp = temp.GetNext(); } temp = head.GetNext(); while(null != temp) { System.out.println(temp.GetData()); temp = temp.GetNext(); } }}
感觉java都用引用很方便,比C++使用大量的指针安全多了。内存也不用自己去析构,很方便。
0 0
- 数据结构--顺序线性表
- 数据结构--顺序线性表
- 线性数据结构--顺序表
- 数据结构-线性顺序表
- 数据结构-线性表-顺序表
- 数据结构-线性表-顺序表
- 数据结构 线性表 顺序表
- 数据结构-线性表-顺序表
- 【数据结构-线性表】顺序表
- 数据结构-线性表-顺序表
- 数据结构实现顺序线性表
- 数据结构--线性表顺序存储
- 数据结构线性表顺序建链表
- 数据结构--线性表顺序实现
- 顺序线性表-数据结构(1)
- 数据结构---线性表---顺序存储
- 数据结构之顺序线性表
- 数据结构之顺序线性表
- java cmmo api自述文件/Readme_chm.html
- 自动装箱
- logback logback.xml常用配置详解(一)<configuration> and <logger>
- 安防布线过程中需要避免的十个主要错误(转载)
- ios开发之蓝牙4.0技术
- 数据结构--顺序线性表
- POJ1330 Nearest Common Ancestors[LCA]
- Android控件之TextClock
- 第七周 oj训练3
- ios学习-- Twitter 的启动动画的实现(bird flying)
- LeetCode[398] Random Pick Index
- JQ读取Json
- JSP与Servlet之间的关系
- springmvc+mybatis使用存储过程查询多结果集