順序表的實現:
public class MyArrayList {
public int usedSize;
public int[] elem;
public final int CAPACITY = 10;
public MyArrayList() {
this.usedSize = 0;
this.elem = new int[CAPACITY];
}
// 在 pos 位置新增元素
public void add(int pos, int data) {
//1、pos是否合法
//2、挪數據
//3、插入數據
//4、usedSize++
//插入數據
if(pos < 0 || pos > this.elem.length - 1){
return;
}else if(isFull()){
//用Arrays.copyOf()方法擴容數組
Arrays.copyOf(this.elem,this.elem.length * 2);
//下面被註釋的代碼塊是用for()循環擴容數組
// int newCapacity = this.elem.length * 2;
// int[] newArray = new int[newCapacity];
// for (int i = 0; i < this.usedSize; i++) {
// newArray[i] = this.elem[i];
// }
// this.elem = newArray;
} else{
int i;
for (i = this.usedSize - 1; i >= pos; i--) {
this.elem[i + 1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
}
public boolean isFull(){
return this.usedSize == this.elem.length;
}
public boolean isEmpty(){
return this.usedSize == 0;
}
// 判定是否包含某個元素
public boolean contains(int toFind) {
for (int i = 0; i < this.elem.length; i++) {
if(toFind == this.elem[i]){
return true;
}
}
return false;
}
// 查找某個元素對應的位置
public int search(int toFind) {
if(isEmpty()){
System.out.println("順序表爲空");
return -1;
}
for (int i = 0; i < this.usedSize; i++) {
if(this.elem[i] == toFind){
return i;
}
}
return -1;
// int left = 0;
// int right = elem.length - 1;
// while(left < right){
// int mid = (left + right) / 2;
// if(toFind < elem[mid]){
// right = mid - 1;
// }else if(toFind > elem[mid]){
// left = mid + 1;
// }else{
// return mid;
// }
// }
}
// 獲取 pos 位置的元素
public int getPos(int pos) {
if(pos < 0 || pos > this.usedSize){
return -1;
}
return this.elem[pos];
}
// 給 pos 位置的元素設爲 value
public void setPos(int pos, int value) {
this.elem[pos] = value;
}
//刪除第一次出現的關鍵字key
public void remove(int toRemove) {//------------------------------------------
//1、順序表是否爲空
//2、查找要刪除數字的下標
//3、刪除
//查找toRemove下標
int index = search(toRemove);
if(index == -1){
System.out.println("沒有你要刪除的數字");
return;
}
for (int i = index; i < this.usedSize - 1; i++) {
this.elem[i] = this.elem[i + 1];
}
this.usedSize--;
}
// 獲取順序表長度
public int size() {
return this.usedSize;
}
// 清空順序表
public void clear() {
this.usedSize = 0;
}
// 打印順序表
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.println(this.elem[i]+ " ");
}
System.out.println();
}
}
鏈表的實現:
import java.util.List;
class ListNode{
public int data;
public ListNode next;
public ListNode(int data){
this.data = data;
this.next = null;
}
}
class MySingleList {
public ListNode head;
public MySingleList(){
this.head = null;
}
//頭插法
public void addFirst(int data){
ListNode node = new ListNode(data);
if(this.head == null){
head = node;
}else {
node.next = head;
head = node;
}
}
//尾插法
public void addLast(int data){
ListNode node = new ListNode(data);
ListNode cur = this.head;
if(this.head == null){
head = node;
}else{
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
}
}
/**
* 找到index-1的位置
* @param index
* @return
*/
private ListNode searchIndex(int index) {
ListNode cur = this.head;
//cur -> index-1
int count = 0;
while(count < index - 1){
cur = cur.next;
count++;
}
return cur;
}
//任意位置插入,第一個數據節點爲0號下標
public boolean addIndex(int index,int data){
if(index < 0 || index > getLength()) {
System.out.println("index不合法!");
return false;
}
if(index == 0) {
addFirst(data);
return true;
}
//找到indext - 1 的位置
ListNode cur = searchIndex(index);
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
return true;
}
//查找是否包含關鍵字key是否在單鏈表當中
public boolean contains(int key){
ListNode cur = this.head;
while(cur != null){
if(cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
private ListNode searchPrev(int key){
ListNode cur = this.head;
while(cur.next != null){
if(cur.next.data == key){
return cur;
}
cur = cur.next;
}
return null;
}
//刪除第一次出現關鍵字爲key的節點
public void remove(int key){
if(this.head == null){
return;
}
if(this.head.data == key){
this.head = this.head.next;
}
ListNode prev = searchPrev(key);
if(prev == null){
return;
}
ListNode del = prev.next;
prev.next = del.next;
}
//刪除所有值爲key的節點
public void removeAllKey(int key){
if(this.head.data == key){
head = head.next;
}
ListNode prev = this.head;
ListNode cur = prev.next;
while(cur != null){
if(prev.next.data == key){
prev.next = cur.next;
cur = cur.next;
}
if(prev.next.data != key){
prev = cur;
cur = cur.next;
}
}
}
//得到單鏈表的長度
public int getLength(){
int count = 0;
ListNode cur = this.head;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
public void display(){
ListNode cur = this.head;
while(cur != null){
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public void display2(ListNode newHead){
ListNode cur = newHead;
while(cur != null){
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
//防止內存泄漏
public void clear(){
this.head = null;
//單鏈表中置空,下面的也可以,這個也可以,雙向鏈表中用上面的暴力法不行
// //一個一個置爲空
// while(this.head != null){
// ListNode cur = this.head.next;
// this.head.next = null;
// this.head = cur;
// }
}
//反轉鏈表
public ListNode reverseList(){
ListNode prev = null;
ListNode cur = this.head;
ListNode curNext = null;
while(cur != null){
curNext = cur.next;
cur.next = prev;
prev = cur;
cur = curNext;
}
return prev;
}
//單鏈表的中間節點
public ListNode middleNode(ListNode head) {
ListNode slow = this.head;
ListNode fast = this.head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
//鏈表的倒數第K個結點
public ListNode FindKthToTail(ListNode head,int k){
if(this.head == null || k <= 0){
return null;
}
ListNode fast = this.head;
ListNode slow = this.head;
while(k - 1 > 0){
if(fast.next != null) {
fast = fast.next;
k--;
}else{
return null;
}
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
public ListNode partition(ListNode pHead, int x){
ListNode small = null;//定義兩個新鏈表,分別存放比x小的元素以及大於等於x的元素
ListNode smallLast = null;//記錄兩個鏈表的最後一個元素
ListNode big = null;
ListNode bigLast = null;
ListNode cur = pHead;
while(cur != null){//遍歷鏈表
if(cur.data < x){//如果小於x
if(small == null){//如果small鏈表爲空,直接把cur放進去
small = cur;
}else{//如果small鏈表不爲空,把cur放在small鏈表最後
smallLast.next = cur;
}
smallLast = cur;//更新小鏈表最後一個元素
}else{//同理如果大於x
if(big == null){
big = cur;
}else{
bigLast.next = cur;
}
bigLast = cur;//更新大鏈表最後一個元素
}
cur = cur.next;//繼續往後遍歷
}
if(small == null){
return big;
}else{
smallLast.next = big;//連接兩個鏈表作爲最終結果並返回
if(bigLast != null){
bigLast.next = null;
}
return small;
}
}
//刪除鏈表重複結點
public ListNode deleteDuplication(){
ListNode node = new ListNode(-1);
ListNode cur = this.head;
ListNode tmp = node;
while(cur != null){
if(cur.next != null && cur.data == cur.next.data){
while (cur.next != null && cur.data == cur.next.data){
cur = cur.next;
}
cur = cur.next;
}else{
tmp.next = cur;
cur = cur.next;
tmp = tmp.next;
}
}
tmp.next = null;
return node.next;
}
//鏈表的迴文結構
public boolean chkPalindrome(){
ListNode slow = this.head;
ListNode fast = this.head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode cur = slow;
ListNode prev = null;
while(cur != null){
ListNode next = cur.next;
cur.next = prev;
prev = cur;
cur =next;
}
ListNode pHead = this.head;
ListNode last = prev;
boolean flag = true;
while(last != null){
if(pHead.data != last.data){
flag = false;
break;
}
pHead = pHead.next;
last = last.next;
}
return flag;
}
//鏈表是否帶環
public boolean hasCycle(){
ListNode slow = this.head;
ListNode fast = this.head;
while(slow != null && fast != null){
if(fast.next == null){
return false;
}
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
return true;
}
}
return false;
}
//找到帶環鏈表的入環點
public ListNode detectCycle(){
ListNode slow = this.head;
ListNode fast = this.head;
while(slow != null && fast != null){
if(fast.next == null){
return null;
}
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
break;
}
if(slow == null || fast == null){
return null;
}
}
fast = this.head;
while(slow != fast && slow != null && fast != null){
slow = slow.next;
fast = fast.next;
}
return fast;
}
//合併兩個有序鏈表
public ListNode mergeTwoLists(ListNode l1, ListNode l2){
ListNode node = new ListNode(-1);
ListNode tmp = node;
while(l1 != null && l2 != null){
if(l1.data < l2.data){
tmp.next = l1;
l1 = l1.next;
tmp = tmp.next;
}else{
tmp.next = l2;
l2 = l2.next;
tmp = tmp.next;
}
}
if(l1 == null){
tmp.next = l2;
}
if(l2 == null){
tmp.next = l1;
}
return node.next;
}
}
雙向鏈表的實現:
import java.util.List;
class ListNode{
public int data;
public ListNode prev;
public ListNode next;
public ListNode(int data){
this.data = data;
}
}
public class DoubleList {
public ListNode head;
public ListNode last;
public DoubleList(){
this.head = null;
this.last = null;
}
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(this.head == null){
this.head = node;
this.last = node;
}else{
node.next = this.head;
this.head.prev = node;
this.head = node;
}
}
public void addLast(int data){
ListNode node = new ListNode(data);
if(this.head == null){
this.head = node;
this.last = node;
}else{
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
public int getLength(){
int count = 0;
ListNode cur = this.head;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
private ListNode searchIndex(int index){
int count = 0;
ListNode cur = this.head;
while(count < index){
cur = cur.next;
count++;
}
return cur;
}
public boolean addIndex(int index,int data){
if(index < 0 || index > getLength()){
System.out.println("下標錯誤");
return false;
}
if(index == 0){
addFirst(data);
return true;
}
if(index == getLength()){
addLast(data);
return true;
}
ListNode cur = searchIndex(index);
if(cur == null){
return false;
}
ListNode node = new ListNode(data);
node.next = cur;
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
return true;
}
public boolean contains(int key){
ListNode cur = this.head;
while(cur != null){
if(cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
//刪除第一次出現關鍵字爲key的節點
public int remove(int key){
int oldData = -1;//存儲要刪除的數字
ListNode cur = this.head;
while(cur != null){
if(cur.data == key){
oldData = cur.data;
//頭
if(cur == this.head){
this.head = this.head.next;
this.head.prev = null;
return oldData;
}else{
cur.prev.next = cur.next;
if(cur.next != null){
cur.next.prev = cur.prev;
}else{//尾
this.last = cur.prev;
}
return oldData;
}
}
cur = cur.next;
}
return -1;
}
//刪除所有值爲key的節點
public void removeAllKey(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.data == key) {
if (cur == this.head) {
this.head = this.head.next;
this.head.prev = null;
} else {
cur.prev.next = cur.next;
if (cur.next != null) {
cur.next.prev = cur.prev;
} else {//尾
this.last = cur.prev;
}
}
}
cur = cur.next;
}
}
//防止內存泄漏
public void clear(){
while(this.head != null){
ListNode cur = this.head.next;
this.head.next = null;
this.head.prev = null;
this.head = cur;
}
this.last = null;
}
public void display(){
ListNode cur = this.head;
while(cur != null){
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
DoubleList doubleList = new DoubleList();
doubleList.addFirst(1);
doubleList.addFirst(1);
doubleList.addFirst(2);
doubleList.addFirst(3);
doubleList.addFirst(3);
doubleList.addFirst(4);
doubleList.addIndex(0,99);
doubleList.addFirst(5);
doubleList.addFirst(5);
//5 99 4 3 2 1
doubleList.addIndex(2,199);
System.out.println(doubleList.remove(4));
doubleList.display();
doubleList.removeAllKey(1);
doubleList.display();
doubleList.clear();
}
}