package com.tyxh.link;
//节点类
public class Node {
protected Node next; //指针域
protected int data;//数据域 public Node( int data) {
this. data = data;
} //显示此节点
public void display() {
System. out.print( data + " ");
}
} package com.tyxh.link;
//单链表
public class LinkList {
public Node first; // 定义一个头结点
private int pos = 0;// 节点的位置 public LinkList() {
this. first = null;
} // 插入一个头节点
public void addFirstNode( int data) {
Node node = new Node(data);
node. next = first;
first = node;
} // 删除一个头结点,并返回头结点
public Node deleteFirstNode() {
Node tempNode = first;
first = tempNode. next;
return tempNode;
} // 在任意位置插入节点 在index的后面插入
public void add(int index, int data) {
Node node = new Node(data);
Node current = first;
Node previous = first;
while ( pos != index) {
previous = current;
current = current. next;
pos++;
}
node. next = current;
previous. next = node;
pos = 0;
} // 删除任意位置的节点
public Node deleteByPos( int index) {
Node current = first;
Node previous = first;
while ( pos != index) {
pos++;
previous = current;
current = current. next;
}
if(current == first) {
first = first. next;
} else {
pos = 0;
previous. next = current. next;
}
return current;
} // 根据节点的data删除节点(仅仅删除第一个)
public Node deleteByData( int data) {
Node current = first;
Node previous = first; //记住上一个节点
while (current. data != data) {
if (current. next == null) {
return null;
}
previous = current;
current = current. next;
}
if(current == first) {
first = first. next;
} else {
previous. next = current. next;
}
return current;
} // 显示出所有的节点信息
public void displayAllNodes() {
Node current = first;
while (current != null) {
current.display();
current = current. next;
}
System. out.println();
} // 根据位置查找节点信息
public Node findByPos( int index) {
Node current = first;
if ( pos != index) {
current = current. next;
pos++;
}
return current;
} // 根据数据查找节点信息
public Node findByData( int data) {
Node current = first;
while (current. data != data) {
if (current. next == null)
return null;
current = current. next;
}
return current;
}
} package com.tyxh.link;
//测试类
public class TestLinkList {
public static void main(String[] args) {
LinkList linkList = new LinkList();
linkList.addFirstNode(20);
linkList.addFirstNode(21);
linkList.addFirstNode(19);
//19,21,20
linkList.add(1, 22); //19,22,21,20
linkList.add(2, 23); //19,22,23,21,20
linkList.add(3, 99); //19,22,23,99,21,20
linkList.displayAllNodes();
// Node node = linkList.deleteFirstNode();
// System.out.println("node : " + node.data);
// linkList.displayAllNodes();
// node = linkList.deleteByPos(2);
// System.out.println("node : " + node.data);
// linkList.displayAllNodes();
// linkList.deleteFirstNode();
Node node = linkList.deleteByData(19);
// Node node = linkList.deleteByPos(0);
System. out.println( "node : " + node. data);
linkList.displayAllNodes();
Node node1 = linkList.findByPos(0);
System. out.println( "node1: " + node1. data);
Node node2 = linkList.findByData(22);
System. out.println( "node2: " + node2. data);
}
}

参考原文  http://blog.csdn.net/tayanxunhua/article/details/11100097

用java简单的实现单链表的基本操作的更多相关文章

  1. PHP单链表的基本操作

    链表的实现 数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个X 上代 ...

  2. 用Java实现单链表的基本操作

    笔试题中经常遇到单链表的考题,下面用java总结一下单链表的基本操作,包括添加删除节点,以及链表转置. package mars; //单链表添加,删除节点 public class ListNode ...

  3. java基础之自定义单链表练习

    一.单链表 1.单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置), ...

  4. 单链表的基本操作--c++

    #include <iostream> //实现单链表的建立,测长和打印 #include <string> using namespace std; struct node ...

  5. 单链表及基本操作(C语言)

    #include <stdio.h> #include <stdlib.h> /** * 含头节点单链表定义及基本操作 */ //基本操作函数用到的状态码 #define TR ...

  6. 【C++/数据结构】单链表的基本操作

    #pragma once #ifndef _CLIST_H_ #define _CLIST_H_ #include <iostream> #include <assert.h> ...

  7. java单链表常用操作

    总结提高,与君共勉 概述. 数据结构与算法亘古不变的主题,链表也是面试常考的问题,特别是手写代码常常出现,将从以下方面做个小结 [链表个数] [反转链表-循环] [反转链表-递归] [查找链表倒数第K ...

  8. (java实现)单链表

    什么是单链表 在了解单链表之前,你知道什么是链表吗?如果你不知道什么是链表,可以看看我的这篇博客<链表-LinkList> 单链表是链表的其中一种基本结构.一个最简单的结点结构如图所示,它 ...

  9. Java实现单链表的各种操作

    Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素   4.实现链表的反转   5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...

随机推荐

  1. 12.从上往下遍历二元树[LevelOrderOfBinaryTree]

    [题目] 输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印. 例如输入 8    /  \   6    10  /\     /\ 5  7   9  11 输出8    ...

  2. JS Replace 全部替换字符 用法

    转载自:http://www.cnblogs.com/skykang/archive/2011/08/04/2127158.html <script language="javascr ...

  3. HDU 2082 母函数模板题

    找单词 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  4. CodeForces - 427B (模拟题)

    Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  5. MVC系统学习3—ModelBinder

    在ASP.NET MVC中,每个请求都被映射到一个Action方法,我们可以在action的方法中定义相应类型的参数,View中通过post.get方式提交的request参数,只要名称一致就会对应到 ...

  6. iOS开发网络篇—网络请求(HTTP协议)小结(转)

    1. 聊一下HTTP协议(协议的完整的通信过程) 2.通信过程 1> 请求 * 客户端 --> 服务器 * 请求的内容 a. 请求行(请求方法\HTTP协议\请求资源路径) b. 请求头( ...

  7. 最简单的ngResource使用样码

    用来实现基本的RESTful风格的API. 后端用django rest_framework(DRF),前端用AngularJS. app.js var prismVersion = angular. ...

  8. 错误解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file

    转自:http://blog.csdn.net/david_xtd/article/details/7625626 前提:ubuntu-debug机器上向SVN提交了pdu-IVT,想在别的普通机器上 ...

  9. android 检测sqlite数据表中字段(列)是否存在 (转)

    原文摘自 http://www.tuicool.com/articles/jmmMnu 一般数据库升级时,需要检测表中是否已存在相应字段(列),因为列名重复会报错.方法有很多,下面列举2种常见的方式: ...

  10. html 绘图渐变和图片填充

    包括线性渐变和径向渐变 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&qu ...