/**
* Created with IntelliJ IDEA.
* Description:
* User: zhuzhuzhuchao
* Date: 2022-01-06
* Time: 14:02
*/
public class TestDemo {
// 作业20211029
// 2
// 报错:局部变量必须初始化
public static void main(String[] args){
String s;
System.out.println("s="+s);
}
// 无头双向链表
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(12);
myLinkedList.addLast(12);
myLinkedList.addLast(12);
myLinkedList.addLast(12);
myLinkedList.addLast(56);
myLinkedList.addLast(12);
myLinkedList.display();
System.out.println(myLinkedList.size());
//System.out.println(myLinkedList.contains(23));
myLinkedList.remove(12);
myLinkedList.display();
//myLinkedList.removeAllKey(12);
//myLinkedList.display();
myLinkedList.addIndex(2,13);
myLinkedList.display();
myLinkedList.clear();
myLinkedList.display();
}
// 面试问题总结
// 1.顺序表和链表的区别?
// 2.数组和链表的区别?
// 3.ArrayList和LinkedList的区别?
// 技巧:当面试官问到某某和某某的区别的时候,从共同点出发。
// 这两个数据结构的共同点 无非就是对数据的组织方式和描述方法不一样。
// 1.顺序表底层是一个数组,他是逻辑上和物理上都是连续的
// 2.链表是一个由若干节点组成的一个数据结构,逻辑上是连续的,物理上是不连续的
// 操作:1.顺序表适合,查找相关的操作,可以使用下标,直接获取到某个位置的元素;
// :2.链表适合于,频繁的插入和删除操作,此时不需要像顺序表一样,移动元素。链表的插入只需要修改指向即可
// :3.顺序表需要看满不满,满了要扩容,有可能浪费元素。空间利用率不高
// 带傀儡节点的双向链表
public static void main(String[] args) {
MyLinkedList1 myLinkedList1 = new MyLinkedList1();
// myLinkedList1.addFirst(1);
// myLinkedList1.addFirst(2);
// myLinkedList1.addFirst(3);
// myLinkedList1.addFirst(4);
// myLinkedList1.addFirst(5);
myLinkedList1.addLast(1);
myLinkedList1.addLast(1);
myLinkedList1.addLast(1);
myLinkedList1.addLast(1);
myLinkedList1.addLast(1);
myLinkedList1.display();
// myLinkedList1.addIndex(4,6);
myLinkedList1.display();
System.out.println(myLinkedList1.contains(1));
// myLinkedList1.remove(7);
// myLinkedList1.display();
// myLinkedList1.removeAllKey(1);
System.out.println("========================");
// myLinkedList1.display();
myLinkedList1.clean();
myLinkedList1.display();
}
}
// 1
// static方法不依赖于对象
class Test {
public static void hello() {
System.out.println("hello");
}
}
public class MyApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=null;
test.hello();
}
}
// 3
// static属于类变量
public class Test {
public int aMethod(){
static int i = 0;// 方法里不能定义static变量
i++;
return i;
}
public static void main(String args[]){
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
// 6
// 下面哪一项不是 java 类访问控制关键字(this)
/**
* Created with IntelliJ IDEA.
* Description:
* User: zhuzhuzhuchao
* Date: 2022-01-06
* Time: 15:34
*/
public class MyLinkedList {
public ListNode head;// 指向双向链表的头节点
public ListNode last;// 指向尾巴节点
//头插法
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;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data) {
ListNode node = new ListNode(data);
ListNode cur = this.head;
if (index < 0 || index >= size()) {
return;
}
if (index == 0) {
addFirst(data);
} else if (index == size()-1) {
addLast(data);
} else {
for (int i = 0; i < index; i++) {
cur = cur.next;
}
node.prev = cur.prev;
cur.prev.next = node;
node.next = cur;
cur.prev = node;
}
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
if (this.head == null) {
return false;
}
ListNode cur = this.head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
ListNode cur = this.head;
if (cur.next == null && cur.prev == null) {
if (cur.val == key) {
this.head = null;
this.last = null;
return;
}
}
while (cur != null && cur.val != key) {
cur = cur.next;
}
if (cur == null) {
return;
}else if (cur == this.head) {
this.head = this.head.next;
this.head.prev = null;
} else if (cur == this.last) {
this.last = this.last.prev;
this.last.next = null;
} else {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
}
// 博哥写
public void remove2(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.val == 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;
}
}
return;
}else {
cur = cur.next;
}
}
}
//删除所有值为key的节点
public void removeAllKey(int key) {
ListNode cur = this.head;
while (cur != null) {
remove(key);
cur = cur.next;
}
}
//得到单链表的长度
public int size() {
ListNode cur = this.head;
int count = 0;
while (cur != null) {
cur = cur.next;
count++;
}
return count;
}
public void display(){
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
public void clear() {
ListNode cur = this.head;
while (cur != null) {
ListNode curNext = cur.next;
cur.prev = null;
cur.next = null;
cur = curNext;
}
this.head = null;
this.last = null;
}
}
class ListNode {
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
/**
* Created with IntelliJ IDEA.
* Description:
* User: zhuzhuzhuchao
* Date: 2022-01-06
* Time: 21:57
*/
public class MyLinkedList1 {
public ListNode last;
public ListNode head = new ListNode(-1);
public void display() {
ListNode cur = this.head.next;
while (cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
// 头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
ListNode cur = this.head.next;
if (cur == null) {
this.head.next = node;
node.prev = this.head;
cur = node;
this.last = node;
} else {
this.head.next = node;
node.prev = this.head;
node.next = cur;
cur.prev = node;
}
}
// 尾插法
public void addLast(int data) {
ListNode node = new ListNode(data);
ListNode cur = this.head.next;
if (cur == null) {
this.head.next = node;
node.prev = this.head;
this.last = node;
} else {
node.prev = this.last;
this.last.next = node;
this.last = node;
}
}
// 任意节点插入
public void addIndex(int index,int data) {
ListNode node = new ListNode(data);
ListNode cur = this.head.next;
if (index < 0 || index >= size()) {
return;
}
if (index == 0) {
addFirst(data);
} else if (index == size()-1) {
addLast(data);
} else {
while (index != 0) {
index--;
cur = cur.next;
}
cur.prev.next = node;
node.prev = cur.prev;
node.next = cur;
cur.prev = node;
}
}
public int size() {
int count = 0;
ListNode cur = this.head.next;
while (cur != null) {
cur = cur.next;
count++;
}
return count;
}
// 查找是否包含关键字key
public boolean contains(int key) {
ListNode cur = this.head.next;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
// 删除第一次出现关键字为key的节点
public void remove(int key) {
ListNode cur = this.head.next;
// 只有一个节点,值为key,或不为key
if (cur.next == null) {
if (cur.val == key) {
cur.prev = null;
this.head.next = null;
this.last = null;
return;
}
return;
}
while (cur != null) {
if (cur.val == key) {
// 尾节点为key
if (cur.next == null) {
cur.prev.next = null;
this.last = cur.prev;
} else {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
return;
}
cur = cur.next;
}
}
// 删除所有值为key的节点
public void removeAllKey(int key) {
ListNode cur = this.head.next;
while (cur != null) {
if (cur.val == key) {
remove(key);
}
cur = cur.next;
}
}
public void clean() {
ListNode cur = this.head.next;
while (cur != null) {
ListNode curNext = cur.next;
cur.prev = null;
cur.next = null;
cur = curNext;
}
this.head.next = null;
this.last = null;
}
}
class ListNode {
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}