java代码单链表 java 单向链表

用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如何实现单链表

/**

* 结点类

*/

private static class NodeT {

T nodeValue; // 数据域

NodeT next; // 指针域保存着下一节点的引用

Node(T nodeValue, NodeT next) {

this.nodeValue = nodeValue;

this.next = next;

}

Node(T nodeValue) {

this(nodeValue, null);

}

}

求一个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如何创建一个单链表和双链表

单向链表

单向链表就是通过每个结点的指针指向下一个结点从而链接起来的结构。

单向链表的初始化:这里我所讲的链表都是头结点不参与计算的,也就是说第一个结点都是头结点后面的第一个结点。所以我要先申明一点,这里我把链表的初始化放在了构造函数部分,然后析构函数负责释放头结点的内存。

单向链表的创建过程:链表的创建就是添加结点到链表的最后,开始是添加一个结点到head结点后面,然后添加一个结点到上次添加的结点后面,每次新建的结点的指针总是指向NULL指针。从上面的示意图可以看出,我们需要一个辅助指针一直指向最后一个结点,这个辅助结点就是为了让每次添加的结点都放置在最后一个位置。

单向链表插入结点过程:源代码中的的插入结点函数我设置了一个指定位置,就是在指定位置插入结点。首先,通过位置变量position让ptemp结点移动到要插入位置的前一个位置,然后接下来的过程就是和创建链表的过程是一样的,把新建的结点添加到ptemp的后面。这里变量position可以从1到链表长度加1,意思就是如果不算头结点的话有3个结点,那你的position变量就可以从1到4,这是因为ptemp指针可以到第3个结点的位置,所以新建结点的位置就可以到4了。

单向链表删除结点过程:源代码中的删除结点函数也有一个指定位置变量,为了删除指定位置的结点。和插入结点一样通过变量position把ptemp移动到要删除结点的前一个位置,然后让ptemp结点中的指针指向要删除结点后面的一个结点,也就是ptemp结点的下一个的下一个结点,虽然这个结点可能为空,但是程序还是正常运行。但是这里和插入结点不同的是变量position只能从1到链表的长度,是因为ptemp移动到最后一个结点的时候,它的下一个结点为空,所以不不需要参与删除了。

双向链表

1.听名字可能就能猜到双向链表就是链表结点包含两个指针,一个指针是指向下一个结点的,另一个指针当然就是指向上一个结点的。

2.双向链表的初始化:由于这里的链表头结点不参与计算,所以头结点的pPre指针是一直指向NULL指针的。

3.双向链表的创建过程:由于双向链表的每个结点包含两个指针那么这个时候我们就要小心处理好每一个指针的指向,要不然会有很多意想不到的错误。同样的,和单向链表的创建过程一样,需要一个辅助指针来指向最后一个结点,然后每新建一个结点,这个结点的pNext指针都是指向NULL指针的,pPre指针指向上一个结点(这是和单向链表不同的地方),然后让上一个指针的pNext指向新建的结点,这样整个链表就连接起来了。

4.双向链表插入结点过程:知道了双向链表的创建过程,那么插入结点的过程就大同小异 了,有一点需要特别注意的就是这里的变量position范围也是从1到链表长度加1,但是如果待插入的位置是最后一个位置的话,情况就不同了,看到下面的图我们可以很好的理解,因为没新建一个结点的时候都需要处理两个指针,而且新建结点的下一个结点的pPre指针就需要指向这个新建的结点,但是有可能这个新建的结点可能就已经是最后一个结点了,那么这个时候再执行

ptemp-pNext-pPre = pnew;

这条指令的时候就会报错了,因为ptemp-pNext已经是个NULL指针了,那空指针哪里还有pPre呢。因此在程序中要进行一次判断,看看结点是否是最后一个结点。

5.双向链表删除结点的过程:要注意的问题和插入结点一样,看看这个结点是否为NULL。这里就不重复了。

java数据结构单链表

你的问题很好理解。但是你的代码问题严重。

1、你想用java代码实现还是c代码实现?从你的代码看是c语言

2、第一段代码中的结构体nod是不是应该写成Node?

3、inset函数中,链表L是有变化的,所以要用指针。结点s是不改变的,所以不应该用指针

4、既然s不是用指针,后面的s-next自然也不能这么写了。

用java单链表实现一元多项式相加的算法?

public class Test {

public static void main(String[] args) {

try{

LinkList list1 = new LinkList();

LinkList list2 = new LinkList();

LinkList list3 = null;

list1.addAt(0, new Item(1, 5));

list1.addAt(1, new Item(-1.5, 3));

list1.addAt(2, new Item(1, 1));

list2.addAt(0, new Item(0.5, 5));

list2.addAt(1, new Item(0.5, 4));

list2.addAt(2, new Item(1.5, 3));

list2.addAt(3, new Item(3, 0));

list3 = mergeLinkList(list1, list2);

System.out.println("一元多项式的相加过程:");

list1.listAll();

System.out.println(" + ");

list2.listAll();

System.out.println(" = ");

list3.listAll();

}

catch(Exception e){

e.printStackTrace();

}

}

/**

* 一元多项式的一般项类

*/

class Item{

private double coef;  //一元多项式的一般项的系数

private int exp;   //一元多项式的一般项的指数

public Item(){

this.coef = 0.0;

this.exp = 0;

}

public Item(double coef, int exp){

this.coef = coef;

this.exp = exp;

}

public double getCoef(){

return this.coef;

}

public void setCoef(double coef){

this.coef = coef;

}

public int getExp(){

return this.exp;

}

public void setExp(int exp){

this.exp = exp;

}

}

/**

* 链表结点类

*/

class Node{

private Item data;

private Node next;   //链表结点的指针域,指向直接后继结点

public Node(){

data = null;

next = null;

}

public Node(Item data, Node next){

this.data = data;

this.next = next;

}

public Item getData(){

return this.data;

}

public void setData(Item data){

this.data = data;

}

public Node getNext(){

return this.next;

}

public void setNext(Node next){

this.next = next;

}

}

/**

* 链表类

*/

class LinkList{

private Node head = null; //头结点指针

private int size = 0;

public LinkList(){

head = new Node();

size = 0;

}

//在i位置插入元素elem

public boolean addAt(int i, Item elem) {

if(i 0 || i size){

return false;

}

Node pre,curr;

int pos;

for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());

curr = new Node(elem, pre.getNext());

pre.setNext(curr);

size++;

return true;

}

//删除i位置的元素

public boolean removeAt(int i) {

if(i 0 || i = size){

return false;

}

Node pre,curr;

for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());

curr = pre.getNext();

pre.setNext(curr.getNext());

size--;

return true;

}

java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

当前名称:java代码单链表 java 单向链表
URL分享:https://www.cdcxhl.com/article40/doscjeo.html

成都网站建设公司_创新互联,为您提供网站设计公司移动网站建设静态网站小程序开发网页设计公司营销型网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司