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创建节点和单向链表的更多相关文章

  1. C语言:将带头节点的单向链表结点域中的数据从小到大排序。-求出单向链表结点(不包括头节点)数据域中的最大值。-将M*N的二维数组中的数据,按行依次放入一维数组,

    //函数fun功能是将带头节点的单向链表结点域中的数据从小到大排序. //相当于数组的冒泡排序. #include <stdio.h> #include <stdlib.h> ...

  2. java笔试之从单向链表中删除指定值的节点

    输入一个单向链表和一个节点的值,从单向链表中删除等于该值的节点,删除后如果链表中无节点则返回空指针. 链表的值不能重复 构造过程,例如 1 -> 2 3 -> 2 5 -> 1 4  ...

  3. [Java算法分析与设计]--单向链表(List)的实现和应用

    单向链表与顺序表的区别在于单向链表的底层数据结构是节点块,而顺序表的底层数据结构是数组.节点块中除了保存该节点对应的数据之外,还保存这下一个节点的对象地址.这样整个结构就像一条链子,称之为" ...

  4. C语言初级链表(之有头节点的单向链表)

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> typedef struct No ...

  5. 单向链表的归并排序——java实现

    在做Coursera上的Algorithms第三周测验练习的时候有一道链表随机排序问题,刚开始没有什么思路,就想着先把单向链表归并排序实现了,再此基础上进行随机排序的改造.于是就结合归并排序算法,实现 ...

  6. C语言:判断字符串是否为回文,-函数fun将单向链表结点数据域为偶数的值累加起来。-用函数指针指向要调用的函数,并进行调用。

    //函数fun功能:用函数指针指向要调用的函数,并进行调用. #include <stdio.h> double f1(double x) { return x*x; } double f ...

  7. C语言:创建动态单向链表,创建完成后,输出每一个节点的数据信息。

    // //  main.c //  dynamic_link_list // //  Created by ma c on 15/8/5. //  Copyright (c) 2015. All ri ...

  8. Java实现单向链表基本功能

    一.前言 最近在回顾数据结构与算法,有部分的算法题用到了栈的思想,说起栈又不得不说链表了.数组和链表都是线性存储结构的基础,栈和队列都是线性存储结构的应用- 本文主要讲解单链表的基础知识点,做一个简单 ...

  9. 线性表的Java实现--链式存储(单向链表)

    单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始. 链式存储结构的线性表将采用一组任意的存储单元存放线性表中的数据元素.由于不需要按顺序存储,链表在 ...

随机推荐

  1. 【Codeforces Round #423 (Div. 2) B】Black Square

    [Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...

  2. Node.js的helloworld 程序

    用文本编辑器.如npp,键入例如以下代码.存储成hello.js console.log('hello') console.log('hello %s->%d','jeapedu', 19418 ...

  3. thinkphp路由的作用

    thinkphp路由的作用 问题 请问一下什么是thinkPHP路由,路由有什么作用?谢谢 解答 网络访问地址从来都是映射访问的,最初是这样,主机名(电脑名称)=>ip地址(如局域网192.16 ...

  4. 13.ng-value

    转自:https://www.cnblogs.com/best/tag/Angular/ 绑定给定的表达式到input[select]或 input[radio]的值上 <input type= ...

  5. POJ 3189 二分+Dinic

    题意: 思路: 二分跨度 枚举最低座次 建图:源点向每头牛连边权为1的边 每头牛向当前枚举的B的区间这段连上边权为1的边 所有座次向汇点连边权为牛棚容量的边 判判流量是不是等于n 一开始写得是直接枚举 ...

  6. Gym - 100338E Numbers 贪心

    Gym - 100338E 题意:给你n,k问在1-n中能整出k的字典序最小的数.范围1018 思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsign ...

  7. Python(二) 表示‘组’的概念与定义

    现实世界中总存在一组一组的事物, 一.列表的定义 type(['hello','world',1,9,True,False]) = <class 'list'> type([[1,2,3, ...

  8. 携手互联网企业10巨头设VC基金

    包括小米科技.盛大集团.人人网.掌趣科技.游族网络.龙图游戏.蓝港互动.37游戏.星辉互动娱乐.博雅互动等10家知名互联网企业作为出资人(LP)的优格创投基金近日正式成立. 众所周知,伴随着移动互联网 ...

  9. C#初学者使用file.creat()创建文件后,显示正由另一进程使用

    string sourcePhotoPath = this.GetUserSelectedPhoto(); if(sourcePhotoPath == null) { return; } string ...

  10. babel的插件

    比如想编译es6的箭头函数,需要使用babel-plugin-transform-es2015-arrow-functions这个插件 此外babel提供了 prests(预设) 相当于是插件的集合 ...