链表-K-链表实现 双端队列
来源:互联网 发布:sql if else语句格式 编辑:程序博客网 时间:2024/06/05 07:14
package e_link.J_FirstLastQue;
/**
* 链表实现 双端队列
*
* @author Administrator
*
*/
public class DequeApp {
public static void main(String[] args) {
Deque theDeque = new Deque();
theDeque.insertRight(453);
theDeque.insertRight(453);
theDeque.insertRight(4);
theDeque.insertRight(453);
theDeque.insertRight(417);
theDeque.insertRight(173);
theDeque.insertLeft(453);
theDeque.insertLeft(453);
theDeque.removeLeft();
theDeque.insertRight(103);
theDeque.insertRight(3);
while (!theDeque.isEmpty()) {
System.out.print(theDeque.removeRight() + " ");
}
}
}package e_link.J_FirstLastQue;
public class Deque {
private DoublyLinkedList theDL;
public Deque() {
theDL = new DoublyLinkedList();
}
public void insertLeft(int key) {
theDL.insertFirst(key);
}
public void insertRight(int key) {
theDL.insertLast(key);
}
public int removeLeft() {
int temp = theDL.deleteLast();
return temp;
}
public int removeRight() {
int temp = theDL.deleteFirst();
return temp;
}
public boolean isEmpty() {
return theDL.isEmpty();
}
}package e_link.J_FirstLastQue;
public class DoublyLinkedList {
private Link first;
private Link last;
public DoublyLinkedList() {
first = null;
last = null;
}
public void insertFirst(int k) {
Link newLink = new Link(k);
if (isEmpty())
last = newLink;
else
first.previous = newLink;
newLink.next = first;
first = newLink;
}
public void insertLast(int k) {
Link newLink = new Link(k);
if (isEmpty())
first = newLink;
else {
newLink.previous = last;
last.next = newLink;
}
last = newLink;
}
/**
* 值是key节点的后面添加dData
*/
public boolean insertAfter(int key, int dData) {
Link newLink = new Link(dData);
Link current = first;
while (current != null) {
if (current.iData == key)
break;
current = current.next;
}
boolean flag = true;
if (current != null) {
if (current == last) {
newLink.next = null;
last = newLink;
} else {
current.next.previous = newLink;
newLink.next = current.next;
}
current.next = newLink;
newLink.previous = current;
} else {
flag = false;
}
return flag;
}
/**
* 删除关键字为key的节点 考虑first和last
*
* @param key
*/
public Link deleteKey(int key) {
Link current = first;
while (current != null) {
if (current.iData == key)
break;
current = current.next;
}
// 连接后面
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 int deleteFirst() {
Link temp = first;
if (first.next == null) {
last = null;
} else {
// first.previous = null;]
first.next.previous = null;
}
// 提取共同点
first = first.next;
return temp.iData;
}
public int deleteLast() {
Link temp = last;
if (last.previous == null) {
first = null;
} else {
// last.next = null;
last.previous.next = null;
}
// 提取共同点
last = last.previous;
return temp.iData;
}
public void displayForward() {
Link current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println();
}
public void displayBackward() {
Link current = last;
while (current != null) {
current.displayLink();
current = current.previous;
}
System.out.println();
}
public boolean isEmpty() {
return first == null;
}
}package e_link.J_FirstLastQue;
public class Link {
public int iData;
public Link next;
public Link previous;
public Link(int iData) {
this.iData = iData;
}
public void displayLink() {
System.out.print(iData + " ");
}
}
/**
* 链表实现 双端队列
*
* @author Administrator
*
*/
public class DequeApp {
public static void main(String[] args) {
Deque theDeque = new Deque();
theDeque.insertRight(453);
theDeque.insertRight(453);
theDeque.insertRight(4);
theDeque.insertRight(453);
theDeque.insertRight(417);
theDeque.insertRight(173);
theDeque.insertLeft(453);
theDeque.insertLeft(453);
theDeque.removeLeft();
theDeque.insertRight(103);
theDeque.insertRight(3);
while (!theDeque.isEmpty()) {
System.out.print(theDeque.removeRight() + " ");
}
}
}package e_link.J_FirstLastQue;
public class Deque {
private DoublyLinkedList theDL;
public Deque() {
theDL = new DoublyLinkedList();
}
public void insertLeft(int key) {
theDL.insertFirst(key);
}
public void insertRight(int key) {
theDL.insertLast(key);
}
public int removeLeft() {
int temp = theDL.deleteLast();
return temp;
}
public int removeRight() {
int temp = theDL.deleteFirst();
return temp;
}
public boolean isEmpty() {
return theDL.isEmpty();
}
}package e_link.J_FirstLastQue;
public class DoublyLinkedList {
private Link first;
private Link last;
public DoublyLinkedList() {
first = null;
last = null;
}
public void insertFirst(int k) {
Link newLink = new Link(k);
if (isEmpty())
last = newLink;
else
first.previous = newLink;
newLink.next = first;
first = newLink;
}
public void insertLast(int k) {
Link newLink = new Link(k);
if (isEmpty())
first = newLink;
else {
newLink.previous = last;
last.next = newLink;
}
last = newLink;
}
/**
* 值是key节点的后面添加dData
*/
public boolean insertAfter(int key, int dData) {
Link newLink = new Link(dData);
Link current = first;
while (current != null) {
if (current.iData == key)
break;
current = current.next;
}
boolean flag = true;
if (current != null) {
if (current == last) {
newLink.next = null;
last = newLink;
} else {
current.next.previous = newLink;
newLink.next = current.next;
}
current.next = newLink;
newLink.previous = current;
} else {
flag = false;
}
return flag;
}
/**
* 删除关键字为key的节点 考虑first和last
*
* @param key
*/
public Link deleteKey(int key) {
Link current = first;
while (current != null) {
if (current.iData == key)
break;
current = current.next;
}
// 连接后面
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 int deleteFirst() {
Link temp = first;
if (first.next == null) {
last = null;
} else {
// first.previous = null;]
first.next.previous = null;
}
// 提取共同点
first = first.next;
return temp.iData;
}
public int deleteLast() {
Link temp = last;
if (last.previous == null) {
first = null;
} else {
// last.next = null;
last.previous.next = null;
}
// 提取共同点
last = last.previous;
return temp.iData;
}
public void displayForward() {
Link current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println();
}
public void displayBackward() {
Link current = last;
while (current != null) {
current.displayLink();
current = current.previous;
}
System.out.println();
}
public boolean isEmpty() {
return first == null;
}
}package e_link.J_FirstLastQue;
public class Link {
public int iData;
public Link next;
public Link previous;
public Link(int iData) {
this.iData = iData;
}
public void displayLink() {
System.out.print(iData + " ");
}
}
0 0
- 链表-K-链表实现 双端队列
- 双端队列(deque)链表实现
- 双端队列(dequeue)链表实现
- 链表实现队列
- 链表实现队列
- 链表实现队列
- 链表实现队列
- 链表实现队列
- 队列:链表实现
- 使用双向链表实现双端队列
- java 双向链表实现双端队列
- leetcode 23. Merge k Sorted Lists 合并k个排序链表 优先级队列
- 各种实现队列的问题:链表实现,栈实现队列及队列实现栈
- 2016ICPC 青岛网赛 Sort 二分+K叉哈夫曼+双队列实现
- 双端队列实现分析
- java实现双端队列
- 双端队列的实现
- 双端队列实现分析
- 链表-I-链表实现迭代器
- MySQL数据库的子查询
- 常见中外出版社
- 链表-j-优先级队列,单向链表实现
- Python-opencv学习
- 链表-K-链表实现 双端队列
- Python学习之日志模块详解
- Windows Server2003本地用户的批量导入和导出
- 链表-L-循环链表
- 基于机器学习的网络异常流量识别系统——工具篇
- 504. Base 7
- SpringMVC入门之六:使用Thymeleaf作为视图
- 指针和引用的区别
- 链表-M-Josephus