代码
import com.sun.corba.se.impl.orbutil.graph.Node;
/**
* Created by junyi.pc on 2017/1/25.
*/
public class Main {
Node head=null;
Node tail=null;
class Node { //内部类
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
public void addNode(int data){//增加节点
Node node=new Node(data);
if(head==null){
head=node;
tail=node;
}
else{
Node temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
}
}
public void deleteNode(int data){//删除节点
Node tempNode=head;
if(head==null){
}
if(tempNode.data==data){
head=tempNode.next;//要删了第一个节点,就要把头结点后移
if(tempNode.next==null){//这里特别考虑,如果是只有一个节点,要把尾结点变null
tail=null;
}
delete(tempNode);//最后删了这个点
}
while(tempNode.next!=null&&tempNode.next.data!=data)//遍历链表找到第一个值与x相等的节点,temp表示这个节点的上一个节点
{
tempNode=tempNode.next;
}
if(tempNode.next==null) return;//如果没有找到则返回
if(tempNode==tail)//如果找到的时候尾节点
{
tail=tempNode;//把尾节点指向他的上一个节点
delete(tempNode.next);//删除尾节点
tempNode.next=null;
}
else//如果不是尾节点,如图4
{
Node node=tempNode.next;//用临时节点node指向要删除的节点
tempNode.next=tempNode.next;//要删除的节点的上一个节点指向要删除节点的下一个节点
delete(node);//删除节点
node=null;
}
}
public void traversal() { //遍历链表
Node tempNode = head;
while ( tempNode != null) {
System.out.println( tempNode.data);
tempNode = tempNode.next;
}
}
public void insert(int data,Node lastNode){
if(lastNode==null){
//一个节点都没有
}
else{
Node node=new Node(data);//新节点来了怎么得也得分配空间吧
node.next=lastNode.next;
lastNode.next=node;
if(node.next==null){//如果恰好这小子是尾节点
tail=node;//设置为尾节点
}
}
}
public void delete(Node node){
//
}
public Node ReverseIteratively() {//链表反转
Node pReversedHead = head;
Node pNode = head;
Node pPrev = null;
while (pNode != null) {
Node pNext = pNode.next;
if (pNext == null) {
pReversedHead = pNode;
}
pNode.next = pPrev;
pPrev = pNode;
pNode = pNext;
}
this.head = pReversedHead;
return this.head;
}
//删除重复节点
public void deleteDuplecate(Node head) {
Node p = head;
while (p != null) {
Node q = p;
while (q.next != null) {
if (p.data == q.next.data) {
q.next = q.next.next;
} else
q = q.next;
}
p = p.next;
}
}
public int length() {//返回链表长度
int length = 0;
Node tempNode = head;
while (tempNode != null) {
length++;
tempNode = tempNode.next;
}
return length;
}
public Node orderList() {
Node nextNode = null;
int tmp = 0;
Node curNode = head;//开始那个点
//类似于冒泡排序
while (curNode.next != null) {
nextNode = curNode.next;
while (nextNode != null) {
if (curNode.data > nextNode.data) {
tmp = curNode.data; //交换
curNode.data = nextNode.data;
nextNode.data = tmp;
}
nextNode = nextNode.next;//相当于j++
}
curNode = curNode.next;//相当于i++
}
return head;
}
public static void main(String[] args){
Main list = new Main();
list.addNode(1);
list.addNode(2);
list.addNode(3);
System.out.println("linkLength:" + list.length());
list.traversal();
list.addNode(4);
list.traversal();
}
}
C:\java\jdk1.8.0_25\bin\java -Didea.launcher.port=7535 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\java\jdk1.8.0_25\jre\lib\charsets.jar;C:\java\jdk1.8.0_25\jre\lib\deploy.jar;C:\java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\java\jdk1.8.0_25\jre\lib\javaws.jar;C:\java\jdk1.8.0_25\jre\lib\jce.jar;C:\java\jdk1.8.0_25\jre\lib\jfr.jar;C:\java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\java\jdk1.8.0_25\jre\lib\jsse.jar;C:\java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\java\jdk1.8.0_25\jre\lib\plugin.jar;C:\java\jdk1.8.0_25\jre\lib\resources.jar;C:\java\jdk1.8.0_25\jre\lib\rt.jar;C:\Users\junyi.pc\Desktop\client\out\production\client;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
linkLength:3
1
2
3
1
2
3
4
Process finished with exit code 0