「java单链表删除节点」Java链表删除

博主:adminadmin 2022-12-21 04:51:08 59

本篇文章给大家谈谈java单链表删除节点,以及Java链表删除对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java语言没有指针,怎样实现链表?

Java语言中的对象引用实际上是一个指针(这里的指针均为概念上的意义,而非语言提供的数据类型),所以我们可以编写这样的类来实现链表中的结点。

程序代码:

class Node 

Object data; 

Node next;//指向下一个结点 

}

将数据域定义成Object类是因为Object类是广义超类,任何类对象都可以给其赋值,增加了代码的通用性。为了使链表可以被访问还需要定义一个表头,表头必须包含指向第一个结点的指针和指向当前结点的指针。为了便于在链表尾部增加结点,还可以增加一指向链表尾部的指针,另外还可以用一个域来表示链表的大小,当调用者想得到链表的大小时,不必遍历整个链表。

链表的数据结构我们可以用类List来实现链表结构,用变量Head、Tail、Length、Pointer来实现表头。存储当前结点的指针时有一定的技巧,Pointer并非存储指向当前结点的指针,而是存储指向它的前趋结点的指针,当其值为null时表示当前结点是第一个结点,因为当删除当前结点后仍需保证剩下的结点构成链表,如果Pointer指向当前结点,则会给操作带来很大困难。如何得到当前结点呢?我们定义了一个方法cursor(),返回值是指向当前结点的指针。类List还定义了一些方法来实现对链表的基本操作,通过运用这些基本操作我们可以对链表进行各种操作。例如reset()方法使第一个结点成为当前结点。insert(Objectd)方法在当前结点前插入一个结点,并使其成为当前结点。remove()方法删除当前结点同时返回其内容,并使其后继结点成为当前结点,如果删除的是最后一个结点,则第一个结点变为当前结点。

链表类List的源代码如下:

package  cn.javatx; import  java.io.IOException;/**

*  @author  ljfan

*  

*/

public  class  List  {

private  Node  Head  =  null;

private  Node  Tail  =  null;

private  Node  Pointer  =  null;

private  int  Length  =  0;public  void  deleteAll()  {

Head  =  null;

Tail  =  null;

Pointer  =  null;

Length  =  0;

}public  void  reset()  {

Pointer  =  null;

}public  boolean  isEmpty()  {

return  (Length  ==  0);

}public  boolean  isEnd()  {

if  (Length  ==  0)

throw  new  java.lang.NullPointerException();

else  if  (Length  ==  1)

return  true;

else

return  (cursor()  ==  Tail);

}public  Object  nextNode()  {

if  (Length  ==  1)

throw  new  java.util.NoSuchElementException();

else  if  (Length  ==  0)

throw  new  java.lang.NullPointerException();

else  {

Node  temp  =  cursor();

Pointer  =  temp;

if  (temp  !=  Tail)

return  (temp.next.data);

else

throw  new  java.util.NoSuchElementException();

}

}public  Object  currentNode()  {

Node  temp  =  cursor();

return  temp.data;

}public  void  insert(Object  d)  {

Node  e  =  new  Node(d);

if  (Length  ==  0)  {

Tail  =  e;

Head  =  e;

}  else  {

Node  temp  =  cursor();

e.next  =  temp;

if  (Pointer  ==  null)

Head  =  e;

else

Pointer.next  =  e;

}

Length++;

}public  int  size()  {

return  (Length);

}public  Object  remove()  {

Object  temp;

if  (Length  ==  0)

throw  new  java.util.NoSuchElementException();

else  if  (Length  ==  1)  {

temp  =  Head.data;

deleteAll();

}  else  {

Node  cur  =  cursor();

temp  =  cur.data;

if  (cur  ==  Head)

Head  =  cur.next;

else  if  (cur  ==  Tail)  {

Pointer.next  =  null;

Tail  =  Pointer;

reset();

}  else

Pointer.next  =  cur.next;

Length--;

}

return  temp;

}private  Node  cursor()  {

if  (Head  ==  null)

throw  new  java.lang.NullPointerException();

else  if  (Pointer  ==  null)

return  Head;

else

return  Pointer.next;

}public  static  void  main(String[]  args)  {

List  a  =  new  List();

for  (int  i  =  1;  i  =  10;  i++)

a.insert(new  Integer(i));

System.out.println(a.currentNode());

while  (!a.isEnd())

System.out.println(a.nextNode());

a.reset();

while  (!a.isEnd())  {

a.remove();

}

a.remove();

a.reset();

if  (a.isEmpty())

System.out.println("There  is  no  Node  in  List  n");

System.out.println("You  can  press  return  to  quitn");

try  {

System.in.read();

}  catch  (IOException  e)  {

}

}

}class  Node  {

Object  data;

Node  next;Node(Object  d)  {

data  =  d;

next  =  null;

}

}

当然,双向链表基本操作的实现略有不同。链表和双向链表的实现方法,也可以用在堆栈和队列的现实中。

在单链表中删除最小值结点,写出函数.(JAVA)

package testonly;

/**

 * 删除单链表中所有最小的节点

 * @author baidu an0011121

 *

 */

public class LinkedListDemo {

        /**

         * 链表节点结构,直接在这里表示了,为了贴出来代码,你可以单独把这个节点类放到一个java文件中

         * @author Administrator

         *

         */

        public static class Node {

                public int data;

                public Node next;

        }

        /**

         * 核心方法,删除单链表中所有的最小的节点

         * @param head

         */

        public static void delMin(Node head) {

                //寻找最小

                Node current = head.next;

                int min = current.data;

                while (null != current) {

                        if (current.data  min) {

                                min = current.data;

                        }

                        current=current.next;

                }

                //删除最小

                Node previous = head;

                current = head.next;

                //两层循环为了删除所有最小

                while(null!=current){

                        while (current.data != min) {

                                previous = current;

                                current = current.next;

                        }

                        previous.next=current.next;

                        current=previous.next;

                }

        }

        /**

         * 打印链表

         * @param head

         */

        public static void print(Node head){

                String printStr="";

                Node p=head.next;

                while(null!=p){

                        printStr+=p.data+"--";

                        p=p.next;

                }

                System.out.println(printStr.substring(0,printStr.length()-3));

        }

        /**

         * 构造链表

         * @param arr

         * @return

         */

        public static Node construct(int[] arr){

                Node headNode=new Node();

                Node p=headNode;

                for(int a:arr){

                        Node dataNode=new Node(); 

                        dataNode.data=a;

                        dataNode.next=null;

                        p.next=dataNode;

                        p=dataNode;

                }

                return headNode;

        }

        /**

         * 主方法

         * @param args

         */

        public static void main(String[] args) {

                int [] arr=new int[]{6,3,5,2,9,7,6,4,3,2,6,6,9,6,3,4,6,5,2};

                //构造

                Node head=construct(arr) ;

                //删除最小

                delMin(head);

                //打印结果

                print(head);

        }

}

以上是刚写出来的。按照你的要求构造了单链表,然后测试了删除最小节点的方法。如果不符合你的要求或者哪块代码不明白,可以追问或者私信我。

java 编程题目 数组模拟链表 如何删除节点

给你一段可以跑到代码 链表完成 约瑟夫问题

/**

* 功能:josephu问题(丢手绢)

* 目的:面向对象编程思想

*/

public class Josephu {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

CycLink cycLink = new CycLink();

cycLink.setLen(9);

cycLink.setK(2);

cycLink.setM(5);

cycLink.creatLink();

cycLink.play();

}

}

/**

* 小孩类

* @author tenglian

*/

class Child{

int no;

Child nextChild; //指向下一个小孩

Child previousChild; //指向上一个小孩

public Child(int no){

this.no = no;

}

}

/**

* 环形链表类

*/

class CycLink{

int len = 0;//小孩的总数

Child firstChild = null;

Child temp = null;

//从第几个小孩开始

int k;

//数到第几个小孩

int m;

//创建环形链表,让小孩围坐一圈

public void creatLink(){

for(int i = 1; i = len; i++){

//如果是第一个小孩 将第一个小孩锁定

if(i == 1){

Child ch = new Child(i);

firstChild = ch;

temp = ch;

} else {

//如果这是最后一个小孩

if(i == len){

Child ch = new Child(i);

temp.nextChild = ch;

temp = ch;

temp.nextChild = this.firstChild;

} else {

//否则继续创建小孩

Child ch = new Child(i);

temp.nextChild = ch;

temp = ch;

}

}

}

}

/**

* 开始play(玩丢手绢游戏)

*/

public void play(){

Child temp = null;//跑龙套1

Child temp2 = null;//跑龙套2

Child temp3 = null;//跑龙套3

//第一步,找到开始数数的小孩——k

temp = this.firstChild;

for(int i = 1; i k; i ++){

temp = temp.nextChild;

}

//从第二步开始循环

while(len 0){

//第二步 找到数到m的小孩

for(int j = 1; j m; j ++){

if(m - j == 1){

temp3 = temp;

}

temp2 = temp.nextChild;

temp = temp2;

}

//第三步,让数到m的小孩出圈

temp3.nextChild = temp2.nextChild;

temp = temp2.nextChild;

len--;

System.out.print(temp2.no + " ");

}

}

public void setLen(int len){

this.len = len;

}

public void setK(int k) {

this.k = k;

}

public void setM(int m) {

this.m = m;

}

}

用Java语言实现单向链表

1.先定义一个节点类

package com.buren;

public class IntNode {

//定义一个节点类

int

info;

//定义属性,节点中的值

IntNode next;

//定义指向下一个节点的属性

public IntNode(int

i){ //构造一个next为空的节点

this(i,null);

}

public IntNode(int i,IntNode

n){ //构造值为i指向n的节点

info=i;

next=n;

}

}

2.再定义一个链表类,这是主要部分

package com.buren;

public class IntSLList {

private IntNode head,tail;

//定义指向头结点和尾结点的指针,

//如果大家看着这个不像指针的话,那就需要对指针有更深刻的了解

public

IntSLList(){

//定义一个空节点

head=tail=null;

}

public boolean

isEmpty(){

//判断节点是否为空

return

head==null;

//这行代码看起来似乎很神奇,其实真的很神奇,偶是服了

}

public void addToHead(int el){

//将el插入到头结点前

head=new

IntNode(el,head);

//将节点插入到头结点前,作为新的投节点

if(head==tail){

//给空链表插入节点时

tail=head;

//头结点和尾结点指向同一个节点

}

}

public void addToTail(int

el){

//向链表的尾部增加结点

if(!isEmpty()){

//判断链表是否为空

tail.next=new

IntNode(el);

//新建立一个值为el的节点,将链表的尾结点指向新节点

tail=tail.next;

//更新尾指针的指向

}else{

head=tail=new

IntNode(el);

//如果链表为空,新建立一个节点,将头尾指针同时指向这个节点

}

}

public int

deleteFromHead(){

//删除头结点,将节点信息返回

int

el=head.info;

//取出节点信息

if(head==tail){

//如果链表中只有一个节点

head=tail=null;

//删除这一个节点

}else{

head=head.next;

//如果链表中不止一个节点,将头结点的下一个节点作为头结点

}

return

el;

//返回原头结点的值

}

public int

deleteFromTail(){

//删除尾结点,返回尾结点的信息

int

el=tail.info;

//取出尾结点的值

if(head==tail){

// 如果链表中只有一个节点

head=tail=null;

//删除这个节点

}else{

IntNode

temp;

//定义中间变量

for(temp=head;temp.next!=tail;temp=temp.next);

//找出尾结点的前一个节点,注意最后的分号,

//这个for循环是没有循环体的,目的在于找出尾结点的前一个节点

//在整个程序中用了很多次这样的写法,相当经典啊

tail=temp;

//将找出来的节点作为尾结点,删除原来的尾结点

tail.next=null;

//将新尾结点的指向设为空

}

return

el;

//返回原尾结点的信息

}

public void

printAll(){

//打印链表中所有节点的信息

if(isEmpty()){

//如果链表为空

System.out.println("This

list is

empty!");

//输出提示信息

return;

//返回到调用的地方

}

if(head==tail){

//当链表中只有一个节点时

System.out.println(head.info);

//输出这个节点的信息,就是头结点的信息

return;

}

IntNode

temp;

//定义一个中间变量

for(temp=head;temp!=null;temp=temp.next){

//遍历整个链表

System.out.print(temp.info+"

");

//输出每个节点的信息

}

System.out.println();

//输出一个换行,可以没有这一行

}

public boolean isInList(int

el){

//判断el是否存在于链表中

IntNode

temp;

//定义一个中间变量

for(temp=head;temp!=null

temp.info!=el;temp=temp.next);

//将el找出来,注意最后的分

return

temp!=null;

// 如果存在返回true,否则返回flase,这两行代码很有思想

}

public void delete(int

el){

//删除链表中值为el的节点

if(head.info==el

head==tail){

//如果只有一个节点,并且节点的值为el

head=tail=null;

//删除这个节点

}else

if(head.info==el){

// 不止一个节点,而头结点的值就是el

head=head.next;

//删除头结点

}else{

IntNode

pred,temp;

//定义两个中间变量

for(pred=head,temp=head.next;temp.info!=el

temp.next!=null;pred=pred.next,temp=temp.next);

//跟上面的类似,自己琢磨吧,也是要注意最后的分号

pred.next=temp.next;

//将temp指向的节点删除,最好画一个链表的图,有助于理解

if(temp==tail){

//如果temp指向的节点是尾结点

tail=pred;

//将pred指向的节点设为尾结点,

}

}

}

//下面这个方法是在链表中值为el1的节点前面插入一个值为el2的节点,

//用类似的思想可以再写一个在链表中值为el1的节点后面插入一个值为el2的节点

public boolean insertToList(int el1,int

el2){

//定义一个插入节点的方法,插入成功返回true,否则返回false

IntNode

pred,temp; //定义两个中间变量

if(isEmpty()){

//判断链表是否为空

return

false;

//如果链表为空就直接返回false

}

if(head.info==el1

head==tail){

//如果链表中只有一个节点,并且这个节点的值是el1

head=new

IntNode(el2,head);

//新建立一个节点

return

true;

}else if(head.info==el1){

IntNode t=new

IntNode(el2);

t.next=head;

head=t;

return

true;

}else{

for(pred=head,temp=head.next;temp!=null

temp.info!=el1;pred=pred.next,temp=temp.next);

if(temp!=null){

IntNode

a=new IntNode(el2);

pred.next=a;

a.next=temp;

return

true;

}else{

System.out.println(el1+"

NOT EXEISTS!");

return

false;

}

}

}

3.下面是测试代码

public static void main(String[] args){

IntSLList test=new

IntSLList();

//test.addToHead(7);

test.addToTail(7);

System.out.println(test.insertToList(7,5));

test.printAll();

System.out.println(test.isInList(123));

}

}

用java编写程序实现单链表,要提供插入,删除,排序,统计等功能,链表节点中的数据要求是整数。

public class Link {

Node head = null;

Node point = null;

Node newNode = null;

public int Count = 0;//统计值

//插入

public void AddNode(int t) {

newNode = new Node();

if (head == null) {

head = newNode;

} else {

point = head;

while (point.next != null) {

point = point.next;

}

point.next = newNode;

}

point = newNode;

point.vlaue = t;

point.next = null;

Count++;

}

//返回值

public int GetValue(int i) {

if (head == null || i 0 || i Count)

return -999999;

int n;

Node temp = null;

point = head;

for (n = 0; n = i; n++) {

temp = point;

point = point.next;

}

return temp.vlaue;

}

//删除

public void DeleteNode(int i) {

if (i 0 || i Count) {

return;

}

if (i == 0) {

head = head.next;

} else {

int n = 0;

point = head;

Node temp = point;

for (n = 0; n i; n++) {

temp = point;

point = point.next;

}

temp.next = point.next;

}

Count--;

}

//排序

public void Sotr() {

for (Node i = head; i != null; i = i.next) {

for (Node j = i.next; j != null; j = j.next) {

if (i.vlaue j.vlaue) {

int t = i.vlaue;

i.vlaue = j.vlaue;

j.vlaue = t;

}

}

}

}

}

class Node {

int vlaue;

Node next;

}

java单链表删除节点的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java链表删除、java单链表删除节点的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。