用Java实现单链表的基本操作
笔试题中经常遇到单链表的考题,下面用java总结一下单链表的基本操作,包括添加删除节点,以及链表转置。
package mars; //单链表添加,删除节点
public class ListNode { private Node head; public ListNode(){
head=null;
}
//在链表前添加节点
public void addpre(int dvalue){
Node n=new Node(dvalue); if(head==null){
head=n;
}else{
n.next=head;
head=n;
} }
//在链表后添加节点
public void add(int dvalue){
Node n=new Node(dvalue);
Node current = head;
while(current!=null){
if(current.next==null){
current.next=n;
return;
}
current=current.next;
} }
//删除值为dvalue的节点
public Node delete(int dvalue){
Node current=head;
if(head.value==dvalue){
head=head.next;
return current;
}
while(current.next!=null){
if(current.next.value==dvalue){
Node temp=current.next;
current.next=temp.next;
return temp;
}
current=current.next;
}
return null;
}
//删除特定位置的节点
public Node deletepos(int pos){
Node current=head;
int counter=0;
if(pos==0){
head=head.next;
return current;
}
while(current!=null){
if((counter==(pos-1))&&(current.next!=null)){
Node temp=current.next;
current.next=temp.next;
return temp;
}
current=current.next;
counter++;
} return null; }
//单链表转置
public void reverse(){
Node a=head;
if(a==null){
return ;
}
Node b=head.next;
if(b==null){
return ;
}
Node c=head.next.next;
a.next=null;
while(c!=null){
b.next=a;
a=b;
b=c;
c=c.next;
}
b.next=a;
head=b;
}
//输出链表信息
public void print(){
Node current = head;
while(current!=null){
System.out.println(current.value);
current=current.next;
} } public static void main(String[] args){ ListNode l=new ListNode();
l.addpre(3);
l.addpre(2);
l.addpre(1);
l.add(7);
l.add(8);
l.add(9);
l.delete(1);
l.deletepos(4);
l.reverse();
l.print();
} } class Node{
public Node next;
public int value;
public Node(){
next=null;
}
public Node(int v){
value=v;
}
}
PS:Java的引用类似于C的指针,例如 :
Node n1=new Node(1); Node n2=n1; Node n3=new Node(3); n2=n3;
执行n2=n1后,n1和n2都是对同一块内存区域(区域1)的引用,通过n1和n2都可以达到修改内存区域1的目的,例如执行n1.value=10后,输出n2.value的值也为10。但是执行n2=n3后,n2则变为了对另一块内存区域(区域3)的应用。
用Java实现单链表的基本操作的更多相关文章
- Java实现单链表的各种操作
Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素 4.实现链表的反转 5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...
- 数据结构——Java实现单链表
一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...
- (java实现)单链表
什么是单链表 在了解单链表之前,你知道什么是链表吗?如果你不知道什么是链表,可以看看我的这篇博客<链表-LinkList> 单链表是链表的其中一种基本结构.一个最简单的结点结构如图所示,它 ...
- java实现单链表的增删功能
JAVA 实现单链表的增删功能 package linked; class LinkedTable{ } public class LinkedTableTest { public static vo ...
- PHP单链表的基本操作
链表的实现 数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个X 上代 ...
- JAVA数据结构——单链表
链表:一. 顺序存储结构虽然是一种很有用的存储结构,但是他有如下几点局限性:1. 因为创造线性表的时候已经固定了空间,所以当需要扩充空间时,就需要重新创建一个地址连续的更大的存储空间.并把原有的数据元 ...
- 使用java实现单链表(转载自:https://www.cnblogs.com/zhongyimeng/p/9945332.html)
使用java实现单链表----(java中的引用就是指针)转载自:https://www.cnblogs.com/zhongyimeng/p/9945332.html ? 1 2 3 4 5 6 7 ...
- 用java简单的实现单链表的基本操作
package com.tyxh.link; //节点类 public class Node { protected Node next; //指针域 protected int data;//数据域 ...
- java实现单链表常见操作
一.概述: 本文主要总结单链表常见操作的实现,包括链表结点添加.删除:链表正向遍历和反向遍历.链表排序.判断链表是否有环.是否相交.获取某一结点等. 二.概念: 链表: 一种重要的数据结构,HashM ...
随机推荐
- 后缀数组---New Distinct Substrings
Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...
- yii2.0配置以pathinfo的形式访问
yii2.0默认的访问形式为:dxr.com/index.php?r=index/list,一般我们都会配置成pathinfo的形式来访问:dxr.com/index/list,这样更符合用户习惯. ...
- http get post
使用java代码模拟http请求 package ftp; import java.io.BufferedReader; import java.io.IOException; import java ...
- 正态QQ图的原理
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- 快速生成PDF书签
PDF没有书签,就像吃饭没有筷子一样,虽然可以将就,但总不是很方便!现介绍一种快速生成书签的方法. 第一步,打开excel,制作书签目录,前面的一列是书签名称(黑色框),后面一列是PDF页码(红色框) ...
- SharePoint 2013 跨网站集发布功能简介
在SharePoint Server 2013网站实施中,我们经常会遇到跨网站集获取数据,而2013的这一跨网站集发布功能,正好满足我们这样的需求. 使用SharePoint 2013中的跨网站发布, ...
- VC连接mysql数据库错误:libmysql.lib : fatal error LNK1113: invalid machine 解决方法
VC连接MySQL的配置过程在上一篇博文中,不过当你设置好,以为万事大吉的时候,运行却出现这个错误:libmysql.lib : fatal error LNK1113: invalid machin ...
- 关于一个软件ipa包的其他图片资源
有时候 当你打开一个ipa包内容的时候 可能会找不到全部的资源 所以你需要在github上下载一个插件 下载下来以后 运行一下 然后会出来这个 把ipa 文件 拖到里面 ...
- IOS 计步器
这篇博客介绍的是当前比较流行的“计步器”-只是简单的知识点 计步器的实现在IOS8开始进行了改变. 但是我会对之前之后的都进行简单介绍. IOS 8 - // // ViewController.m ...
- ios 计算缓存大小
- (void)getSize2 { // 图片缓存 NSUInteger size = [SDImageCache sharedImageCache].getSize; // NSLog(@&qu ...