Etiketler
package lab5;
public class DLinkedList extends Object{
Node head;
void addFirst(int data){
Node n = new Node(data);
if( head == null){
head = n;
}
else{
n.next = head;
head.prev = n;
head = n;
}
}
void addLast(int data){
Node n = new Node(data);
if( head == null){
head = n;
}
else{
Node temp = head;
while( temp.next != null){
temp = temp.next;
}
temp.next = n;
n.prev = temp;
}
}
void insertAfter(int data, int position){
if( position < 0 )
throw new LinkedListException(“Position cannot be negative”);
if(head == null)
throw new LinkedListException(“LinkedList is empty”);
Node n = new Node(data);
Node temp = head;
int i = 0;
while( position > i && temp != null){
temp = temp.next;
i++;
}
if(temp == null)
throw new LinkedListException(“Position greater than list size”);
if( temp.next != null){
n.next = temp.next;
temp.next.prev = n;
}
temp.next = n;
n.prev = temp;
}
Node removeFirst(){
if(head == null)
throw new LinkedListException(“LinkedList is empty”);
Node temp = head;
head = head.next;
head.prev = null;
return temp;
}
Node removeLast(){
if(head == null)
throw new LinkedListException(“LinkedList is empty”);
Node temp = head;
if( head.next == null){
head = null;
}
else{
while( temp.next != null){
temp = temp.next;
}
temp.prev.next = null;
temp.prev = null;
}
return temp;
}
void printList(){
if( head == null){
System.out.println(“Empty dlist”);
}
else{
Node temp = head;
while( temp!= null){
System.out.println(temp.data);
temp = temp.next;
}
}
}
protected DLinkedList clone(){
return this;
}
class LinkedListException extends RuntimeException{
LinkedListException(String message){
super(message);
}
}
public static void main(String[] args) {
DLinkedList dlist = new DLinkedList();
dlist.addFirst(3);
dlist.insertAfter(94, 0);
dlist.printList();
}
}