网站首页 > 文章中心 > 其它

java单向链表代码

作者:小编 更新时间:2023-10-20 13:09:27 浏览量:192人看过

求一个JAVA的单链表程序

package OS;

public class IntNode

{

public String name;

public int run_time=(int)(Math.random()*100);

public IntNode next;

java单向链表代码-图1

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;

java单向链表代码-图2

next=nn;

public class IntSLList

public IntNode head;//头尾"指针"

public IntNode tail;

public IntSLList(){

head=tail=null;

java单向链表代码-图3

//判别链表是否为空

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;

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.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单向链表

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单向链表代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章