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

package mars;

//单链表添加,删除节点
public class ListNode { private Node head; public ListNode(){
head=null;
}
//在链表前添加节点
public void addpre(int dvalue){
Node n=new Node(dvalue); if(head==null){
head=n;
}else{
n.next=head;
head=n;
} }
//在链表后添加节点
public void add(int dvalue){
Node n=new Node(dvalue);
Node current = head;
while(current!=null){
if(current.next==null){
current.next=n;
return;
}
current=current.next;
} }
//删除值为dvalue的节点
public Node delete(int dvalue){
Node current=head;
if(head.value==dvalue){
head=head.next;
return current;
}
while(current.next!=null){
if(current.next.value==dvalue){
Node temp=current.next;
current.next=temp.next;
return temp;
}
current=current.next;
}
return null;
}
//删除特定位置的节点
public Node deletepos(int pos){
Node current=head;
int counter=0;
if(pos==0){
head=head.next;
return current;
}
while(current!=null){
if((counter==(pos-1))&&(current.next!=null)){
Node temp=current.next;
current.next=temp.next;
return temp;
}
current=current.next;
counter++;
} return null; }
//单链表转置
public void reverse(){
Node a=head;
if(a==null){
return ;
}
Node b=head.next;
if(b==null){
return ;
}
Node c=head.next.next;
a.next=null;
while(c!=null){
b.next=a;
a=b;
b=c;
c=c.next;
}
b.next=a;
head=b;
}
//输出链表信息
public void print(){
Node current = head;
while(current!=null){
System.out.println(current.value);
current=current.next;
} } public static void main(String[] args){ ListNode l=new ListNode();
l.addpre(3);
l.addpre(2);
l.addpre(1);
l.add(7);
l.add(8);
l.add(9);
l.delete(1);
l.deletepos(4);
l.reverse();
l.print();
} } class Node{
public Node next;
public int value;
public Node(){
next=null;
}
public Node(int v){
value=v;
}
}

PS:Java的引用类似于C的指针,例如 :

Node n1=new Node(1);    Node n2=n1;    Node n3=new Node(3);   n2=n3;

执行n2=n1后,n1和n2都是对同一块内存区域(区域1)的引用,通过n1和n2都可以达到修改内存区域1的目的,例如执行n1.value=10后,输出n2.value的值也为10。但是执行n2=n3后,n2则变为了对另一块内存区域(区域3)的应用。

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

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

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

  2. 数据结构——Java实现单链表

    一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...

  3. (java实现)单链表

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

  4. java实现单链表的增删功能

    JAVA 实现单链表的增删功能 package linked; class LinkedTable{ } public class LinkedTableTest { public static vo ...

  5. PHP单链表的基本操作

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

  6. JAVA数据结构——单链表

    链表:一. 顺序存储结构虽然是一种很有用的存储结构,但是他有如下几点局限性:1. 因为创造线性表的时候已经固定了空间,所以当需要扩充空间时,就需要重新创建一个地址连续的更大的存储空间.并把原有的数据元 ...

  7. 使用java实现单链表(转载自:https://www.cnblogs.com/zhongyimeng/p/9945332.html)

    使用java实现单链表----(java中的引用就是指针)转载自:https://www.cnblogs.com/zhongyimeng/p/9945332.html ? 1 2 3 4 5 6 7 ...

  8. 用java简单的实现单链表的基本操作

    package com.tyxh.link; //节点类 public class Node { protected Node next; //指针域 protected int data;//数据域 ...

  9. java实现单链表常见操作

    一.概述: 本文主要总结单链表常见操作的实现,包括链表结点添加.删除:链表正向遍历和反向遍历.链表排序.判断链表是否有环.是否相交.获取某一结点等. 二.概念: 链表: 一种重要的数据结构,HashM ...

随机推荐

  1. C#中 导入和导出Excel的方法

    using System.Data; using System.Data.OleDb; /// <summary> /// Excel转为DataTable /// </summar ...

  2. FL2440驱动添加(2): RTC(Real time clock)

    一,Linux下的时间分为两种,系统时间与硬件时间(RTC芯片): 1,系统时间就是运行系统能够直接看到的时间: 2,硬件时间就是RTC芯片中的时间,断电任然有电池供电: linux系统开机时,会从R ...

  3. DP入门---Robberies

    HDU  2955 Description The aspiring Roy the Robber has seen a lot of American movies, and knows that ...

  4. 泛函编程(19)-泛函库设计-Parallelism In Action

    上节我们讨论了并行运算组件库的基础设计,实现了并行运算最基本的功能:创建新的线程并提交一个任务异步执行.并行运算类型的基本表达形式如下: import java.util.concurrent._ o ...

  5. [moka同学收藏]Yii2.0 rules验证规则

    required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...

  6. svn 大全

    环境:Win7 32 bit SVN简介:程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是一 ...

  7. 硅谷新闻8--TabLayout替换ViewPagerIndicator

    1.关联库 compile 'com.android.support:design:23.3.0' 2.布局写上TabLayout <android.support.design.widget. ...

  8. HTML JavaScript简介

    一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...

  9. [Design Pattern] Substitute Interface

    [Design Pattern] Substitute Interface 目的 将对象的成员建立为替身接口的成员,用来解耦对象之间的循环相依. 情景 假设开发人员接手一个系统,在系统里有订单对象.送 ...

  10. SharePoint 2013 新功能探索 之 标注控件

    SharePoint 2013 引入了新的UI,同时也跟进了网络潮流,把应用最广泛的标注控件也引入到了SharePoint,先看两个应用    以上是两个开发当中经常会用到,下面就介绍一下如何开发相同 ...