java单向链表代码 单链表的基本操作java-成都快上网建站

java单向链表代码 单链表的基本操作java

求一个JAVA的单链表程序

package OS;

创新互联公司专注于网站建设|成都网站维护公司|优化|托管以及网络推广,积累了大量的网站设计与制作经验,为许多企业提供了网站定制设计服务,案例作品覆盖门窗定制等行业。能根据企业所处的行业与销售的产品,结合品牌形象的塑造,量身建设品质网站。

public class IntNode

{

public String name;

//public int run_time=44;

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;

}

}

package OS;

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)

head=tail=null;

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.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("请输入节点数据:");

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 ji) {

p = p.nextNode;

j++;

}

if( p == null || ji) {

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 ji-1) {

p = p.nextNode;

j++;

}

if( p == null || ji-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.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;

} else {

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单向链表代码 单链表的基本操作java
路径分享:http://kswjz.com/article/hejhii.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流