链结点(Link)
在链表中,每个数据项都被包含在在链结点中。一个链结点是某各类的对象,这个类可以叫做Link。
链表的java代码实现
class Link {//这里是链结点
public int iData;
public double dData;
public Link next;
public Link(int id, double dd) {
super();
iData = id;
dData = dd;
}
public void displayLink() {
System.out.println("{" + iData + "," + dData + "}");
}
}
class LinkList {//链表
private Link first;
public LinkList() {
first = null;
}
public boolean isEmpty() {
return (first == null);
}
//插入方法:作用在表头插入一个新链结点,这是最容易插入一个链接点的地方,因为first已经指向了第一个链接点。为了插入新的链结点,只需要是新创建的链结点的next字段等于原来的first值,然后改变first的值,使它指向新创建的链接点。
public void insertFirst(int id, double dd) {
Link newLink = new Link(id, dd);
newLink.next = first;
first = newLink;
}
//delete方法时插入方法的逆操作。它通过把first重新纸箱第二个链接点,断开了和第一个链接点的连接。通过查看第一个链结点的next字段可以找到第二个链接点。
public Link deleteFirst() {
Link temp = first;
first = first.next;
return temp;
}
public void displayList() {
System.out.println("List(first-->last)");
Link current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
/**
* 查找和删除指定的链结点用到的方法
*/
public Link find(int key) {
Link current = first;
while (current.iData != key) {
if (current.next == null) {
return null;
} else
current = current.next;
}
return current;
}
public Link delete(int key) {
Link current = first;
Link previous = first;
while (current.iData != key) {
if (current.next == null) {
return null;
} else {
previous = current;
current = current.next;
}
}
if (current == first)
first = first.next;
else
previous.next = current.next;
return current;
}
}
public class LinkListApp {
public static void main(String[] args) {
LinkList linkList = new LinkList();
linkList.insertFirst(22, 2.99);
linkList.insertFirst(44, 4.99);
linkList.insertFirst(66, 6.99);
linkList.insertFirst(88, 8.99);
linkList.displayList();
while (!linkList.isEmpty()) {
Link aLink = linkList.deleteFirst();
System.out.println("Delete ");
aLink.displayLink();
System.out.println("");
}
linkList.displayList();
}
}
双端链表相对于传统的链表,增加一个特性:挤兑最后一个链结点的引用相对第一个连接点的引用一样。就是说可以在最后插入节点。
class Link
{
public long dData; // data item
public Link next; // next link in list
// -------------------------------------------------------------
public Link(long d) // constructor
{ dData = d; }
// -------------------------------------------------------------
public void displayLink() // display this link
{ System.out.print(dData + " "); }
// -------------------------------------------------------------
} // end class Link
////////////////////////////////////////////////////////////////
class FirstLastList
{
private Link first; // ref to first link
private Link last; // ref to last link
// -------------------------------------------------------------
public FirstLastList() // constructor
{
first = null; // no links on list yet
last = null;
}
// -------------------------------------------------------------
public boolean isEmpty() // true if no links
{ return first==null; }
// -------------------------------------------------------------
public void insertFirst(long dd) // insert at front of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
last = newLink; // newLink <-- last
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
// -------------------------------------------------------------
public void insertLast(long dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if( isEmpty() ) // if empty list,
first = newLink; // first --> newLink
else
last.next = newLink; // old last --> newLink
last = newLink; // newLink <-- last
}
// -------------------------------------------------------------
public long deleteFirst() // delete first link
{ // (assumes non-empty list)
long temp = first.dData;
if(first.next == null) // if only one item
last = null; // null <-- last
first = first.next; // first --> old next
return temp;
}
// -------------------------------------------------------------
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
// -------------------------------------------------------------
} // end class FirstLastList
////////////////////////////////////////////////////////////////
class FirstLastApp
{
public static void main(String[] args)
{ // make a new list
FirstLastList theList = new FirstLastList();
theList.insertFirst(22); // insert at front
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11); // insert at rear
theList.insertLast(33);
theList.insertLast(55);
theList.displayList(); // display the list
theList.deleteFirst(); // delete first two items
theList.deleteFirst();
theList.displayList(); // display again
} // end main()
} // end class FirstLastApp
双向链表的Java实现
package com.exercise.dataStructureTest;
/** 链结点 */
class Node1 {
public long dData;
public Node1 next;
public Node1 previous;
public Node1(long dd) {
dData = dd;
}
public void displayLink() {
System.out.println(dData + " ");
}
}
class DoublyLinkedList {
private Node1 first;
private Node1 last;
public DoublyLinkedList() {
first = null;
last = null;
}
public boolean isEmpty() {
return first == null;
}
public void insertFirst(long dd) {
Node1 newNode1 = new Node1(dd);
if (isEmpty()) // if empty list,
last = newNode1; // newLink <-- last
else
first.previous = newNode1; // newLink <-- old first
newNode1.next = first; // newLink --> old first
first = newNode1; // first --> newLink
}
public void insertLast(long dd) {
Node1 newNode1 = new Node1(dd);
if (isEmpty()) {
first = newNode1;
} else {
last.next = newNode1;
newNode1.previous = last;
}
last = newNode1;
}
public Node1 deleteFirst() {
Node1 temp = first;
if (first.next == null) {
last = null;
} else {
first.next.previous = null;
}
first = first.next;
return temp;
}
public Node1 deleteLast() {
Node1 temp = last;
if (first.next == null) {
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
return temp;
}
public boolean insertAfter(long key, long dd) {
Node1 current = first;
while (current.dData != key) {
current = current.next;
if (current == null) {
return false;
}
}
Node1 newNode1 = new Node1(dd);
if (current == last) {
newNode1.next = null;
last = newNode1;
} else {
newNode1.next = current.next;
current.next.previous = newNode1;
}
newNode1.previous = current;
current.next = newNode1;
return true;
}
public Node1 deleteKey(long key) {
Node1 current = first;
while (current.dData != 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() {
System.out.println("List(first-->last):");
Node1 current = first;
while (current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
public void displayBackWord() {
System.out.println("List(last-->first):");
Node1 current = last;
while (current != null) {
current.displayLink();
current = current.previous;
}
System.out.println("");
}
}
public class doublyLinked {
public static void main(String[] args) {
DoublyLinkedList theList = new DoublyLinkedList();
theList.insertFirst(22);
theList.insertFirst(44);
theList.insertFirst(66);
theList.insertLast(11);
theList.insertLast(33);
theList.insertLast(55);
theList.displayForward();// 正向遍历
theList.displayBackWord();// 反向遍历
theList.deleteFirst();
theList.deleteLast();
theList.deleteKey(11);
theList.displayForward();
theList.insertAfter(22, 77);
theList.insertAfter(33, 88);
theList.displayForward();
}
}