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实现--链式存储(单向链表)
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始. 链式存储结构的线性表将采用一组任意的存储单元存放线性表中的数据元素.由于不需要按顺序存储,链表在 ...
随机推荐
- Java基础学习总结(12)——一哈希编码HashCode
一.哈希编码 现在是站在JAVA虚拟机的角度来看内存里面的布局,站在JAVA虚拟机的角度,在内存里面有好多好多个对象,这里用椭圆来代表一个个对象.一个程序运行起来的时候,可能会有很多个对象在内存里面分 ...
- 安卓自己定义对话框及The specified child already has a child问题
问题:在android开发过程中,有时会在不同情况下遇到同种问题:The specified child already has a parent.You must call removeView() ...
- 并查集树数据结构hdu1325
我的解法就是去构造了一棵树 以数组的存储方式 数组的值存放节点的根. 排除空树 剩下的就是出现环和多根节点的情况 也就是排除森林和有一个节点多个入度的情况 排除森林就用到了并查集 也就是便利数组让其仅 ...
- Understanding IIS Bindings, Websites, Virtual Directories, and lastly Application Pools
In a recent meeting, some folks on my team needed some guidance on load testing the Web application ...
- Domino服务器SSL的配置录像
Domino服务器SSL的配置录像 格式:avi, 大小:25M 时长: 6分钟 本文出自 "李晨光原创技术博客" 博客,转载请与作者联系!
- PullToRefreshListView中嵌套ViewPager滑动冲突的解决
PullToRefreshListView中嵌套ViewPager滑动冲突的解决 最近恰好遇到PullToRefreshListView中需要嵌套ViewPager的情况,ViewPager 作为头部 ...
- codeforces 710A King Moves(水)
output standard output The only king stands on the standard chess board. You are given his position ...
- win7防火墙里开启端口的图文教程
转载于:http://www.cnblogs.com/vipsoft/archive/2012/05/02/2478847.html 开启端口:打开“控制面板”中的“Windows防火墙”,点击左侧的 ...
- 原生ajax实现方式
http://www.cnblogs.com/rubylouvre/archive/2013/01/08/2851051.html <!DOCTYPE html> <html lan ...
- UTC时间 GMT时间 本地时间 北京时间 时区 夏令时简要说明
1.UTC时间 与 GMT时间 整个地球分为二十四时区,每个时区都有自己的本地时间.为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated). ...