以前学习过链表的时候由于类型的接收不同,每次要重写链表

下面修改可用链表

  1. class Link{
  2.  
  3. private class Node{
  4. private Object data ;
  5. private Node next ;
  6.  
  7. public Node (Object data){
  8. this.data = data ;
  9. }
  10. public void add(Node newNode){
  11. if(this.next == null)
  12. this.next = newNode;
  13. else{
  14. this.next.add(newNode) ;
  15. }
  16. }
  17. public void print(){
  18. System.out.println(this.data) ;
  19. if(this.next == null)
  20. return ;
  21. else {
  22. this.next.print() ;
  23. }
  24. }
  25. public boolean containsNode(Object data){
  26. if(data.equals(this.data))
  27. return true;
  28. else {
  29. if(this.next != null)
  30. return this.next.containsNode(data);
  31. else
  32. return false ;
  33. }
  34.  
  35. }
  36. public Object getNode(int index){
  37. if(Link.this.foot ++ == index){
  38. return this.data ;
  39. } else {
  40. return this.next.getNode(index) ;
  41. }
  42. }
  43. public void setNode(int index ,Object data){
  44. if(Link.this.foot ++ == index){
  45. this.data = data ;
  46.  
  47. }
  48. else{
  49. this.next.setNode(index,data) ;
  50. }
  51. }
  52. public void removeNode(Node previous ,Object data) {
  53. if(data.equals(this.data)){
  54. previous.next = this.next ;
  55. } else {
  56.  
  57. this.next.removeNode(this,data) ;
  58.  
  59. }
  60. }
  61. public void toArrayNode(){
  62. Link.this.retArray[Link.this.foot ++ ] = this.data ;
  63. if(this.next != null)
  64. this.next.toArrayNode() ;
  65. }
  66. }
  67. private Node root ;
  68. private int count = 0 ;
  69. private int foot = 0 ;
  70. private Object [] retArray ;
  71. public void add(Object data){
  72.  
  73. if(data == null)
  74. return ;
  75.  
  76. Node newNode = new Node(data) ;
  77.  
  78. if (root == null)
  79. root = newNode;
  80. else {
  81. this.root.add(newNode) ;
  82. }
  83. this.count ++ ;
  84. }
  85. public void print(){
  86. this.foot = 0 ;
  87. if(root == null)
  88. return ;
  89. else{
  90. root.print() ;
  91. }
  92. }
  93. public int size() {
  94. return this.count ;
  95. }
  96. public boolean isEmpty(){
  97. return this.count == 0 ;
  98. }
  99. public boolean contains(Object data) {
  100. if(this.root == null || data == null)
  101. return false ;
  102. else {
  103. return this.root.containsNode(data) ;
  104. }
  105. }
  106. public Object get(int index){
  107. if(index > this.count)
  108. return null ;
  109. else{
  110. return this.root.getNode(index) ;
  111. }
  112.  
  113. }
  114. public void set(int index , Object data) {
  115. if(index > this.count)
  116. return ;
  117. else{
  118. this.foot = 0 ;
  119. this.root.setNode(index,data) ;
  120. }
  121.  
  122. }
  123. public void remove(Object data){
  124. if(this.contains(data)){
  125. if(data.equals(this.root.data))
  126. this.root = this.root.next ;
  127. else{
  128. this.root.next.removeNode(root,data) ;
  129. }
  130.  
  131. }
  132. }
  133. public Object [] toArray(){
  134. if(this.root == null){
  135. return null ;
  136. } else {
  137. this.foot = 0 ;
  138. this.retArray = new Object[this.count] ;
  139. this.root.toArrayNode() ;
  140. return this.retArray ;
  141. }
  142. }
  143.  
  144. }
  145.  
  146. public class Link1{
  147. public static void main(String args[]){
  148. Link all = new Link() ;
  149. all.add("A") ; //String转为Object
  150. all.add("B") ;
  151. all.add("C") ;
  152. all.remove("A") ;//String已经覆写了equals()方法
  153. Object [] data = all.toArray();
  154. for(int x = 0 ; x < data.length ; x ++){
  155. String str = (String)data[x] ; //每一个对象向下转型 Object变为String
  156. System.out.println(str) ;
  157. }
  158.  
  159. }
  160.  
  161. }

总结:

Object类对象可以接受一切数据类型,解决了数据统一问题

Object修改链表的更多相关文章

  1. CLRS10.2-4练习 - 修改链表查询方法

    要求: As written, each loop iteration in the LIST-SEARCH' procedure requires two tests:one for x ≠ L.n ...

  2. JAVA 链表操作:循环链表

    主要分析示例: 一.循环链表简述 二.单链表循环链表 三.双链表循环链表 一.循环链表简述 循环链表即链表形成了一个循环的结构,尾节点不再指向NULL,而是指向头节点HEAD,此时判定链表的结束是尾节 ...

  3. JAVA 链表操作:单链表和双链表

    主要讲述几点: 一.链表的简介 二.链表实现原理和必要性 三.单链表示例 四.双链表示例 一.链表的简介 链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都 ...

  4. 单链表的python实现

    首先说下线性表,线性表是一种最基本,最简单的数据结构,通俗点讲就是一维的存储数据的结构. 线性表分为顺序表和链接表: 顺序表示指的是用一组地址连续的存储单元依次存储线性表的数据元素,称为线性表的顺序存 ...

  5. Java链表讲解

    主要讲述几点: 一.链表的简介 二.链表实现原理和必要性 三.单链表示例 四.双链表示例 一.链表的简介 链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都 ...

  6. Python3玩转单链表——逆转单向链表pythonic版

    [本文出自天外归云的博客园] 链表是由节点构成的,一个指针代表一个方向,如果一个构成链表的节点都只包含一个指针,那么这个链表就是单向链表. 单向链表中的节点不光有代表方向的指针变量,也有值变量.所以我 ...

  7. java对单向单向链表的操作

    概述:众所周知,数据对于数据的存储时连续的,也就是说在计算机的内存中是一个整体的.连续的.不间断的ADT数据结构.伴随的问题也会随之出现,这样其实对于内存的动态分配是不灵活的.而链表具备这个优点.因此 ...

  8. Java链表设计

    链表 1,链表的实现 在实际开发之中对象数组是一项非常实用的技术,并且利用其可以描述出“多”方的概念,例如:一个人有多本书,则在人的类里面一定要提供有一个对象数组保存书的信息,但是传统的对象数组依赖于 ...

  9. 由反转链表想到python链式交换变量

    这两天在刷题,看到链表的反转,在翻解体思路时看到有位同学写出循环中一句搞定三个变量的交换时觉得挺6的,一般用的时候都是两个变量交换(a,b=b,a),这种三个变量的交换还真不敢随便用,而且这三个变量都 ...

随机推荐

  1. C++:预处理指令

    Preprocessor directives 预处理器指令 预处理器指令是指那些包含在我们代码中的预处理器语句行,这些预处理器语句不是真正的代码语句,但是他们指导程序如何进行编译.这些语句总是以 ‘ ...

  2. Android --- 读取系统资源函数getResources()小结

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1201/655.html 编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用, ...

  3. BootStrap详解之(一)

    一.BootStrap简介 BootStrap是一个用来构建网站前段框架的一个插件.无论你是想构建应用程序.博客还是CMS网站,Bootstrap都特别的使用,只要你想得到,它就能行.Bootstra ...

  4. 使用curl获取乱码问题

    今天通过curl获取百度地图接口数据,获取到居然是乱码,于是我查看是不是编码问题,发现返回的编码和自己的编码都是utf-8, 继续找原因,发现header报文中  Content-encoding 为 ...

  5. java的property

    System.currentTimeMillis() 返回以毫秒为单位的当前时间.System.gc() 垃圾回收System.getProperties().返回当前的系统属性System.getP ...

  6. Socket在手机上的应用

    usb读取:pid vid --可以唯一的确定设备获取手机驱动socket固定端口通信 wifipc机在局域网内,udp的数据包(整个网段) 蓝牙配对 bluetoothsocket 如果放大:可以分 ...

  7. Binary Watch

    Binary Watch 描述 Consider a binary watch with 5 binary digits to display hours (00 - 23) and 6 binary ...

  8. PAT (Advanced Level) 1073. Scientific Notation (20)

    简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  9. codeforces 665C Simple Strings

    相同的一段字母变一下就可以. #include<cstdio> #include<cstring> #include<cmath> #include<vect ...

  10. mysql 本地操作

    先把数据库传到根目录 再直接导入  没有50M限制root@b101 [/home/user/www]# mysql -uusername_007li -ppasswd -D dbname_li12d ...