📄 dlinkedlista.java
字号:
package myList;
public class DLinkedListA
{
// really simple, contains just one reference to head element
DElemA head;
DElemA tail;
DLinkedListA() //simple constructor
{
System.out.println("constructing new DoublyLinkedList for Author");
head = new DElemA();
tail = new DElemA();
head = null;
tail = head;
}
//*********************************************
public boolean emptyList()
{
return head == null;
}
//*********************************************
public void setHead(DElemA h)
{
head = h;
tail = h;
}
//*********************************************
/*
public DElemA getHead()
{
return head;
}
*/
//*********************************************
/*
public void insertFirst(DElemA el)
{
el.setNext(head);
el.setPrevous(head.previous);
head.previous = el;
head = el;
tail = el;
}
*/
//*********************************************
/*
public DElemA lastNode()
{
// returns last DElemAent of list, null if empty list
DElemA l;
if (emptyList()) return null;
l = head;
while (l.getNext() != null) l = l.getNext();
return l;
}
*/
//*********************************************
public void insert(DElemA d)
{
tail.next = d;
d.previous = tail;
d.next = head;
head.previous =d;
tail = d;
}
public void delete(DElemA d)
{
d.previous.next = d.next;
d.next.previous = d.previous;
}
/*
public void insertLast(DElemA l) {
// for an empty list insertlast is insertfirst!
if (emptyList())
insertFirst(l);
else //define the next-reference of the last DElemAent
{
tail.next = l;
l.previous = tail;
l.next = head;
head.previous = l;
tail = l;
}
}
*/
//*********************************************
/*
public void removeFirst(DElemA d)
{
//pre: list not empty!
//post: first DElemAent removed and returned in d
if (!emptyList())
{
d.setTitle(head.getTitle());
d.setNext(head.getNext());
head = head.getNext();
}
else System.out.println("removeFirst: Error: empty list!");
*/
//*********************************************
/*
public void removeLast(DElemA d)
{
//pre: list not empty!
//post: last DElemAent removed and returned in d
DElemA l, l1;
if (!emptyList())
{l = head; l1 = l;
if (l.getNext() == null)
{
head = null;
}
else
{
while (l.getNext()!= null)
{
l1 = l; l = l.getNext();
}
l1.setNext(null);
}
d.setTitle(l.getTitle());
d.setNext(null);
}
else System.out.println("removeLast: Error: empty list!");
}
*/
//*********************************************
/*
public DLinkedListA copyList()
{
//returns the copy of the list as theNewList
DLinkedListA theNewList;
DElemA copy, old;
theNewList = new DLinkedListA(); // make a new list
if (!emptyList())
{
copy = new DElemA();
theNewList.setHead(copy); //define the new head
old = head;
copy.setTitle(old.getTitle()); //copy the title
while(old.getNext() != null)
{
copy.setNext(new DElemA()); //there is another DElemAent to copy
//next is set to null in the constructor!
copy = copy.getNext(); //move to the next destination
old = old.getNext(); //move to next source DElemAent
copy.setTitle(old.getTitle());// copy the title
}
}
return theNewList;
}
*/
//*********************************************
/*
public DElemA first()
{
//pre: list not empty
//post: list unchanged head DElemAent returned
if (!emptyList()) System.out.println("First: Error: Empty List!!");
return head;
}
*/
//*********************************************
/*
public DLinkedListA tail()
{
//pre: list not empty
//post: list unchanged, reference to tail returned
DLinkedListA l;
if (emptyList())
{System.out.println("Tail: Error: Empty List!!");
return null;
}
else
{
l = new DLinkedListA();
l.setHead(head.getNext());
return l;
}
}
*/
/*
public DElemA lookForAuthor(String t)
{
//pre: list not empty.
//post: list unchanged, if found elent with matching key returned
// if not found return null
DElemA l;
if (emptyList())
{
System.out.println("lookForAuthor: Error: Empty List!!");
return null;
}
else
{
l = head;
while ((l != null) && (l.getAuthor().equals(t) == false))
l = l.getNext();
if (l != null) //found it
return l;
else //too bad
{
System.out.println("lookForAuthor Error: Author not found!!");
return null;
}
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -