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

笔试题中经常遇到单链表的考题,下面用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)的应用。

时间: 2024-08-19 01:08:15

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

数据结构复习--java实现单链表基本操作

单链表的基本操作包括建立单链表.查找运算(按序查找和按值查找).插入运算(前插和后插)和删除运算.下面给出具体的java实现程序: package com.zpp.test; //首先创建一个节点类 public class Node { private Node next; //指针域 private int data;//数据域 public Node( int data) { this. data = data; } } package com.zpp.test; public class

(java实现)单链表

什么是单链表 在了解单链表之前,你知道什么是链表吗?如果你不知道什么是链表,可以看看我的这篇博客<链表-LinkList> 单链表是链表的其中一种基本结构.一个最简单的结点结构如图所示,它是构成单链表的基本结点结构.在结点中数据域用来存储数据元素,指针域用于指向下一个具有相同结构的结点. 因为只有一个指针结点,称为单链表. 单链表中三个概念需要区分清楚:分别是头指针,头节点和首元节点. 头结点.头指针和首元结点(此段转自@ciyeer大牛的博客) 头结点:有时,在链表的第一个结点之前会额外增设

java实现单链表

前面已经介绍了java如何实现顺序链表:http://www.cnblogs.com/lixiaolun/p/4643664.html 接下来,我们开始学习java实现单链表. 单链表类 package linklist; public class LinkList { class Element { public Object value=null; private Element next=null; } private Element header = null;//头结点 /** * 初

java 实现单链表的逆序

</pre><pre name="code" class="java">package com.ckw.mianshi; /** * java 实现单链表的逆序 * @author Administrator * */ public class SingleLinkedReverse { class Node{ int data; Node next; public Node(int data){ this.data = data; } }

JAVA 实现单链表

1 public class LinkNode { 2 public String data; 3 public LinkNode next; 4 5 LinkNode(){ 6 this.data = "a"; 7 this.next = null; 8 } 9 10 LinkNode(String string){ 11 this.data = string; 12 this.next = null; 13 } 14 } 1 public class LinkList { 2 3

Java 实现单链表反序

//单链表反序 public class SingleLinkedListReverse { public static void main(String[] args) { Node head = new Node(0); Node temp = null; Node cur = null; for (int i = 1; i <= 10; i++) { temp = new Node(i); if (i == 1) { head.setNext(temp); } else { cur.set

单链表的基本操作实现

1.写这篇博文的原因 C语言有三个重要部分:流程控制.函数.指针. 对于指针,单单了解它的简单运用是远远不够的,最重要的是学习链表.所以这篇文章通过用C语言实现链表的一些基本操作和总结,希望对C语言的指针有更新的理解和认识. 2.单链表的定义 单链表是通过一组任意的存储单元来存储线性表中的数据元素,这些存储单元可以是连续的也可以是不连续的.为了建立起数据元素之间的关系,对于每个数据元素除了存放元素自身的信息外,还必须有包含指向该元素直接后继元素位置的信息,这两部分信息组成一个节点,即每个节点都有

《实验课---单链表的基本操作》

//线性表---链表的基本操作#include<stdio.h>#include<stdlib.h>#include<conio.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define NULL 0typedef int Status; //函数结果状态代码typedef int ElemType; //数据元素类型//------线性表的单链表存储结构

用java实现单链表

对于一个单链表来说,要求有最基本的数据节点以及一些重要的方法. 方法应该有增删改查.定位.输出.获取链表长度.排序.链表读入.链表输出.下面是我用java写的单链表 public class List { public class Node{//定义节点 public int data; public Node next; public Node(int data){ this.data = data; } } private Node head;//头节点 public Node getHea