关于链表

来源:互联网 发布:java void方法结束 编辑:程序博客网 时间:2024/05/19 23:24
package cn.lianbiao;


public class MyLink {
Node head=null;

class Node {
//链表中的节点,data代表节点的值,next是指向下一个节点的引用
Node next=null;  //节点的引用,指向下一个节点
int data;      //节点的对象,即内容
public Node(int date)
{
this.data=date;
}
}
//index:删除第index个节点
public boolean deleteNode(int index)
{
if (index < 1 || index > length()) {
return false;
}
if(index==1)
{
head=head.next;
return true;
}
int i=1;
Node preNode=head;
Node curNode=preNode.next;
while(curNode!=null)
{
if(i==index)
{
preNode.next=curNode.next;
return true;
}
preNode=curNode;
curNode=curNode.next;
i++;
}
return false;
}
//返回字节长度
public int length()
{
int length=0;
Node tmp=head;
while(tmp!=null)
{
length++;
tmp=tmp.next;
}
return length;
}
//在不知道指针的情况下删除指定节点
public boolean deleteNode11(Node n) {
if (n == null || n.next == null)
return false;
int tmp = n.data;
n.data = n.next.data;
n.next.data = tmp;
n.next = n.next.next;
System.out.println("删除成功!");
return true;
}
public void printList()
{
Node tmp=head;
while(tmp!=null)
{
System.out.println(tmp.data);
tmp=tmp.next;
}
}
//向链表中插入数据
public void addNode(int d) {
Node newNode=new Node(d);    //实例化一个节点
if(head==null)
{
head=newNode;
return;
}
Node tmp=head;
while(tmp.next!=null)
{
tmp=tmp.next;
}
tmp.next=newNode;

}
//链表反转
public Node ReverseIterativaly(Node head)
{
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 Node SearchMid(Node head)
{
Node in=this.head,out=this.head;
while(in!=null&&in.next!=null&&in.next.next!=null)
{
in=in.next.next;
out=out.next;
}
System.out.println("Mid:"+out.data);
return out;
}
//查找倒数第K个元素
//采用两个指针P1,P2,P1先前移K步,然后P1、P2同时移动,当p1移动到尾部时,P2所指位置的元素即倒数第k个元素 。
public Node findElem(Node head,int k)
{
if(k<1||k>this.length())
{
return null;
}
Node p1=head;
Node p2=head; 
for(int i=0;i<k;i++)   //前移k步
{
p1=p1.next;
while(p1!=null)
{
p1=p1.next;
p2=p2.next;
}
}
return p2;
}
//对链表进行排序
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;
}
curNode=curNode.next;
}
return head;
}
     //删除链表中的重复节点
public boolean 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;
}
}
return false;
}
//从尾到头输出单链表,采用递归方式实现
public void printListReversely(Node pListHead)
{
if(pListHead!=null)
{
printListReversely(pListHead.next);
System.out.println("printListReversely"+pListHead.data);
}
}
//判断链表是否有环,有环情况下找出环的入口节点
boolean IsLoop(Node head)
{
Node fast=head,slow=head;
if(fast==null)
{
return false;
}
while(fast!=null&&fast.next!=null)
{
fast=fast.next.next;
slow=slow.next;
if(fast==slow)
{
System.out.println("该链表有环");
return true;
}
}
return !(fast==null||fast.next==null);
}
//找出链表环的入口
public Node FindLoopPort(Node head)
{
Node fast=head,slow=head;
while(fast!=null&&fast.next!=null)
{
slow=slow.next;
fast=fast.next.next;
if(slow==fast)
{
break;
}
if(fast==null||fast.next==null)

return null;
slow=head;
while(slow!=fast)
{
slow=slow.next;
fast=fast.next;
}
}
return slow;
}
}

package cn.lianbiao;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
MyLink list=new MyLink();
list.addNode(5);
list.addNode(4);
list.addNode(3);
list.addNode(2);
list.addNode(1);
list.addNode(0);
System.out.println("linkLength:"+list.length());
System.out.println("head.data:"+list.head.next.data);
list.printList();
list.deleteNode(4);
System.out.println("After deleteNode(4):"+list.deleteNode(4));
list.printList();
}

}

























0 0
原创粉丝点击