Java实现循环链表
/**
* @auther: 巨未
* @DATE: 2019/1/4 0004 20:12
* @Description: 循环链表
*/
class ClinkDemo {
class Entry {
int data;
Entry next;
public Entry() {
this.data = -1;
this.next = null;
}
public Entry(int val) {
this.data = val;
this.next = null;
}
}
private Entry head = null;
public ClinkDemo() {
this.head = new Entry();//得到头结点的引用
this.head.next = this.head;
}
/*
*头插法
*/
public void insertHead(int val) {
Entry entry = new Entry(val);
entry.next = this.head.next;
this.head.next = entry;
}
/*
*尾插法
*/
public void insertTail(int val) {
Entry cur = this.head;
while (cur.next != this.head) {
cur = cur.next;
}
Entry entry = new Entry(val);
cur.next = entry;
entry.next = this.head; //让插入的next域指向头结点的地址
}
/*
*删除所有值为key的节点
*/
public void deleteAllkey(int key) {
Entry pre = this.head;
Entry cur = this.head.next;
while (cur != this.head) {
if (cur.data == key) {
pre.next = cur.next;
cur = cur.next;// cur = pre.next;
} else
pre = cur;
cur = cur.next;
}
}
/*
*打印
*/
public void show() {
Entry cur = this.head.next;
while (cur != this.head) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
}
public class TestCLinkDemo {
public static void main(String[] args) {
ClinkDemo clinkDemo = new ClinkDemo();
for (int i = 0; i < 5; i++) {
clinkDemo.insertHead(i);//头插 倒序
}
clinkDemo.show();
for (int i = 0; i < 5; i++) {
clinkDemo.insertTail(i); //尾插 正序
}
clinkDemo.show();
}
}
Java实现双向链表
/**
* @auther: 巨未
* @DATE: 2019/1/4 0004 20:43
* @Description: 双向链表
*/
class DlinkDemo{
class Entry{
int data;
Entry prev;
Entry next;
public Entry() { //
this.data = -1;
this.prev = null;
this.next = null;
}
public Entry(int val) {
this.data = val;
this.prev = null;
this.next = null;
}
}
private Entry head = null;
public DlinkDemo() {
this.head = new Entry();//得到头结点的引用
}
/*
*头插法 *************第一次插入
*/
public void insertHead(int val) {
Entry entry = new Entry(val); //插入的节点
entry.next = head.next;
this.head.next = entry;
entry.prev = this.head;
// entry.next.prev = entry; //java.lang.NullPointerException
// entry.next本来就是null,空指针异常
if(entry.next != null) {
entry.next.prev = entry;
}
}
/*
*尾插法
*/
public void insertTail(int val) {
Entry cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
Entry entry = new Entry(val);
cur.next = entry;
entry.prev = cur;
}
/*
*打印
*/
public void show() {
Entry cur = this.head.next;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
/*
*删除一个值为Key的节点****************最后一个
*/
public void deletekey(int key) {
Entry cur = this.head.next;
while (cur != null) { //遍历双向链表
if(cur.data == key) {
cur.next.prev = cur.prev; //删除节点的后继的前驱为 它的前驱
if (cur.next != null) {
cur.prev.next = cur.next; //删除节点的前驱的后继为 它的后继
} cur = cur.next;
}
}
}
}
public class TestDlinkDemo {
public static void main(String[] args) {
DlinkDemo dlinkDemo = new DlinkDemo();
/* for (int i = 0; i < 5 ; i++) {
dlinkDemo.insertHead(i);
}
dlinkDemo.show();*/
for (int i = 0; i < 5 ; i++) {
dlinkDemo.insertTail(i);
}
dlinkDemo.show();
dlinkDemo.deletekey(1);
}
}