1.复杂方法的图分析
文章来源:https://www.toymoban.com/news/detail-724884.html
2.My_LinkedList代码
package My_liNKEDlIST;
public class My_LinkedList implements MY_lIST{
static class ListNode{
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val){
this.val = val;
}
}
public ListNode head;
public ListNode last;
@Override
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(head == null){
head = node;
last = node;
}
else{
node.next = head;
head.prev = node;
head = node;
}
}
@Override
public void addLast(int data) {
ListNode node = new ListNode(data);
if(head == null){
head = node;
last = node;
}
else{
last.next = node;
node.prev = last;
last = node;
}
}
@Override
public void addIndex(int index, int data) throws IndexOutOFException{
int len = size();
if(index < 0 || index > len){
throw new IndexOutOFException("越界了");
}
if(index == 0){
addFirst(data);
return;
}
if(index == len){
addLast(data);
return;
}
ListNode cur = head;
ListNode node = new ListNode(data);
while(index != 0){
cur = cur.next;
index--;
}
cur.prev.next = node;
node.next = cur;
node.prev = cur.prev;
cur.prev = node;
}
@Override
public boolean contains(int key) {
ListNode cur = head;
while(cur != null){
if(cur.val == key){
return true;
}
cur = cur.next;
}
return false;
}
@Override
public void remove(int key) {
ListNode cur = head;
while(cur != null){
if(cur.val == key){
//如果删除的结点为头结点
if(cur == head){
head = head.next;
if(head != null) {
head.prev = null;
}
}else {
if(cur == last){
cur.prev.next = cur.next;
last = last.prev;
}else {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
}
return;
}
else{
cur = cur.next;
}
}
}
@Override
public void removeAllKey(int key) {
ListNode cur = head;
while(cur != null){
if(cur.val == key){
//如果删除的结点为头结点
if(cur == head){
head = head.next;
if(head != null) {
head.prev = null;
}
}else {
if(cur.next == null) {
cur.prev.next = cur.next;
}else {
cur.next.prev = cur.prev;
}
}
}
cur = cur.next;
}
}
@Override
public int size() {
ListNode cur = head;
int count = 0;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
@Override
public void display() {
ListNode cur = head;
while(cur != null){
System.out.println(cur.val+" ");
cur = cur.next;
}
}
@Override
public void clear() {
ListNode cur = head;
while(cur != null){
ListNode tmp = cur.next;
cur.prev = null;
cur.next = null;
cur = tmp;
}
this.head = null;
this.last = null;
}
}
3.接口MY_lIST
public interface MY_lIST {
// 2、无头双向链表实现
//头插法
public void addFirst(int data);
//尾插法
public void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data);
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key);
//删除第一次出现关键字为key的节点
public void remove(int key);
//删除所有值为key的节点
public void removeAllKey(int key);
//得到单链表的长度
public int size();
public void display();
public void clear();
}
4.测试类
public class Test {
public static void main(String[] args) {
My_LinkedList LIST = new My_LinkedList();
LIST.addFirst(1);
LIST.addFirst(1);
LIST.addFirst(1);
//LIST.display();
LIST.addLast(4);
LIST.addLast(5);
LIST.addLast(6);
//LIST.display();
LIST.addIndex(4,7);
//LIST.display();
//LIST.clear();
//LIST.display();
//LIST.remove(4);
LIST.removeAllKey(1);
LIST.display();
}
}
双链表的思路和单链表大同小异,就是多加了一个前驱指针,希望大家好好学习双链表的代码文章来源地址https://www.toymoban.com/news/detail-724884.html
到了这里,关于数据结构之双链表的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!