java單鏈表操作


java的單鏈表的一些操作


package listTest;

/**
* 自定義鏈表設計
*
*/
public class MyLink {
Node head = null; // 頭節點

/**
* 鏈表中的節點,data代表節點的值,next是指向下一個節點的引用
*
* @author zjn
*
*/
class Node {
Node next = null;// 節點的引用,指向下一個節點
int data;// 節點的對象,即內容

public Node(int data) {
this.data = data;
}
}

/**
* 向鏈表中插入數據 如果是第一個節點就放在頭,否則一直往后走
*
* @param d
*/
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;
}

/**
*
* @param index:刪除第index個節點
* @return
*/
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) {//如果循環到了當前的節點就把當前的next指向給消掉
preNode.next = curNode.next;
return true;
}
preNode = curNode;
curNode = curNode.next;
i++;
}
return false;
}

/**
*
* @return 返回節點長度
*/
public int length() {
int length = 0;
Node tmp = head;
while (tmp != null) {
length++;
tmp = tmp.next;
}
return length;
}



public void printList() {
Node tmp = head;
while (tmp != null) {

System.out.print(tmp.data + " ");
tmp = tmp.next;
}
System.out.println();
}




/**
* 排序
*
* @return
*/
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 static void deleteDuplecate(Node head) {
Node p = head;
while (p != null) {
Node q = p;//q把當前的指針記下來你
while (q.next != null) {//單鏈表的最后一個節點的next為空
if (p.data == q.next.data) {
q.next = q.next.next;
} else
q = q.next;
}
p = p.next;
}

}
/**
* 從尾到頭輸出單鏈表,采用遞歸方式實現
*
* @param pListHead
*/
public static void printListReversely(Node pListHead) {
if (pListHead != null) {
printListReversely(pListHead.next);
System.out.println("printListReversely:" + pListHead.data);
}
}
/**
* 查找單鏈表的中間節點
*
* @param head
* @return
*/
public Node SearchMid(Node head) {
Node p = this.head,
q = this.head;
while (p != null && p.next != null && p.next.next != null) {

p = p.next.next;
q = q.next;
}
System.out.println("Mid:" + q.data);
return q;
}
/**
* 查找倒數 第k個元素
*
* @param head
* @param k
* @return
*/
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;
}
/**
* 判斷鏈表是否有環,單向鏈表有環時,尾節點相同
*
* @param head
* @return
*/
public 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);
}

/**
* 找出鏈表環的入口
*
* @param head
* @return
*/
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;
}



/**
* 鏈表反轉
*
* @param head
* @return
*/
public void ReverseIteratively(Node head,MyLink list ) {
Node pReversedHead = head;
Node pNode = head;
Node pPrev = null;
while (pNode != null){
Node pNext = pNode.next;//pNode是不是尾節點
if (pNext == null) {
pReversedHead = pNode;
}
pNode.next = pPrev;
//pNext = pPrev;
pPrev = pNode;
pNode = pNext;
}
this.head = pReversedHead;
//return this.head;
}

/**
* 在不知道頭指針的情況下刪除指定節點
*
* @param n
* @return
*/
public static boolean deleteNodeNoHead(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 static void main(String[] args) {
MyLink list = new MyLink();

list.addNode(5);
list.addNode(3);
list.addNode(1);
//list.addNode(5);
list.addNode(7);
list.addNode(5);

boolean flag= list.IsLoop(list.head);
if(flag==true){
System.out.println("有環");
}else{
System.out.println("無環");

}
/* Node n=list.findElem(list.head, 2);
deleteNodeNoHead(n);
list.printList();*/
/* list.addNode(6);
list.addNode(2);
list.addNode(2);
list.addNode(55);
list.addNode(36);*/
// System.out.println("linkLength:" + list.length());
//System.out.println("head.data:" + list.head.data);
//deleteDup2(list.head);

//printListReversely(list.head);
//list.printList();
//list. ReverseIteratively(list.head);
// Node n= list.findElem(list.head, 3);

// System.out.println("原始的:");
//list.printList();
// list. reverse2(list.head);
//System.out.println(n.data);
//list.SearchMid(list.head);
// System.out.println("翻轉之后:========");
list.printList();
/* list.deleteNode(4);
System.out.println("After deleteNode(4):");
list.printList();

Node node1= list.new Node(1);
deleteNode11(node1);
System.out.println("After 1:");
list.printList();*/
}
}



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com