java版的數據結構
㈠ 什麼是java數據結構
數據結構不是JAVA專有.
應該說數據結構是獨立於某個具體語言的.
單從JAVA來說,就是用JAVA語言的語法方式,來表達一個對象應該具有什麼樣的表現形式.
㈡ Java中的數據結構有哪些
List相關:包括ArrayList(基於數組),LinkedList(基於鏈表),Stack等
Map相關:包括TreeMap,HashMap等
Set相關:包括TreeSet,HashSet等
總的來說,常見數據結構Java集合框架中都有實現。
㈢ java數據結構
首先明確,帶權路徑長度WPL最小的二叉樹稱作最優二叉樹或哈夫曼樹
那麼比如說有4個節點,分別帶權7,5,2,4如下ab兩圖
WPLa=7*2+5*2+2*2+4*2=36
WPLb=7*1+5*2+2*3+4*3=35
WPL=30*2+5*5*4+8*4*15*3+15*2+27*2=
不算了 口算不行... 看上式也知道你出現的概率越大,相當於基地越大,就給你乘個小的代價,必然是最優的。
㈣ java 數據結構
<數據結構與java集合框架>第二版中就有
自己可以查!
當然為了分在麻煩也要寫:
//Queue.java
import java.util.*;
public class Queue {
protected LinkedList list;
// Postcondition: this Queue object has been initialized.
public Queue() {
list = new LinkedList();
} // default constructor
// Postcondition: the number of elements in this Queue object has been
// returned.
public int size() {
return list.size();
} // method size
// Postcondition: true has been returned if this Queue object has no
// elements. Otherwise, false has been returned.
public boolean isEmpty() {
return list.isEmpty();
} // method isEmpty
// Postconditon: A of element has been inserted at the back of this
// Queue object. The averageTime (n) is constant and
// worstTime (n) is O (n).
public void enqueue(Object element) {
list.addLast(element);
} // method enqueue
// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: The element that was at the front of this Queue object -
// just before this method was called -- has been removed
// from this Queue object and returned.
public Object dequeue() {
return list.removeFirst();
} // method dequeue
// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: the element at index 0 in this Queue object has been
// returned.
public Object front() {
return list.getFirst();
} // method front
} // Queue class
//Car.java
public class Car {
protected int arrivalTime;
// The Car has been constructed.
public Car() {
}
// The car has been constructed from the arrival time.
public Car(int nextArrivalTime) {
arrivalTime = nextArrivalTime;
} // constructor with int parameter
// Postcondition: The arrival time of the Car has been returned.
public int getArrivalTime() {
return arrivalTime;
} // method getArrivalTime
} // class Car
//CarWash.java
import general.GUI;
import general.Process;
import java.util.*;
public class CarWash implements Process {
protected final String PROMPT = "\nIn the Input line, please enter "
+ "the next arrival time. The sentinel is ";
protected final int ILLEGAL_INPUT = -1;
protected final int SENTINEL = 999;
protected final int INFINITY = 10000; // indicates no car being washed
protected final int MAX_SIZE = 5; // maximum cars allowed in carQueue
protected final int WASH_TIME = 10; // minutes to wash one car
protected GUI gui;
protected Queue carQueue;
protected int currentTime, nextDepartureTime, numberOfCars,
sumOfWaitingTimes;
// The CarWash has been initialized.
public CarWash() {
gui = new GUI(this);
carQueue = new Queue();
currentTime = 0;
numberOfCars = 0;
sumOfWaitingTimes = 0;
nextDepartureTime = INFINITY;
gui.println(PROMPT + SENTINEL);
} // constructor
// Postcondition: The next arrival time, in the string s, has been
// processed.
public void processInput(String s) {
int nextArrivalTime;
gui.println(s);
nextArrivalTime = parseOK(s);
if (nextArrivalTime != ILLEGAL_INPUT)
if (nextArrivalTime != SENTINEL) {
while (nextArrivalTime >= nextDepartureTime)
processDeparture();
processArrival(nextArrivalTime);
gui.println(PROMPT + SENTINEL);
} // processing next arrival
else { // process any cars remaining on the queue:
while (nextDepartureTime < INFINITY)
processDeparture();
printResult();
gui.freeze();
} // processing cars still on queue after last arrival
else
gui.println(PROMPT + SENTINEL);
} // processInput
// Postcondition: if a legal value for next arrival time has been
// obtained from the Input line, that value has been
// returned. Otherwise, ILLEGAL_INPUT has been returned.
protected int parseOK(String s) {
final String INTEGER_NEEDED = "\nThe input line should consist of an integer.";
StringTokenizer tokens = new StringTokenizer(s);
try {
return Integer.parseInt(tokens.nextToken());
} // try
catch (NoSuchElementException e) {
gui.println(e + INTEGER_NEEDED);
} // catch not enough input
catch (NumberFormatException e) {
gui.println(e + INTEGER_NEEDED);
} // input not in integer form
return ILLEGAL_INPUT;
} // method parseOK
// Postcondition: A car has arrived and has either been turned away --
// if the Queue was full before this message was sent --
// or has entered the car wash.
protected void processArrival(int nextArrivalTime) {
final String OVERFLOW = "Overflow";
currentTime = nextArrivalTime;
if (carQueue.size() == MAX_SIZE)
gui.print(OVERFLOW);
else {
numberOfCars++;
if (nextDepartureTime == INFINITY)
nextDepartureTime = currentTime + WASH_TIME;
else
carQueue.enqueue(new Car(nextArrivalTime));
} // not an overflow
} // method processArrival
// Postcondition: A car has finished getting washed.
protected void processDeparture() {
int arrivalTime, waitingTime;
currentTime = nextDepartureTime;
if (!carQueue.isEmpty()) {
Car car = (Car) carQueue.dequeue();
arrivalTime = car.getArrivalTime();
waitingTime = currentTime - arrivalTime;
sumOfWaitingTimes += waitingTime;
nextDepartureTime = currentTime + WASH_TIME;
} // carQueue was not empty
else
nextDepartureTime = INFINITY;
} // method processDeparture
// Postcondition: The average waiting time, or an errro message,
// has been printed.
protected void printResult() {
final String NO_CARS_MESSAGE = "There were no cars in the car wash.";
final String AVERAGE_WAITING_TIME_MESSAGE = "\n\nThe average waiting time, in minutes, was ";
final String CLOSE_WINDOW_PROMPT = "\nThe execution of the project "
+ "is finished. Please close this window when you want to.";
if (numberOfCars == 0)
gui.println(NO_CARS_MESSAGE);
else
gui.println(AVERAGE_WAITING_TIME_MESSAGE
+ ((double) sumOfWaitingTimes / numberOfCars));
gui.print(CLOSE_WINDOW_PROMPT);
} // method printResult
} // class CarWash
//CarWashMain .java
public class CarWashMain {
public static void main(String[] args) {
CarWash carWash = new CarWash();
} // method main
} // class CarWashMain
㈤ JAVA數據結構哪些
主要是3種介面:List Set Map
List:ArrayList,LinkedList:順序表ArrayList,鏈表LinkedList,堆棧和隊列可以使用LinkedList模擬
Set:HashSet沒有重復記錄的集合
Map:HashMap就是哈希表
二叉樹可以利用遞歸的思想來模擬自行設計,從JDK5開始還提供了一個新的隊列介面
圖!!!沒遇到過這樣的情況,恐怕還是要自己模擬
㈥ JAVA版和C++版的數據結構有什麼不同
數據結構本身是一種邏輯上的概念,它是獨立於特定語言或者實現的
比如說鏈表,概念上說就是一組結點構成的數據結構,其中每個結點均帶有後續結點信息。各種語言都可以實現鏈表,但實現的思路都是基於上面的邏輯概念。
因此,學習數據結構不必拘泥於某種特定語言,歸根結底是要把握每個數據結構(邏輯上)的精髓
在這個基礎上,每種語言都可以實現特定的數據結構,差別只在於語法實現級別。
另外雖然Java/C++等語言都帶有大量的標准類庫,但這並不意味著可以忽視數據結構基礎理論的學習。這直接關繫到實際應用時,是只能死板套用現成模板,還是靈活應用各種結構高效實現需求。
㈦ Java數據結構
使用java集合新出的stream()方法 就是用流的方式處理集合
㈧ Java數據結構與演算法有哪些
《Java數據結構和演算法》(第2版)介紹了計算機編程中使用的數據結構和演算法,對於在計算機應用中如何操作和管理數據以取得最優性能提供了深入淺出的講解。全書共分為15章,分別講述了基本概念、數組、簡單排序、堆和隊列、鏈表、遞歸、進階排序、二叉樹、紅黑樹、哈希表及圖形等知識。附錄中則提供了運行專題Applet和常式、相關書籍和問題解答。《Java數據結構和演算法》(第2版)提供了學完一門編程語言後進一步需要知道的知識。本書所涵蓋的內容通常作為大學或學院中計算機系二年級的課程,在學生掌握了編程的基礎後才開始本書的學習。