java创建节点和单向链表
package datastructure; public class Node {
private Object data;
private Node next; public Node() {
this(null,null);
} public Node(Object data) {
this(data,null);
} public Node(Object data, Node next) { this.data = data;
this.next = next;
}
public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
} public Node getNext() {
return next;
} public void setNext(Node next) {
this.next = next;
} }
package datastructure; import java.util.Scanner; public class LinkList implements IList {
private Node head; //单链表的头指针
public LinkList(){
head=new Node(); //初始化头指针
}
public LinkList(int n,boolean Order) throws Exception{
this();
if(Order)
create1(n); // 尾插法
else
create2(n); // 头插法
} private void create1(int n) throws Exception {
Scanner sc=new Scanner(System.in);
for(int j=0;j<n;j++)
insert(0,sc.next());
}
private void create2(int n) throws Exception {
Scanner sc=new Scanner(System.in);
for(int j=0;j<n;j++)
insert(length(),sc.next());
}
public void clear() {
head.setData(null);
head.setNext(null);
} public boolean isEmpty() { return head.getNext()==null;
} public int length() {
Node p=head.getNext();
int length=0;
while(p!=null){
p=p.getNext();
length++;
}
return length; } public Object get(int i) throws Exception {
Node p=head.getNext();
int j=0;
while(p!=null&&j<i){
p=p.getNext();
j++;
}
if(j>i||p==null){
throw new Exception("第i个元素不存在");
} return p.getData();
} public void insert(int i, Object x) throws Exception {
Node p =head;
int j=-1;
while(p!=null&&j<i-1){
p=p.getNext();
j++;
}
if(p==null||j>i-1){
throw new Exception("插入位置不合法");
}
Node s=new Node(x);
s.setNext(p.getNext());
p.setNext(s);
} public void remove(int i) throws Exception {
Node p=head;
int j=-1;
while(p.getNext()!=null&&j<i-1){
p=p.getNext();
j++;
}
if(j>i-1||p.getNext()==null)
throw new Exception("删除位置不合法");
p.setNext(p.getNext().getNext());
} public int indexOf(Object x) {
Node p=head.getNext();
int j=0;
while(p!=null&&!p.getData().equals(x)){
p=p.getNext();
j++;
}
if(p==null)
return -1;
else
return j; } public void display() {
Node node =head.getNext();
while(node!=null){
System.out.println(node.getData()+" ");
node=node.getNext();
}
System.out.println();
} }
java创建节点和单向链表的更多相关文章
- C语言:将带头节点的单向链表结点域中的数据从小到大排序。-求出单向链表结点(不包括头节点)数据域中的最大值。-将M*N的二维数组中的数据,按行依次放入一维数组,
//函数fun功能是将带头节点的单向链表结点域中的数据从小到大排序. //相当于数组的冒泡排序. #include <stdio.h> #include <stdlib.h> ...
- java笔试之从单向链表中删除指定值的节点
输入一个单向链表和一个节点的值,从单向链表中删除等于该值的节点,删除后如果链表中无节点则返回空指针. 链表的值不能重复 构造过程,例如 1 -> 2 3 -> 2 5 -> 1 4 ...
- [Java算法分析与设计]--单向链表(List)的实现和应用
单向链表与顺序表的区别在于单向链表的底层数据结构是节点块,而顺序表的底层数据结构是数组.节点块中除了保存该节点对应的数据之外,还保存这下一个节点的对象地址.这样整个结构就像一条链子,称之为" ...
- C语言初级链表(之有头节点的单向链表)
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> typedef struct No ...
- 单向链表的归并排序——java实现
在做Coursera上的Algorithms第三周测验练习的时候有一道链表随机排序问题,刚开始没有什么思路,就想着先把单向链表归并排序实现了,再此基础上进行随机排序的改造.于是就结合归并排序算法,实现 ...
- C语言:判断字符串是否为回文,-函数fun将单向链表结点数据域为偶数的值累加起来。-用函数指针指向要调用的函数,并进行调用。
//函数fun功能:用函数指针指向要调用的函数,并进行调用. #include <stdio.h> double f1(double x) { return x*x; } double f ...
- C语言:创建动态单向链表,创建完成后,输出每一个节点的数据信息。
// // main.c // dynamic_link_list // // Created by ma c on 15/8/5. // Copyright (c) 2015. All ri ...
- Java实现单向链表基本功能
一.前言 最近在回顾数据结构与算法,有部分的算法题用到了栈的思想,说起栈又不得不说链表了.数组和链表都是线性存储结构的基础,栈和队列都是线性存储结构的应用- 本文主要讲解单链表的基础知识点,做一个简单 ...
- 线性表的Java实现--链式存储(单向链表)
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始. 链式存储结构的线性表将采用一组任意的存储单元存放线性表中的数据元素.由于不需要按顺序存储,链表在 ...
随机推荐
- Linux能ping通IP,ping不通域名
今天碰到个问题, 能ping通IP地址, ping不通域名, 一直以为是 DNS解析服务器的问题, 找了半天. 问题不在这里. [root@www postfix]# cat /etc/resolv. ...
- How Blink works
How Blink works Author: haraken@ Last update: 2018 Aug 14 Status: PUBLIC Working on Blink is not eas ...
- 如何查看 Linux 中所有正在运行的服务
有许多方法和工具可以查看 Linux 中所有正在运行的服务.大多数管理员会在 System V(SysV)初始化系统中使用 service service-name status 或 /etc/ini ...
- mysql分页小结
mysql.select('*').from('books') .join('cSessionInfo', 'books.openid', 'cSessionInfo.open_id') .limit ...
- Unity 实现Log实时输出到屏幕或控制台上<一>
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/49818953 作者:car ...
- Safe and efficient allocation of memory
Aspects of the present invention are directed at centrally managing the allocation of memory to exec ...
- oracle 01578
http://blog.itpub.net/7728585/viewspace-670597/ http://www.2cto.com/database/201208/149056.html http ...
- 安卓https
http://www.tuicool.com/articles/NrmE3e http://blog.csdn.net/guestcode/article/details/50194357 http: ...
- QQ互联账号登录
本文说明的是依据某应用通过网页的qq信息来登录的过程.用途是利用QQ账号就能高速自己主动注冊并可以登录客户应用. 从webserver与腾讯server通信获取开房平台用户OpenID,再在应用ser ...
- intellij—idea14 注冊机
package com.qunar.fresh; import java.math.BigInteger; import java.util.Date; import java.util.Random ...