java 单向链表_java中单向链表用的多吗

展开全部

java中单向链表用的多.

这是我写的一个差不多,你看一下吧:

package com.test.list;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class LinkedList {

public static void main(String[] args) {

MyList l = new MyList();

MyListNode node = l.createList();

l.printNode(node);

//l.searchNode(node, 4);

//node = l.insertNode(node, 3, "g");

//l.printNode(node);

node = l.deleteNode(node, "d");

l.printNode(node);

}

}

class MyListNode {

public String data;

public MyListNode nextNode;

}

class MyList {

public MyListNode createList() {

MyListNode node = new MyListNode();

MyListNode q ,p;

q = new MyListNode();

q = node;

while (true) {

String s = null;

try {

BufferedReader br = new BufferedReader(new InputStreamReader(

System.in));

System.out.println("请输入32313133353236313431303231363533e58685e5aeb931333363373230节点数据:");

s = br.readLine();

if (s.equals("0")) {

break;

} else {

p = new MyListNode();

p.data = s;

p.nextNode = null;

q.nextNode = p;

q = p;

}

} catch (Exception e) {

e.printStackTrace();

}

}

return node;

}

public void printNode(MyListNode node) {

MyListNode p = node.nextNode;

while (p!= null) {

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

p = p.nextNode;

}

}

public void searchNode(MyListNode node, int i){

MyListNode p = node.nextNode;

int j = 1;

while (p != null && j

p = p.nextNode;

j++;

}

if( p == null || j>i) {

System.out.println("error");

}

System.out.println(" --"+p.data+"--");

}

public MyListNode insertNode(MyListNode node, int i ,String s) {

MyListNode p = node.nextNode;

int j = 1;

while (p != null && j

p = p.nextNode;

j++;

}

if( p == null || j>i-1) {

System.out.println("error");

}

MyListNode n = new MyListNode();

n.data = s;

n.nextNode = p.nextNode;

p.nextNode = n;

return node;

}

public MyListNode deleteNode(MyListNode node ,String s) {

MyListNode p = node;

while(p.nextNode != null && !p.nextNode.data.equals(s)) {

p = p.nextNode;

}

p.nextNode = p.nextNode.nextNode;

return node;

}

}

/*逆位序创建

public MyListNode createList() {

MyListNode node = new MyListNode();

node.nextNode = null;

while(true) {

String s = null;

try {

BufferedReader br = new BufferedReader(new InputStreamReader(

System.in));

System.out.println("请输入节点数据:");

s = br.readLine();

if(s.equals("0")) {

break;

}else {

MyListNode n = new MyListNode();

n.data = s;

n.nextNode = node.nextNode;

node.nextNode = n;

}

} catch (Exception e) {

e.printStackTrace();

}

}

return node;

}

*/

Java单向链表是一种常见的数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。单向链表的特点是只能从头节点开始遍历到尾节点,不能反向遍历。 下面是一个简单的Java实现单向链表的例子: ```java public class Node { int data; // 节点数据 Node next; // 指向下一个节点的指针 public Node(int data) { this.data = data; this.next = null; } } public class LinkedList { Node head; // 链表头节点 public LinkedList() { this.head = null; } // 在链表尾部添加节点 public void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } // 在链表查找某个节点是否存在 public boolean contains(int data) { Node current = head; while (current != null) { if (current.data == data) { return true; } current = current.next; } return false; } // 删除链表第一个出现的指定节点 public void remove(int data) { if (head == null) { return; } if (head.data == data) { head = head.next; return; } Node current = head; while (current.next != null) { if (current.next.data == data) { current.next = current.next.next; return; } current = current.next; } } // 获取链表的节点数 public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.next; } return count; } } ``` 在上面的代码,Node类表示链表节点,LinkedList类表示单向链表。它包含了添加、查找、删除和获取节点数等常见操作。你可以根据自己的实际需求来对链表进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值