package OS;
public class IntNode
{
public String name;
public int run_time=(int)(Math.random()*100);
public IntNode next;
public int num;
public IntNode(int n,String n1){
this(n,n1,null);
}
public IntNode(int n,String n1,IntNode nn){
num=n;
name=n1;
next=nn;
public class IntSLList
public IntNode head;//头尾"指针"
public IntNode tail;
public IntSLList(){
head=tail=null;
//判别链表是否为空
public boolean isEmpty(){
return head==null;
//从链表头部添加结点————此处的函数的传递参数是一个数值,也就是info
public void addToHead(IntNode some){
some.next=head;
head=some;
if(tail==null)
tail=head;
//从链表的尾部添加结点————同上
public void addToTail(IntNode some){
if(!isEmpty())
tail.next=some;
tail=tail.next;
else
head=tail=some;
//从链表头开始删除
public void deleteFromHead(){
if(head==tail)
else{
head=head.next;
这是我写的一个差不多,你看一下吧:
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.printNode(node);
node = l.deleteNode(node, "d");
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("请输入节点数据:");
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){
int j = 1;
while (p != null ji) {
j++;
if( p == null || ji) {
System.out.println("error");
System.out.println(" --"+p.data+"--");
public MyListNode insertNode(MyListNode node, int i ,String s) {
while (p != null ji-1) {
if( p == null || ji-1) {
MyListNode n = new MyListNode();
n.data = s;
n.nextNode = p.nextNode;
p.nextNode = n;
public MyListNode deleteNode(MyListNode node ,String s) {
MyListNode p = node;
while(p.nextNode != null !p.nextNode.data.equals(s)) {
p.nextNode = p.nextNode.nextNode;
/*逆位序创建
node.nextNode = null;
while(true) {
if(s.equals("0")) {
}else {
n.nextNode = node.nextNode;
node.nextNode = n;
*/
java.util.Linkedlist是双向链表,当然也就包括了单链表的功能,你可以去看他怎么写的啊
public class SingleLinkedListE {
private EntryE first, last;
private int size = 0;
public void add(E element) {
EntryE newEntry = new EntryE(element, null);
if (first == null) {
first = last = newEntry;
last.next = newEntry;
last = newEntry;
++size;
public E get(int index) {
if (index 0 || index = size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
EntryE e = first;
for (int i = 0; i index; ++i)
e = e.next;
return e.data;
private static class EntryE {
Entry(E data, EntryE next) {
this.data = data;
this.next = next;
E data;
EntryE next;
以上就是土嘎嘎小编为大家整理的java单向链表代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!