展开全部

//单链表类

package dataStructure.linearList;

import dataStructure.linearList.Node; //导入单链表结点类

import java.util.Iterator; //导入迭代器接口

public class SinglyLinkedList extends AbstractList implements LList //单链表类,实现线性表接口

{

protected Node head; //头指针,指向单链表第32313133353236313431303231363533e59b9ee7ad94313332393064661个结点

public SinglyLinkedList() //构造空单链表

{

this.head = null;

}

public SinglyLinkedList(Node head) //构造指定头指针的单链表

{

this.head = head;

}

public boolean isEmpty() //判断单链表是否为空,O(1)

{

return this.head==null;

}

public int length() //返回单链表长度

{ //单链表遍历算法,O(n)

int i=0;

Node p=this.head;

while (p!=null)

{

i++;

p = p.next;

}

return i;

}

public E get(int index) //返回序号为index的对象,index初值为0

{ //若单链表空或序号错误返回null,O(n)

if (this.head!=null && index>=0)

{

int j=0;

Node p=this.head;

while (p!=null && j

{

j++;

p = p.next;

}

if (p!=null)

return (E)p.data;

}

return null;

}

public E set(int index, E element) //设置序号为index的对象为element,O(n)

{ //若操作成功返回原对象,否则返回null

if (this.head!=null && index>=0 && element!=null)

{

int j=0;

Node p=this.head;

while (p!=null && j

{

j++;

p = p.next;

}

if (p!=null)

{

E old = (E)p.data;

p.data = element;

return old; //若操作成功返回原对象

}

}

return null; //操作不成功

}

public boolean add(int index, E element) //插入element对象,插入后对象序号为index

{ //若操作成功返回true,O(n)

if (element==null)

return false; //不能添加空对象(null)

if (this.head==null || index<=0) //头插入

this.head = new Node(element, this.head);

else //单链表不空且index>=1

{

int j=0;

Node p=this.head;

while (p.next!=null && j

{

j++;

p = p.next;

}

p.next = new Node(element, p.next);//中间/尾插入

}

return true;

}

public boolean add(E element) //在单链表最后添加对象,重载,O(n)

{

return add(Integer.MAX_VALUE, element);

}

public E remove(int index) //移去序号为index的对象,O(n)

{ //若操作成功返回被移去对象,否则返回null

E old = null;

if (this.head!=null && index>=0)

if (index==0) //头删除

{

old = (E)this.head.data;

this.head = this.head.next;

}

else //中间/尾删除

{

int j=0;

Node p=this.head;

while (p.next!=null && j

{

j++;

p = p.next;

}

if (p.next!=null)

{

old = (E)p.next.data; //操作成功,返回原对象

p.next = p.next.next; //删除p的后继结点

}

}

return old;

}

public void clear() //清空单链表,O(1)

{

this.head = null;

}

public String toString() //返回显示单链表所有元素值对应的字符串

{ //单链表遍历算法,O(n)

String str="(";

Node p = this.head;

while (p!=null)

{

str += p.data.toString();

p = p.next;

if (p!=null)

str += ", ";

}

return str+")";

}

//以上实现LList接口,第2章

//以下2.4 迭代器内容

public Iterator iterator() //返回一个迭代器对象

{

return new Itr();

}

private class Itr implements Iterator //私有内部类,实现迭代器接口

{

private Node cursor = head;

public boolean hasNext() //若有后继元素,返回true

{

return cursor!=null && cursor.next!=null;

}

public E next() //返回后继元素

{

if (cursor != null && cursor.next!=null)

{

E element = cursor.next.data;

cursor = cursor.next;

return element;

}

return null;

}

public void remove() //不支持该操作

{

throw new UnsupportedOperationException();

}

}//内部类Itr结束

//以下第8章 8.2.1 顺序查找,散列表中用

public Node search(E element, Node start) //从单链表结点start开始顺序查找指定对象

{ //若查找成功返回结点,否则返回null

if (this.head==null || element==null)

return null;

Node p=start;

while (p!=null && !element.equals(p.data))

{

System.out.print(p.data+"? ");

p = p.next;

}

return p;

}

public Node search(E element) //顺序查找指定对象

{

return search(element, head);

}

/*

public boolean contain(E element) //以查找结果判断单链表是否包含指定对象

{ //若包含返回true,否则返回false

return this.search(element)!=null;

}

*/

public boolean remove(E element) //移去首次出现的指定对象,O(n)

{ //若操作成功返回true

if (this.head==null || element==null)

return false;

if (element.equals(this.head.data))

{

this.head = this.head.next; //头删除

return true;

}

Node front=this.head, p=front.next; //中间/尾删除

while (p!=null && !element.equals(p.data))

{

front = p;

p=p.next;

}

if (p!=null)

{

front.next = p.next;

return true;

}

return false;

}

//以下是第2章习题

public SinglyLinkedList(E[] element) //由指定数组中的多个对象构造单链表

{

this.head = null;

if (element!=null && element.length>0)

{

this.head = new Node(element[0]);

Node rear=this.head;

int i=1;

while (i

{

rear.next = new Node(element[i++]);

rear = rear.next;

}

}

}

public void concat(SinglyLinkedList list) //将指定单链表list链接在当前单链表之后

{

if (this.head==null)

this.head = list.head;

else

{

Node p=this.head;

while (p.next!=null)

p = p.next;

p.next = list.head;

}

}

public SinglyLinkedList(SinglyLinkedList list) //以单链表list构造新的单链表

{ //复制单链表

this.head = null;

if (list!=null && list.head!=null)

{

this.head = new Node(list.head.data);

Node p = list.head.next;

Node rear = this.head;

while (p!=null)

{

rear.next = new Node(p.data);

rear = rear.next;

p = p.next;

}

}

}

//递归方法

// public SinglyLinkedList(SinglyLinkedList list) //以单链表list构造新的单链表

public void copy(SinglyLinkedList list) //复制单链表

{

this.head = copy(list.head);

}

private Node copy(Node p) //复制单链表,递归方法

{

Node q=null;

if (p!=null)

{

q = new Node(p.data);

q.next = copy(p.next);

}

return q;

}

/*//递归方法

public String toString()

{

return "("+ this.toString(this.head) +")";

}

public String toString(Node p) //递归方法

{

if (p!=null)

return p.data.toString() + ", " + this.toString(p.next); //递归调用

return "";

}

public SinglyLinkedList(E[] element) //由指定数组中的多个对象构造单链表

{

this.head = null;

if (element!=null)

this.head = create(element,0);

}

private Node create(E[] element, int i) //由指定数组构造单链表

{ //递归方法

Node p=null;

if (i

{

p = new Node(element[i]);

p.next = create(element, i+1);

}

return p;

}

*/

public boolean equals(Object obj) //比较两条单链表是否相等

{

if (obj == this)

return true;

if (obj instanceof SinglyLinkedList)

{

SinglyLinkedList list = (SinglyLinkedList)obj;

return equals(this.head, list.head);

}

return false;

}

private boolean equals(Node p, Node q) //比较两条单链表是否相等,递归方法

{

if (p==null && q==null)

return true;

if (p!=null && q!=null)

return p.data.equals(q.data) && equals(p.next, q.next);

return false;

}

//以下是第8章习题

public boolean replace(Object obj, E element)//将元素值为obj的结点值替换为element,O(n)

{ //若替换成功返回true,否则返回false

if (obj==null || element==null)

return false;

Node p=this.head;

while (p!=null)

{

if (obj.equals(p.data))

{

p.data = element;

return true;

}

p = p.next;

}

return false;

}

public boolean replaceAll(Object obj, E element) //将所有元素值为obj的结点值替换为element,O(n)

{ //若替换成功返回true,否则返回false

boolean done=false;

if (obj!=null && element!=null)

{

Node p=this.head;

while (p!=null)

{

if (obj.equals(p.data))

{

p.data = element;

done = true;

}

p = p.next;

}

}

return done;

}

public boolean removeAll(Object obj) //将所有元素值为obj的结点删除

{

if (this.head==null || obj==null)

return false;

boolean done=false;

while (this.head!=null && obj.equals(this.head.data))

{

this.head = this.head.next; //头删除

done = true;

}

Node front=this.head, p=front.next;

while(p!=null)

{

if (obj.equals(p.data))

{

front.next = p.next; //删除p结点

p = front.next;

done = true;

}

else

{

front = p;

p = p.next;

}

}

return done;

}

public static void main(String[] args)

{

String[] letters={"A","B","C","D","E","F"};

SinglyLinkedList list1 = new SinglyLinkedList(letters);

SinglyLinkedList list2 = new SinglyLinkedList(list1);

list2.copy(list1);

System.out.println(list2.toString());

System.out.println("equals(), "+list2.equals(list1));

}

}

/*

程序运行结果如下:

(A, B, C, D, E, F)

*/

/* 第2章 //可行,但效率低,时间复杂度是O(n*n)。

public String toString()

{

String str="{";

if (this.length()!=0)

{

for(int i=0; i

str += this.get(i).toString()+", ";

str += this.get(this.length()-1).toString();

}

return str+"}";

}

*/

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

java单链表例子_写一个java链表的例子?随便举例说一下。相关推荐

  1. java将古诗竖排_写一个java程序 将一首古诗竖着排序从右往左读

    展开全部 import java.awt.Color; import java.awt.Font; import java.awt.GradientPaint; import java.awt.Gra ...

  2. mysql死锁 简单例子_写一个Mysql死锁的例子

    创建表 CREATE TABLE `test1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL, PRIMAR ...

  3. 控制台编写JAVA程序教程_写一个java程序的步骤是什么?写java程序技巧

    写Java程序是要按照步骤来的,这样才能写好一个java程序,那么接下来,我们就来给大家讲解一下写一个java程序的步骤是什么? (1)创建Java项目:"FileàNewàProjectà ...

  4. java结果分行显示_编写一个java程序。分行显示自己的姓名,地址,电话!用Test.java命名。_学小易找答案...

    [单选题]16.骨骼肌进行完全强直收缩时,相邻两次刺激的时间间隔应 [填空题]实习岗位名称 [单选题]神经调节的基本方式是: [单选题]Thank you for your nice gifts. - ...

  5. java输入字符串异常_设计一个 Java 程序,自定义异常类,从命令行(键盘)输入一个字符串,如果该字符串值为“XYZ”。。。...

    设计一个 Java 程序,自定义异常类,从命令行(键盘)输入一个字符串,如果该字符串值为"XYZ",则抛出一个异常信息"This is a XYZ",如果从命令 ...

  6. java 多线程 卖票_编写一个Java 多线程程序,完成三个售票窗口同时出售20张票(如下图所示);...

    编写一个Java 多线程程序,完成三个售票窗口同时出售20张票(如下图所示); 程序分析:(1)票数要使用同一个静态值: (2)为保证不会出现卖出同一个票数,要java多线程同步锁. 设计思路: (1 ...

  7. java编写salary函数_编写一个Java程序,在程序中包含一个Employee类,Employee类包含name、age、salary三个成员变量...

    编写一个Java程序,在程序中包含一个Employee类,Employee类包含name.age.salary三个成员变量,Employee类中有4个构造方法,分别为无参的.带一个参数用来对name属 ...

  8. java 64内存不足_请教一个 Java 内存占用的问题

    第 1 条附言  ·  364 天前 2020-03-04 01:08:55.525 [HikariPool-1 housekeeper] WARN c.z.hikari.pool.HikariPoo ...

  9. java 数据立方_写一个Java应用程序,从键盘输入一个整数,然后输出它的平方值立方值...

    这是一个跟输入/输出流有关的面试题.下面我们来分析怎么实现该功能. 解析:在java中没有像C语言那样有一个专供接收从键盘输入值的scanf函数,所以一般的做法是从键盘输入一行字符,保存到字符串s中, ...

最新文章

  1. Django学习之路(一)--初识django
  2. AI大牛纷纷离职!2021大厂AI Lab现状盘点,网友:名存实亡
  3. C++中的内联函数inline
  4. 调用加了SSL签名的WebService
  5. 详解使用fastboot为Android刷入原厂镜像
  6. 无法初始化sftp协议。主机是sftp服务器吗?_WinSCP v5.15.3 免费的 开源图形化 SFTP 客户端...
  7. 微软发布 Microsoft Edge 85 稳定版
  8. python中test_在python中生成py.test测试
  9. Apache FOP与Eclipse和OSGi的集成
  10. nginx 一个请求发给多台机器_配置Nginx实现负载均衡
  11. 2020-08-21 Qt+MSVC 强制中文UTF-8编码
  12. python one class svm_sklearn例程:OneClassSVM物种分布建模
  13. redis 包之间关系
  14. 项目设计之----命令模式的利用
  15. Windows防火墙 命令行批量阻断攻击IP(非范围)
  16. 基于struts2的个人信息管理系统(一)
  17. ClickHouse 之 FORMAT 应用
  18. css之sticky定位
  19. Python 绘图大全之使用 Python Folium 制作生成热图的详细指南
  20. 投影仪如何选择?怎样选购家用投影仪

热门文章

  1. nginx 安装 虚拟主机
  2. 解决AJAX CalendarExtender控件不显示中文的情况(转帖博客园某人(不好意思,实在是没有找到您的尊姓大名,感谢一下!))...
  3. getHibernateTemplate()和getSession()的区别
  4. js bool true false 真假比较
  5. 使用 Android NDK 的交叉编译工具链移植 C/C++ 项目到安卓平台
  6. android中TextView显示中文发生乱码的问题
  7. Yacc 与 Lex 快速入门(词法分析和语法分析)
  8. 后台开发必读书籍--Linux 高性能服务器编程
  9. linux命令端口探测
  10. shell实例第7讲:awk命令