链表-H-双向链表,允许前后遍历
来源:互联网 发布:pythonpath linux 编辑:程序博客网 时间:2024/06/07 05:35
package e_link.H_doublyLinked;
/**
* 双向链表,允许前后遍历
*
* @author Administrator
*/
public class DoublyLinkedApp {
public static void main(String[] args) {
DoublyLinkedList theList = new DoublyLinkedList();
theList.insertFirst(10);
theList.insertFirst(20);
theList.insertFirst(30);
theList.insertLast(80);
theList.insertLast(90);
theList.insertLast(100);
theList.insertAfter(30, 40);
theList.insertAfter(30, 60);
theList.displayForward();
System.out.println();
theList.displayBackward();
System.out.println();
theList.deleteFirst();
theList.deleteLast();
theList.deleteKey(60);
theList.displayForward();
System.out.println();
theList.displayBackward();
}
}package e_link.H_doublyLinked;
public class DoublyLinkedList {
private Link first;
private Link last;
public DoublyLinkedList() {
first = null;
last = null;
}
public boolean isEmpty() {
return first == null;
}
public void insertFirst(long dd) {
Link newLink = new Link(dd);
if (isEmpty()) {
last = newLink;
} else {
first.previous = newLink;
}
newLink.next = first;
first = newLink;
}
public void insertLast(long dd) {
Link newLink = new Link(dd);
if (isEmpty()) {
first = newLink;
} else {
last.next = newLink;
newLink.previous = last;
}
last = newLink;
}
public Link deleteFirst() {
Link temp = first;
if (first.next == null) {
last = null;
} else {
first.next.previous = null;
}
first = first.next;
return temp;
}
public Link deleteLast() {
Link temp = last;
if (first.next == null) {
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
return temp;
}
/*
* 在key后面添加数据
*/
public boolean insertAfter(long key, long dd) {
Link current = first;
while (current.data != key) {
current = current.next;
if (current == null)
return false;
}
Link newLink = new Link(dd);
if (current == last) {
newLink.next = null;
last = newLink;
} else {
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}
/*
* 删除值为key的对象
*/
public Link deleteKey(long key) {
Link current = first;
while (current.data != key) {
current = current.next;
if (current == null)
return null;
}
//干掉前面
if (current == first) {
first = current.next;
} else {
current.previous.next = current.next;
}
//连接后面
if (current == last) {
last = current.previous;
} else {
current.next.previous = current.previous;
}
return current;
}
public void displayForward() {
Link link = first;
while (link != null) {
link.displayLink();
link = link.next;
}
}
public void displayBackward() {
Link link = last;
while (link != null) {
link.displayLink();
link = link.previous;
}
}
}package e_link.H_doublyLinked;
public class Link {
public long data;
public Link next;
public Link previous;
public Link(long dd) {
data = dd;
}
public void displayLink() {
System.out.print(data + " ");
}
}
/**
* 双向链表,允许前后遍历
*
* @author Administrator
*/
public class DoublyLinkedApp {
public static void main(String[] args) {
DoublyLinkedList theList = new DoublyLinkedList();
theList.insertFirst(10);
theList.insertFirst(20);
theList.insertFirst(30);
theList.insertLast(80);
theList.insertLast(90);
theList.insertLast(100);
theList.insertAfter(30, 40);
theList.insertAfter(30, 60);
theList.displayForward();
System.out.println();
theList.displayBackward();
System.out.println();
theList.deleteFirst();
theList.deleteLast();
theList.deleteKey(60);
theList.displayForward();
System.out.println();
theList.displayBackward();
}
}package e_link.H_doublyLinked;
public class DoublyLinkedList {
private Link first;
private Link last;
public DoublyLinkedList() {
first = null;
last = null;
}
public boolean isEmpty() {
return first == null;
}
public void insertFirst(long dd) {
Link newLink = new Link(dd);
if (isEmpty()) {
last = newLink;
} else {
first.previous = newLink;
}
newLink.next = first;
first = newLink;
}
public void insertLast(long dd) {
Link newLink = new Link(dd);
if (isEmpty()) {
first = newLink;
} else {
last.next = newLink;
newLink.previous = last;
}
last = newLink;
}
public Link deleteFirst() {
Link temp = first;
if (first.next == null) {
last = null;
} else {
first.next.previous = null;
}
first = first.next;
return temp;
}
public Link deleteLast() {
Link temp = last;
if (first.next == null) {
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
return temp;
}
/*
* 在key后面添加数据
*/
public boolean insertAfter(long key, long dd) {
Link current = first;
while (current.data != key) {
current = current.next;
if (current == null)
return false;
}
Link newLink = new Link(dd);
if (current == last) {
newLink.next = null;
last = newLink;
} else {
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}
/*
* 删除值为key的对象
*/
public Link deleteKey(long key) {
Link current = first;
while (current.data != key) {
current = current.next;
if (current == null)
return null;
}
//干掉前面
if (current == first) {
first = current.next;
} else {
current.previous.next = current.next;
}
//连接后面
if (current == last) {
last = current.previous;
} else {
current.next.previous = current.previous;
}
return current;
}
public void displayForward() {
Link link = first;
while (link != null) {
link.displayLink();
link = link.next;
}
}
public void displayBackward() {
Link link = last;
while (link != null) {
link.displayLink();
link = link.previous;
}
}
}package e_link.H_doublyLinked;
public class Link {
public long data;
public Link next;
public Link previous;
public Link(long dd) {
data = dd;
}
public void displayLink() {
System.out.print(data + " ");
}
}
0 0
- 链表-H-双向链表,允许前后遍历
- 遍历双向链表
- 整理一个双向链表list.h
- 双向链表list.h升序排序
- 双向链表的插入、删除、遍历
- 内核双向链表list.h中的list_entry
- 浅谈list.h头文件之双向循环链表
- WHU1478----2014年武大邀请赛H题----双向链表
- 算法导论10.2-8-用一个整数地址替代前后指针实现双向链表
- 【二叉树->链表】二叉树结构转双向线性链表结构(先序遍历)
- windbg之使用!list指令遍历双向链表
- 单双向链表的构建与遍历
- 二叉树转双向链表+BFS+中序遍历
- 单链表/双向链表的建立/遍历/插入/删除
- 链表-双向链表
- 中序遍历二叉树将遍历结果存入双向循环链表C语言实现
- 中前后序遍历
- 只允许遍历链表一遍,定位链表倒数第m个元素
- 链表-F-有序链表
- 链表-G- 利用有序链表给数组排序
- Unity3d调用Anroid(Android Studio环境)方法
- Javascript如何实现二分查找?
- Log4net配置与使用简要说明
- 链表-H-双向链表,允许前后遍历
- 高质量的SRS的一些特性
- 链表-I-链表实现迭代器
- MySQL数据库的子查询
- 常见中外出版社
- 链表-j-优先级队列,单向链表实现
- Python-opencv学习
- 链表-K-链表实现 双端队列
- Python学习之日志模块详解