链表(数据结构--Java版)

[color=red][b]1.普通链表的实现[/b][/color]

//链表节点类
class LinkedListNode {
int info;
LinkedListNode link;
}
//链表类
public class LinkedListClass {
protected LinkedListNode first, last;
protected int count;
public LinkedListClass() {
first = null;
last = null;
count = 0;
}
public LinkedListClass(LinkedListClass otherList) {
copy(otherList);
}
public void initializeList() {
first = null;
last = null;
count = 0;
}
/* 输出链表 */
public void print() {
LinkedListNode current;
current = first;
while (current != null) {
System.out.println(current.info + " ");
current = current.link;
}
}
/* 链表的长度 */
public int length() {
return count;
}
/* 链表是否为空 */
public boolean isEmptyList() {
return (first == null);
}
/* 检索第一个节点 */
public int front() {
int temp = first.info;
return temp;
}
/* 检索最后一个节点 */
public int back() {
int temp = last.info;
return temp;
}
/* 正向构建链表,向链表尾插入节点 */
public void insertlLast(int newIntem) {
LinkedListNode newNode;
newNode = new LinkedListNode();
newNode.info = newIntem;
newNode.link = null;
if (first == null) {
first = newNode;
last = newNode;
} else {
last.link = newNode;
last = newNode;
}
count++;
}
/* 反向构建链表,向链表头部插入新节点 */
public void insertFirst(int newItem) {
LinkedListNode newNode;

newNode = new LinkedListNode();
newNode.info = newItem;

newNode.link = first;
first = newNode;

if (last == null)// 如果链表为空,新插入的newNode也是链表中的最后一个节点
last = newNode;
count++;
}
/* 复制链表 */
private void copy(LinkedListClass otherList) {
LinkedListNode newNode;
LinkedListNode current;

first = null;
if (otherList.first == null) {
first = null;
last = null;
count = 0;
} else {
count = otherList.count;
current = otherList.first;
first = new LinkedListNode();
first.info = current.info;
first.link = null;
last = first;
current = current.link;

while (current != null) {
newNode = new LinkedListNode();
newNode.info = current.info;
newNode.link = null;

last.link = newNode;
last = newNode;

current = current.link;
}
}
}
/* 拷贝链表的方法 */
public void copyList(LinkedListClass otherList) {
if (this != otherList)
copy(otherList);
}
}


[color=red][b]2.有序链表的实现[/b][/color]

*有序链表*/
public class OrderedLinkedList extends LinkedListClass {

public OrderedLinkedList(){
super();
}
public OrderedLinkedList(OrderedLinkedList otherList){
super(otherList);
}
/*
* 搜索链表 1.将搜索项与链表中的当前节点相比较,如果当前节点的info大于等于serachItem,则停止搜索 否则,就把下一个节点变成当前节点。
*/
public boolean search(int searchItem) {
LinkedListNode current;
boolean found;
current = first;
found = false;
while (current != null && !found) {
if (current.info >= searchItem)
found = true;
else
current = current.link;
}
if (found)
found = (current.info == searchItem);
return found;
}

/*
* 插入节点
* 1. 链表最初为空
* 2. 链表为非空,且新元素小于链表中最小的元素
* 3. 链表为非空,且待插入的元素比链表中的第一个元素大,改元素应插入链表中的某个位置
* a. 新项大于链表中的所有元素,插入链表末尾
* b. 新项插入链表中间的某个位置
*/
public void insertNode(int insertItem) {
LinkedListNode current;
LinkedListNode trailCurrent;
LinkedListNode newNode;

boolean found;
newNode = new LinkedListNode();
newNode.info = insertItem;
newNode.link = null;

if (first == null) {
first = newNode;
count++;
} else {
trailCurrent = first;
current = first;
found = false;

while (current != null && !found)
if (current.info >= insertItem)
found = true;
else {
trailCurrent = current;
current = current.link;
}
if (current == first) {
newNode.link = first;
first = newNode;
count++;
} else {
trailCurrent.link = newNode;
newNode.link = current;
count++;
}
}
}

/*
* 删除节点
* 1. 链表为空
* 2. 待删除项的元素包含在链表的第一个节点中,须调整first的值
* 3. 待删除项在链表的摸个位置
* 4. 链表为非空,待删除的节点不在链表中
*/
public void deleteNode(int deleteItem) {
LinkedListNode current;
LinkedListNode trailCurrent;
boolean found;

if (first == null)// Case 1
System.out.println("Error!");
else {
if (first.info == deleteItem) {// Case 2
first = first.link;
count--;
} else {
found = false;
trailCurrent = first;
current = first.link;
while (current != null && !found) {
if (current.info >= deleteItem)
found = true;
else {
trailCurrent = current;
current = current.link;
}
}
if (current == null)// Case 4
System.out.println("deleteItem is not in the list!");
else if (current.info == deleteItem) {
if (first == current) {// Case 2
first = first.link;
count--;
} else {// Case 3
trailCurrent.link = current.link;
count--;
}
} else
// Case 4
System.out.println("deleteItem is not in the list!");
}
}
}
}

[color=red][b]3.无序链表的实现[/b][/color]

/*无序链表*/
public class UnorderedLinkedList extends LinkedListClass {

public UnorderedLinkedList(){
super();
}
public UnorderedLinkedList(UnorderedLinkedList otherList){
super(otherList);
}
/* 查找节点 */
public boolean search(int searchItem) {
LinkedListNode current;
boolean found;
current = first;
found = false;

while (current != null && !found) {
if (current.info == searchItem)
found = true;
else
current = current.link;
}
return found;
}

/* 删除节点
* 1. 链表为空
* 2. 链表非空,且删除的节点是第一个节点
* a. 链表只有一个节点
* b. 链表含有多个节点
* 3. 待删除的节点不是第一个节点
* a. 待删除的节点不是最后一个节点
* b. 待删除节点是最后一个节点
* 4. 待删除节点不在链表中
* */
public void deleteNode(int deleteItem) {
LinkedListNode current;
LinkedListNode trailCurrent;
boolean found;

if (first == null)
System.out.println("Error!");
else {
if (first.info == deleteItem) {
first = first.link;
if (first == null)
last = null;
count--;
} else {
found = false;
trailCurrent = first;
current = first.link;
while (current != null && !found) {
if (current.info == deleteItem)
found = true;
else {
trailCurrent = current;
current = current.link;
}
}
if (found) {
count--;
trailCurrent.link = current.link;
if (last == current)
last = trailCurrent;
} else
System.out.println("deletItem is not in the list!");
}
}
}

}

[color=red][b]4.双向链表的实现[/b][/color]


/*双向链表*/
class DoublyLinkedNode {
int info;

DoublyLinkedNode next;

DoublyLinkedNode back;
}

public class DoublyLinkedClass {

int count;

DoublyLinkedNode first;

DoublyLinkedNode last;

public DoublyLinkedClass() {
first = null;
last = null;
count = 0;
}

public void initializeList() {
first = null;
last = null;
count = 0;
}

/* 链表是否为空 */
public boolean isEmpty() {
return (first == null);
}

/* 链表的长度 */
public int length() {
return count;
}

/* 输出链表 */
public void print() {
DoublyLinkedNode current;
current = first;
while (current != null) {
System.out.println(current.info + " ");
current = current.next;
}
}

/* 反向输出链表 */
public void reversePrint() {
DoublyLinkedNode current;
current = last;
while (current != null) {
System.out.println(current.info + " ");
current = current.back;
}
}

/* 搜索链表 */
public boolean search(int searchItem) {
boolean found;
DoublyLinkedNode current;
found = false;
current = first;
while (current != null && !found)
if (current.info >= searchItem)
found = true;
else
current = current.next;
if (found)
found = (current.info == searchItem);
return found;
}

/*
* 插入节点
* 1. 在一个空链表中插入
* 2. 在一个非空链表的开始处插入
* 3. 在一个非空链表的末尾处插入
* 4. 在一个非空链表的中间某个位置插入
*/
public void insertNode(int insertInterm) {
DoublyLinkedNode current;
DoublyLinkedNode trailCurrent = null;
DoublyLinkedNode newNode;
boolean found;

newNode = new DoublyLinkedNode();

newNode.info = insertInterm;
newNode.next = null;
newNode.back = null;

if (first == null) {
first = newNode;
last = newNode;
count++;
} else {
found = false;
current = first;
while (current != null && !found) {
if (current.info >= insertInterm)
found = true;
else {
trailCurrent = current;
current = current.next;
}
}
if (current == first) {
first.back = newNode;
newNode.next = first;
first = newNode;
count++;
} else {
if (current != null) {
trailCurrent.next = newNode;
newNode.back = trailCurrent;
newNode.next = current;
current.back = newNode;
} else {
trailCurrent.next = newNode;
newNode.back = trailCurrent;
last = newNode;
}
count++;
}
}
}

/*
* 删除节点
* 1. 链表为空
* 2. 待删除项是链表中的第一个节点,须改变first的值
* 3. 待删除项在链表中的某个位置
* 4. 待删除项不再链表中
*/
public void deleteNode(int deleteItem) {
DoublyLinkedNode current;
DoublyLinkedNode trailCurrent;
boolean found;

if (first == null)
System.err.println("Cannot delete from an empty list.");
else if (first.info == deleteItem) {
current = first;
first = first.next;
if (first != null)
first.back = null;
else
last = null;
count--;
} else {
found = false;
current = first;
while (current != null && !found)
if (current.info >= deleteItem)
found = true;
else
current = current.next;
if (current == null)
System.out.println("deleteItem is not in the list!");
else if (current.info == deleteItem) {
trailCurrent = current.back;
trailCurrent.next = current.next;

if (current.next != null)
current.next.back = trailCurrent;
if (current == last)
last = trailCurrent;
count--;
} else
System.out.println("deleteItem is not in the list!");
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值