一、线性表的抽象数据类型表述

线性表的结构简单,长度允许动态增长或搜索;可以对线性表中的任何数据元素进行访问和查找;允许进行数据的插入和删除操作;求线性表中的指定数据的前驱和后继;合并线性表以及拆分线性表中。

Java JDK中有ArrayList和LinkedList两个类很好的实现了顺序存储和链式存储。因此学习数据结构的最好方式是去研究JDK源码。

这里的代码个人作为练习,为了便于理解,很多地方处理的并非十分严谨,仅供参考。转载注明出处,技术讨论 email: <yaoyao0777@Gmail.com>

  1. package org.yohhan.data_structure.api;
  2.  
  3. /**
  4. * @author yohhan <yaoyao0777@Gmail.com>
  5. * @date create:2016年1月10日 下午4:05:35
  6. *
  7. * 线性表的抽象数据类型
  8. *
  9. */
  10. public interface MyList {
  11. public void clear();
  12.  
  13. public boolean isEmpty();
  14.  
  15. public int length();
  16.  
  17. public Object get(int i) throws Exception;
  18.  
  19. public void insert(int i, Object obj) throws Exception;
  20.  
  21. public void remove(int i) throws Exception;
  22.  
  23. public int indexOf(Object obj);
  24.  
  25. public void display();
  26. }

二、线性表的顺序存储实现

1.顺序表的定义:

顺序存储采用一组地址连续的存储单元依次存储线性表占用的各个数据元素的存储结构。

2.顺序表的特点:

1)逻辑上相邻的数据元素,在物理存储位置上也是相邻的。

2)存储密度高,事先需要分配足够应用的存储空间。

3)随机存取,查询速度快,直接访问地址单元中的数据。时间复杂度 O(1)

4)插入删除操作会引起大量的数据移动,时间复杂度O(n)

3.顺序表的结构类描述:

  1. package org.yohhan.data_structure.linear;
  2.  
  3. import org.yohhan.data_structure.api.MyList;
  4.  
  5. /**
  6. * @author yohhan <yaoyao0777@Gmail.com>
  7. * @date create:2016年1月10日 下午4:09:15
  8. *
  9. * 线性表的顺序存储实现
  10. */
  11. public class SqList implements MyList {
  12.  
  13. private Object[] listElem; // 数组作为线性表的存储空间
  14. private int curLen; // 线性表的当前长度
  15.  
  16. public SqList(int maxSize) {
  17. curLen = 0;
  18. listElem = new Object[maxSize];
  19. }
  20.  
  21. @Override
  22. public void clear() {
  23. curLen = 0;
  24. }
  25.  
  26. @Override
  27. public boolean isEmpty() {
  28. return curLen == 0;
  29. }
  30.  
  31. @Override
  32. public int length() {
  33. return curLen;
  34. }
  35.  
  36. @Override
  37. public Object get(int i) throws Exception {
  38. if (i < 0 || i > curLen - 1)
  39. throw new Exception("元素不存在");
  40. return listElem[i];
  41. }
  42.  
  43. @Override
  44. public void insert(int i, Object obj) throws Exception {
  45. if (curLen == listElem.length)
  46. throw new Exception("存储空间已满");
  47. if (i < 0 || i > curLen)
  48. throw new Exception("插入位置不合法");
  49. for (int j = curLen; j > i; j--)
  50. listElem[j] = listElem[j - 1];
  51. listElem[i] = obj;
  52. ++curLen;
  53. }
  54.  
  55. @Override
  56. public void remove(int i) throws Exception {
  57. if (i < 0 || i > curLen - 1)
  58. throw new Exception("删除位置不合法");
  59. for (int j = i; j < curLen; j++)
  60. listElem[j] = listElem[j + 1];
  61. --curLen;
  62. }
  63.  
  64. @Override
  65. public int indexOf(Object obj) {
  66. int j = 0;
  67. while (j < curLen && !listElem[j].equals(obj))
  68. j++;
  69. if (j < curLen)
  70. return j;
  71. return -1;
  72. }
  73.  
  74. @Override
  75. public void display() {
  76. for (int j = 0; j < curLen; j++)
  77. System.out.print(listElem[j] + ", ");
  78. }
  79.  
  80. @Override
  81. public String toString() {
  82. StringBuffer sb = new StringBuffer();
  83. sb.append("[");
  84. for (Object obj : listElem) {
  85. if (null != obj)
  86. sb.append(obj.toString() + ", ");
  87. }
  88. sb.delete(sb.length() - 2, sb.length());
  89. sb.append("]");
  90. return sb.toString();
  91. }
  92.  
  93. }

三、线性表的链式存储实现

1.单链表的定义:

采用链式存储方式存储的线性表,链表中每一个结点中包含存放数据元素值的数据域和存放逻辑上相邻结点的指针域。(示例中的结点只包含一个指针域,称为单链表(Single Linked List))

(图片摘自网上)

单链表通过指向后继结点的指针将结点串联成一条链。

以线性表中的第一个数据元素的存储地址作为线性表的起始地址,称为线性表的头指针。通过头指针(head)来唯一标识一个链表。

单链表中的最后一个节点没有后继,指针域为空指针(null),称为尾结点。

示例中采用一个“虚头结点”数据域不存放具体值,指针域中的指针指向单链表的第一个结点(首结点)。即当头结点中的指针域为空时,链表为空

2.链式存储的特点:

1)不需要预先分配存储空间,动态分配存储空间,存储密度较低、

2) 无法随机查询,需要从头遍历来查找元素。时间复杂度O(n)

3)便于进行数据的插入和删除。时间复杂度O(1)

3.线性表的单链表实现:

结点类结构:

  1. package org.yohhan.data_structure.node;
  2.  
  3. /**
  4. * @author yohhan <yaoyao0777@Gmail.com>
  5. * @date create:2016年1月10日 下午4:39:15
  6. *
  7. * 链表的结点类描述
  8. *
  9. */
  10. public class Node {
  11.  
  12. private Object data;
  13. private Node next;
  14.  
  15. public Node() {
  16. this(null, null);
  17. }
  18.  
  19. public Node(Object data) {
  20. this(data, null);
  21. }
  22.  
  23. public Node(Object data, Node next) {
  24. super();
  25. this.data = data;
  26. this.next = next;
  27. }
  28.  
  29. public Object getData() {
  30. return data;
  31. }
  32.  
  33. public void setData(Object data) {
  34. this.data = data;
  35. }
  36.  
  37. public Node getNext() {
  38. return next;
  39. }
  40.  
  41. public void setNext(Node next) {
  42. this.next = next;
  43. }
  44.  
  45. }

单链表类的描述:

  1. package org.yohhan.data_structure.linear;
  2.  
  3. import org.yohhan.data_structure.api.MyList;
  4. import org.yohhan.data_structure.node.Node;
  5.  
  6. /**
  7. * @author yohhan <yaoyao0777@Gmail.com>
  8. * @date create:2016年1月10日 下午4:51:58
  9. *
  10. * 带头结点的单链表实现
  11. *
  12. */
  13. public class LinkList implements MyList {
  14.  
  15. private Node head;
  16.  
  17. public LinkList() {
  18. head = new Node();
  19. }
  20.  
  21. public LinkList(int n, boolean order) throws Exception {
  22. this();
  23. if (order)
  24. createFromTail(n);
  25. else
  26. createFromHead(n);
  27. }
  28.  
  29. private void createFromTail(int n) throws Exception {
  30. // 可在此添加输入流,传入数据
  31.  
  32. for (int j = 0; j < n; j++)
  33. insert(length(), j + 1);
  34. }
  35.  
  36. private void createFromHead(int n) throws Exception {
  37. // 可在此添加输入流,传入数据
  38.  
  39. for (int j = 0; j < n; j++)
  40. insert(0, j + 1);
  41. }
  42.  
  43. @Override
  44. public void clear() {
  45. head.setData(null);
  46. head.setNext(null);
  47. }
  48.  
  49. @Override
  50. public boolean isEmpty() {
  51. return head.getNext() == null;
  52. }
  53.  
  54. @Override
  55. public int length() {
  56. Node node = head.getNext();
  57. int length = 0;
  58. while (node != null) {
  59. node = node.getNext();
  60. ++length;
  61. }
  62. return length;
  63. }
  64.  
  65. /**
  66. * 按位序号查找算法
  67. */
  68. @Override
  69. public Object get(int i) throws Exception {
  70. Node node = head.getNext();
  71. int j = 0;
  72. while (node != null && j < i) {
  73. node = node.getNext();
  74. ++j;
  75. }
  76. if (j > i || node == null)
  77. throw new Exception("元素不存在");
  78. return node.getData();
  79. }
  80.  
  81. /**
  82. * 在index前插入新结点
  83. */
  84. @Override
  85. public void insert(int i, Object obj) throws Exception {
  86. Node node = head;
  87. int j = -1;
  88. while (node != null && j < i - 1) {
  89. node = node.getNext();
  90. ++j;
  91. }
  92. if (j > i - 1 || node == null)
  93. throw new Exception("插入位置不合法");
  94. Node newNode = new Node(obj);
  95. newNode.setNext(node.getNext());
  96. node.setNext(newNode);
  97. }
  98.  
  99. @Override
  100. public void remove(int i) throws Exception {
  101. Node node = head;
  102. int j = -1;
  103. while (node.getNext() != null && j < i - 1) { // 链表不能为空
  104. node = node.getNext();
  105. ++j;
  106. }
  107. if (j > i - 1 || node == null)
  108. throw new Exception("删除位置不合法");
  109. node.setNext(node.getNext().getNext());
  110. }
  111.  
  112. /**
  113. * 按值查找算法
  114. */
  115. @Override
  116. public int indexOf(Object obj) {
  117. Node node = head.getNext();
  118. int j = 0;
  119. while (node != null && !node.getData().equals(obj)) {
  120. node = node.getNext();
  121. ++j;
  122. }
  123. if (node != null)
  124. return j;
  125. return -1;
  126. }
  127.  
  128. @Override
  129. public void display() {
  130. Node node = head.getNext();
  131. while (node != null) {
  132. System.out.print(node.getData() + "");
  133. node = node.getNext();
  134. }
  135. System.out.println();
  136. }
  137.  
  138. @Override
  139. public String toString() {
  140. StringBuffer sb = new StringBuffer();
  141. sb.append("[");
  142. Node node = head.getNext();
  143. while (node != null) {
  144. sb.append(node.getData().toString() + ", ");
  145. node = node.getNext();
  146. }
  147. sb.delete(sb.length() - 2, sb.length());
  148. sb.append("]");
  149. return sb.toString();
  150. }
  151.  
  152. }

附:线性表的单元测试类:

  1. package org.yohhan.data_structure.test;
  2.  
  3. import static org.junit.Assert.*;
  4. import org.junit.Test;
  5. import org.yohhan.data_structure.linear.LinkList;
  6. import org.yohhan.data_structure.linear.SqList;
  7.  
  8. /**
  9. * @author yohhan <yaoyao0777@Gmail.com>
  10. * @date create:2016年1月10日 下午4:25:12
  11. *
  12. * 线性表单元测试类
  13. *
  14. */
  15. public class LinearTest {
  16.  
  17. @Test
  18. public void sqListTest() throws Exception {
  19. SqList sqList = new SqList(10);
  20. for (int i = 0; i < 5; i++)
  21. sqList.insert(i, i + 1);
  22. System.out.println(sqList);
  23. assertTrue(sqList.toString().equals("[1, 2, 3, 4, 5]"));
  24. assertTrue(sqList.length() == 5);
  25. assertTrue(sqList.indexOf(2) == 1);
  26. assertTrue(sqList.get(0).equals(1));
  27. sqList.remove(1);
  28. assertTrue(sqList.length() == 4);
  29. assertTrue(sqList.indexOf(1) == 0);
  30. }
  31.  
  32. @Test
  33. public void linkListTest() throws Exception {
  34. LinkList linkList = new LinkList(5, true);
  35. System.out.println(linkList);
  36. assertTrue(new LinkList(5, false).toString().equals("[5, 4, 3, 2, 1]"));
  37. assertTrue(linkList.toString().equals("[1, 2, 3, 4, 5]"));
  38. assertTrue(linkList.length() == 5);
  39. assertTrue(linkList.indexOf(2) == 1);
  40. assertTrue(linkList.get(0).equals(1));
  41. linkList.remove(1);
  42. assertTrue(linkList.length() == 4);
  43. assertTrue(linkList.indexOf(1) == 0);
  44.  
  45. }
  46.  
  47. }

线性表结构的Java实现的更多相关文章

  1. [置顶] ※数据结构※→☆线性表结构(queue)☆============队列 顺序存储结构(queue sequence)(八)

    队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进行删除操作的 ...

  2. [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)

    循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...

  3. [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)

    优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...

  4. [置顶] ※数据结构※→☆线性表结构(stack)☆============栈 序列表结构(stack sequence)(六)

    栈(stack)在计算机科学中是限定仅在表尾进行插入或删除操作的线性表.栈是一种数据结构,它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据.栈 ...

  5. ※数据结构※→☆线性表结构(list)☆============单向循环链表结构(list circular single)(四)

    循环链表是另一种形式的链式存贮结构.它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环. 单循环链表——在单链表中,将终端结点的指针域NULL改为指向表头结点或开始结点即可. 循环链表的 ...

  6. 根据数据库表结构生成java类

    import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWrit ...

  7. [置顶] ※数据结构※→☆线性表结构(list)☆============单向链表结构(list single)(二)

    单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始:链表是使用指针进行构造的列表:又称为结点列表,因为链表是由一个个结点组装起来的:其中每个结点都有指 ...

  8. [置顶] ※数据结构※→☆线性表结构(list)☆============双向链表结构(list double)(三)

    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点. ~~~~~~~~~~~~ ...

  9. 线性表的Java实现

    一.概念 对于常用的数据结构,可分为线性结构和非线性结构,线性结构主要是线性表,非线性结构主要是数和图.当n>0时,表可表示为:(a0,a1,a2,a3,…an) 1. 线性表的特征: 1.存在 ...

随机推荐

  1. Keras学习基础(2)

    目录: Keras的模块结构 数据预处理 模型 网络层 网络配置 Keras中的数据处理 文本预处理 序列预处理 图像预处理 Keras中的模型 Sequential顺序模型 Model模型[通用模型 ...

  2. HDU 2266 How Many Equations Can You Find(模拟,深搜)

    题目 这是传说中的深搜吗....不确定,,,,貌似更加像是模拟,,,, //我要做深搜题目拉 //实际上还是模拟 #include<iostream> #include<string ...

  3. apply,call,bind区别

    js中有三个改变this指针的方法,分别是 apply,call,bind.很多人只知道能改变的this,但是具体的适用场景不是太清楚.我也是遇到坑后不断的实践发现了区别. call ,apply方法 ...

  4. 八进制、十进制、操作符(day04)

    把二进制表示的数字从右向左每三个数位分成 一组,每组用一个0到7之间的数字替换. 这个替换结果叫做数字的八进制表示方式 (八进制) 可以直接在程序里用八进制方式表示数字, 这种数字必须以0做开头 可以 ...

  5. 1069. The Black Hole of Numbers

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  6. Ningx初学

    原文地址 1. 到官网下载Ningx 2. 启动 2.1双击nginx.exe图标,可见黑窗口一闪而过,启动完毕. 2.2 命令行到nginx目录,输入nginx启动.(注,此方式命令行窗口无任何提示 ...

  7. binlog

    binlog基本定义:二进制日志,也成为二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制的形式保存在磁盘中: 作用:MySQL的作用类似于Oracle的归档日志,可以用来查看数据库的变 ...

  8. [jQuery]$.get跨域提交不发送原因

    使用 $.ajax({ url: "http://pastebin.com/embed_js.php?i=sy9gt3FR", dataType: "jsonp" ...

  9. HDU 4510

    省一等,保研. #include <iostream> #include <cstdio> #include <cstring> #include <algo ...

  10. ural 1707. Hypnotoad's Secret(线段树)

    题目链接:ural 1707. Hypnotoad's Secret 题目大意:给定N和M,然后N组s0, t0, Δs, Δt, k,每组能够计算出k个星星的坐标:M组a0, b0, c0, d0, ...