السلام عليكم ورحمه الله وبركاته
كتبت برنامج جافا يعمل العمليات التاليه :
حذف من اليمين
حذف من اليسار
وsort
بأستخدام Doubly linked list
بحيث ان يكون الان بوت راندم
واحسب الرن تايم للكل عمليه
كتبت كود ولكن الرن تايم تظهر اعداد كبير
الرجاء المساعده
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class DoublyLinkedList {
private Link first;
private Link last;
public DoublyLinkedList() {
first = null;
last = null;
}
public boolean isEmpty(){
return first == null;
}
public Link deleteFirst(){
Link temp = first;
if (first.next == null)
last = null;
else
first.next.previous = null;
first = first.next;
return temp;
}
public Link deleteLast(){
Link temp = last;
if (first.next == null)
first = null;
else
last.previous.next = null;
last = last.previous;
return temp;
}
public void display () {
System.out.print("List (first to last): ");
Link current = first; // start at beginning
while (current != null) // until end of list,
{
current.displayLink();
current = current.next; // move to next link
}
System.out.println("");
}
public void insertsort (long key){
Link newLink = new Link(key);
Link previous = null;
Link current = first;
if (isEmpty())
last = newLink;
else
while (current != null && key > current.dData) {
previous = current;
current = current.next;
}
if (previous == null)
first = newLink;
else
previous.next = newLink;
newLink.next = current;
}
public static void main(String[] args)throws IOException {
int ch ;
Scanner input = new Scanner(System.in);
int i = 0 ;
DoublyLinkedList list = new DoublyLinkedList();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int dqarr[] = new int [101];
double b =System.currentTimeMillis();
System.out.print( "Enter number of element :p" );
System.out.print(dqarr+ "\t");
int n =Integer.parseInt(br.readLine());
Random rand = new Random();
for( i = 0; i < n ; i ++ ) {
dqarr = rand.nextInt(3);
list.insertsort(dqarr);
}
list.display();
System.out.print("Effe");
double a =System.currentTimeMillis();
System.out.println(a-b);
list.deleteFirst();
list.display();
}
}
class Link {
public long dData; // data item
public Link next; // next link in list
public Link previous; // previous link in list
public Link(long d)
{
dData = d;
}
public void displayLink(){
System.out.print(dData + " ");
}
}