中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

java數(shù)據(jù)結(jié)構(gòu)代碼大全 java數(shù)據(jù)結(jié)構(gòu)代碼大全

求程序代碼(java版的數(shù)據(jù)結(jié)構(gòu))

3個(gè)class,運(yùn)行UI.java。

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、蒼梧ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的蒼梧網(wǎng)站制作公司

******

public class CircuitException extends Exception {public CircuitException(){}}

*****

import java.util.LinkedList;

public class GPS {

public static final int MAX = 65535;

public GPS(int maxSize){

graph = new Graph(maxSize);

}

public GPS(){

graph = new Graph();

}

public Graph graph;

public static void main(String args[]){

GPS gps = new GPS();

try {

gps.graph.addEdge("a", "b", 1);

gps.graph.addEdge("a", "c", 1);

gps.graph.addEdge("b","d" , 1);

gps.graph.addEdge("c","d" , 1);

gps.graph.addEdge("d","e" , 1);

gps.graph.addEdge("d","f" , 1);

gps.graph.addEdge("e","t" , 2);

gps.graph.addEdge("f","t" , 1);

LinkedList list = gps.graph.getPath("a", "d");

for(int i = 0 ; i list.size() ; i++){

System.out.print(list.get(i));

}System.out.println();

} catch (CircuitException e) {

System.out.println("出現(xiàn)了自環(huán)!");

}

gps.graph.showGraph();

System.out.println(gps.graph.gap);

}

public class Graph{

public int Zuidazhi = 50;

public int changdu = 0;

public Jiao[] vertex;

public double gap;

public Graph(){

vertex = new Jiao[Zuidazhi];

}

public Graph(int maxSize){

this.Zuidazhi = maxSize;

vertex = new Jiao[maxSize];

}

public void addVertex(String name){

vertex[changdu++] = new Jiao(name);

}

public void addEdge(String v1, String v2,double edge) throws CircuitException{

//先找到v1;

if(v1.equals(v2))

throw new CircuitException();

Jiao from = null;

Jiao to = null;

for(int i = 0 ; i changdu ; i++){

if(vertex[i].name.equals(v1)){

from = vertex[i];

}else if(vertex[i].name.equals(v2)){

to = vertex[i];

}

}

if(from == null){

this.addVertex(v1);

from = this.vertex[changdu-1];

}

if(to == null){

this.addVertex(v2);

to = this.vertex[changdu-1];

}//已經(jīng)找到v1和v2;

//沒有檢測(cè)是否v1 v2邊已經(jīng)存在!

//加入邊。

Jiao v1adj = new Jiao(v2);

v1adj.edge = edge;

Jiao v2adj = new Jiao(v1);

v2adj.edge = edge;

//添加聯(lián)系

//檢查聯(lián)系是否已經(jīng)存在

Jiao temp = from;

while(temp.next!=null){

Jiao temppar = temp;

temp = temp.next;

if(temp.name.equals(v1adj.name)){

temppar.next = temp.next;

}

}

v1adj.next = from.next;

from.next = v1adj;

//v2adj.next = to.next;

//to.next = v2adj;

}

//假設(shè)要找的必然存在,不用想是否不在

public LinkedList getPath(String v1 ,String v2){

int count = 0;

//System.out.println(count++);

boolean found[] = new boolean[changdu];

double distance[] = new double[changdu];

int to = 0;

Jiao from = null;

for(int i = 0 ; i changdu ; i++){

found[i] = false;

distance[i] = MAX;

}

for(int i = 0 ; i changdu ; i++){

if(vertex[i].name.equals(v1)){//找到始發(fā)地

from = vertex[i];

distance[i] = 0;

found[i] = true;

//System.out.println(count++);

}

if(vertex[i].name.equals(v2)){//找到目的地

to = i;

//System.out.println(count++);

}

}

//必須先準(zhǔn)備好路徑!

Jiao forCount = from;

int degree = 0;

while(forCount!=null){

degree++;

forCount=forCount.next;

}

LinkedList[] list = new LinkedList[degree];

int [] mark = new int[degree];

for(int i = 0 ; i degree ; i++){

list[i]=new LinkedList();

mark[i]=MAX;

}

int test=0;

int count2 = 0;

int count3 = 0;

//System.out.println(count+++"xx");

while(!found[to]test++100){

//System.out.println(count+++"FIRST");

//開始時(shí)from到所有都是最大值。

//找到標(biāo)記了的節(jié)點(diǎn)

//找標(biāo)記了的節(jié)點(diǎn)鄰接的未標(biāo)記的節(jié)點(diǎn)。

//得到最短的邊,并標(biāo)記。

//更新現(xiàn)有路徑

double min = MAX;

int address = -1;

int father = -1;

for(int i = 0 ; i changdu ; i++){//對(duì)于已經(jīng)找到的頂點(diǎn)尋找最小的往后的距離。

if(found[i]){//找到了的。

Jiao temp = vertex[i];

while(temp!=null){//vertex的鄰接頂點(diǎn)~~

//先看temp的號(hào)碼~

int tempNumber = -1;

for(int j = 0 ; j changdu ; j++){

if(vertex[j].name.equals(temp.name)){

tempNumber = j;

break;

}

}

if(!found[tempNumber]){//如果是還沒有找到的~

double dist = distance[i]+temp.edge;

if(dist min){

min = dist;

father = i;

//System.out.println(" "+min);

address = tempNumber;

}

}

temp = temp.next;

}

}

}found[address] = true;

distance[address] = min;

//添加到已有路徑中去!

//知道father

for(int i = 0 ; i degree ; i++){

if(list[i].isEmpty()||list[i].getLast().equals(vertex[father].name)){

list[i].addLast(vertex[address].name);

break;

}

}

}

for(int i = 0 ; i degree ; i++){

if(list[i].isEmpty())

continue;

else{

if(list[i].getLast().equals(v2)){

gap=0;

//先求出gap

Jiao pre = from;

Jiao nex = null;

for(int j = 0 ; j list[i].size() ; j++){

for(int k = 0 ; k changdu ; k++){

if(vertex[k].name.equals(list[i].get(j))){

nex = vertex[k];break;

}

}

while(pre.next!=null){//找到下一個(gè)的長(zhǎng)度

pre = pre.next;

//System.out.println(nex.name +"nex.name");

if(pre.name.equals(nex.name)){

gap+=pre.edge;

//System.out.println(" gap2 "+gap);

}

}

pre = nex;

}

//System.out.println(gap+"gap");

return list[i];

}

}

}

return null;

}

public void showGraph(){

Jiao temp;

for(int i = 0 ; i changdu ; i++){

temp = vertex[i];

while(temp!=null){

System.out.print(temp.name+temp.edge+" ");

temp = temp.next;

}System.out.println();

}System.out.println("Show Over!");

}

}

public class Jiao{

public String name;

public Jiao next = null;

public double edge;

public Jiao(String name){

this.name = name;

}

}

}

******

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.LinkedList;

import javax.swing.JButton;

import javax.swing.DefaultListModel;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JSeparator;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

public class UI extends JFrame implements ActionListener{

private JTextField textField_5;

private JTextField textField_4;

private JList list_1;

private JList list;

private JTextField textField_1;

private JTextField textField_3;

private JTextField textField_2;

private JTextField textField;

private DefaultListModel model = new DefaultListModel();

private DefaultListModel model_1 = new DefaultListModel();

/**

* Launch the application

* @param args

*/

public static void main(String args[]) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

UI frame = new UI();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame

*/

public UI() {

super();

setTitle("GPS尋路");

getContentPane().setLayout(null);

setBounds(100, 100, 500, 375);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(11, 36, 221, 193);

getContentPane().add(scrollPane);

list = new JList(model);

scrollPane.setViewportView(list);

final JScrollPane scrollPane_1 = new JScrollPane();

scrollPane_1.setBounds(253, 36, 218, 193);

getContentPane().add(scrollPane_1);

list_1 = new JList(model_1);

scrollPane_1.setViewportView(list_1);

final JLabel label = new JLabel();

label.setText("從");

label.setBounds(10, 249, 24, 18);

getContentPane().add(label);

final JLabel label_1 = new JLabel();

label_1.setText("到");

label_1.setBounds(11, 273, 24, 18);

getContentPane().add(label_1);

textField = new JTextField();

textField.setBounds(50, 247, 103, 22);

getContentPane().add(textField);

textField_2 = new JTextField();

textField_2.setBounds(50, 271, 103, 22);

getContentPane().add(textField_2);

final JLabel label_2 = new JLabel();

label_2.setText("距離");

label_2.setBounds(11, 297, 37, 18);

getContentPane().add(label_2);

textField_3 = new JTextField();

textField_3.setBounds(50, 295, 103, 22);

getContentPane().add(textField_3);

final JButton button = new JButton();

button.setText("添加");

button.setBounds(155, 250, 73, 28);

getContentPane().add(button);

final JButton button_1 = new JButton();

button_1.setText("刪除");

button_1.setBounds(155, 285, 73, 28);

getContentPane().add(button_1);

final JLabel label_3 = new JLabel();

label_3.setText("距離:");

label_3.setBounds(253, 297, 39, 18);

getContentPane().add(label_3);

textField_1 = new JTextField();

textField_1.setBounds(293, 295, 86, 22);

getContentPane().add(textField_1);

final JButton button_2 = new JButton();

button_2.setText("顯示路徑");

button_2.setBounds(385, 249, 86, 68);

getContentPane().add(button_2);

final JLabel label_4 = new JLabel();

label_4.setText("路徑表示");

label_4.setBounds(11, 10, 66, 18);

getContentPane().add(label_4);

final JLabel label_5 = new JLabel();

label_5.setText("最佳路徑");

label_5.setBounds(253, 12, 66, 18);

getContentPane().add(label_5);

//

button.addActionListener(this);

button_1.addActionListener(this);

button_2.addActionListener(this);

final JLabel label_6 = new JLabel();

label_6.setText("從");

label_6.setBounds(253, 249, 24, 18);

getContentPane().add(label_6);

textField_4 = new JTextField();

textField_4.setBounds(293, 247, 86, 22);

getContentPane().add(textField_4);

final JLabel label_7 = new JLabel();

label_7.setText("到");

label_7.setBounds(253, 273, 24, 18);

getContentPane().add(label_7);

textField_5 = new JTextField();

textField_5.setBounds(293, 271, 86, 22);

getContentPane().add(textField_5);

final JSeparator separator = new JSeparator();

separator.setOrientation(SwingConstants.VERTICAL);

separator.setBounds(239, 10, 8, 317);

getContentPane().add(separator);

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getActionCommand().equals("添加")){

try{String from = textField.getText();

String to = textField_2.getText();

if(from.equals(to)){

JOptionPane.showMessageDialog(null, "始點(diǎn)與終點(diǎn)不能相同");

return;

}

if(from.equals("")||to.equals("")){

JOptionPane.showMessageDialog(null, "添加不能為空");

return;

}for(int i = 0 ; i model.size() ; i ++){

if(model.get(i).toString().substring(0, model.get(i).toString().indexOf(":")).equals(

from+"-"+to))

model.remove(i);

}

double length = Double.parseDouble(textField_3.getText());

model.addElement(from+"-"+to+": "+length);

}catch(Exception e1){

JOptionPane.showMessageDialog(null, "距離為數(shù)字值");

}

}

if(e.getActionCommand().equals("刪除")){

for(int i = 0 ; i model.size() ; i++){

if(list.isSelectedIndex(i))

model.remove(i);

}

}

if(e.getActionCommand().equals("顯示路徑")){

try{

model_1.removeAllElements();

GPS gps = new GPS();

String full,from,to;

double length;

for(int i = 0 ; i model.size() ; i++){

full = model.get(i).toString();

from = full.substring(0,full.indexOf("-"));

to = full.substring(full.indexOf("-")+2,full.lastIndexOf(":"));

length = Double.parseDouble(full.substring(full.indexOf(":")+1, full.length()-1));

//System.out.println(from);

//System.out.println(to);

try {

gps.graph.addEdge(from, to, length);

System.out.println(from +" "+ to);

} catch (CircuitException e1) {

System.out.println("有環(huán)存在");

}

}LinkedList list = gps.graph.getPath(textField_4.getText(), textField_5.getText());

model_1.addElement(textField_4.getText());

for(int i = 0 ; i list.size() ; i++){

model_1.addElement(list.get(i));

}//計(jì)算路徑長(zhǎng)度

textField_1.setText(""+gps.graph.gap);

}catch(Exception e1){

JOptionPane.showMessageDialog(null, "沒有找到有關(guān)節(jié)點(diǎn)");

}

}

}

}

關(guān)于數(shù)據(jù)結(jié)構(gòu)(java)的一個(gè)代碼

描述棧抽象數(shù)據(jù)類型的SStack接口的聲明

public interfaceSStackE //棧接口

{

boolean isEmpty(); //判斷是否空棧,若空棧返回true

boolean push(E element); //元素element入棧,若操作成功返回true

E pop(); //出棧,返回當(dāng)前棧頂元素,若??辗祷豱ull

E get(); //取棧頂元素值,未出棧,若棧空返回null

}

順序棧類具體操作方法的聲明:

importdataStructure.linearList.SStack;

public classSeqStackE implements SStackE

//順序棧類

{

private Object value[]; //存儲(chǔ)棧的數(shù)據(jù)元素

private int top; //top為棧頂元素下標(biāo)

public SeqStack(int capacity) //構(gòu)造指定容量的空棧

{

this.value = newObject[Math.abs(capacity)];

this.top=-1;

}

public SeqStack() //構(gòu)造默認(rèn)容量的空棧

{

this(10);

}

public boolean isEmpty() //判斷是否空棧,若空棧返回true

{

return this.top==-1;

}

public boolean push(E element) //元素element入棧,若操作成功返回true

{

if (element==null)

return false; //空對(duì)象(null)不能入棧

if (this.top==value.length-1) //若棧滿,則擴(kuò)充容量

{

Object[] temp = this.value;

this.value = newObject[temp.length*2];

for (int i=0; itemp.length;i++)

this.value[i] = temp[i];

}

this.top++;

this.value[this.top] = element;

return true;

}

public E pop() //出棧,返回當(dāng)前棧頂元素,若棧空返回null

{

if (!isEmpty())

return (E)this.value[this.top--];

else

return null;

}

public E get() //取棧頂元素值,未出棧,棧頂元素未改變

{

if (!isEmpty())

return (E)this.value[this.top];

else

return null;

}

public String toString() //返回棧中各元素的字符串描述

{

String str="{";

if (this.top!=-1)

str +=this.value[this.top].toString();

for (int i=this.top-1; i=0; i--)

str += ","+this.value[i].toString();

return str+"} ";

}

實(shí)例引用public static void main(String args[])

{

SeqStackString stack = newSeqStackString(20);

System.out.print("Push: ");

char ch='a';

for(int i=0;i5;i++)

{

String str =(char)(ch+i)+"";

stack.push(str);

System.out.print(str+" ");

}

System.out.println("\n"+stack.toString());

System.out.print("Pop : ");

while(!stack.isEmpty()) //全部出棧

System.out.print(stack.pop().toString()+" ");

System.out.println();

}

求解釋這段java代碼,關(guān)于數(shù)據(jù)結(jié)構(gòu)的

(1)sumAll()

意思是遞歸的求二叉樹每個(gè)節(jié)點(diǎn)的值的和,比如只有一級(jí)的

1

2 3

這樣對(duì)象1的左右對(duì)象就是2,3,通過sumAll求和,就是

int ans = this.i; // ans 實(shí)際等于1

if(left != null)

ans = ans + left.sumAll(); // ans = 1 + 2 = 3

if(right != null)

ans = ans + right.sumAll(); //ans = 3+ 3 = 6

如果有好幾級(jí)的二叉樹,也是依次類推,切記一定先從最底層開始推算,比如

1

2 3

2 1 3 5

1 2

(2)程序?qū)嶋H上是通過一個(gè)遞歸學(xué)習(xí)二叉樹的遍歷

(3) static int maxArray(int [] array)這個(gè)很簡(jiǎn)單就不說了哈~

(4) left前加下劃線不是書寫習(xí)慣,只是為了突出對(duì)象的左右節(jié)點(diǎn),命名上并沒有建議非要每個(gè)變量都這玩~ 當(dāng)然,如果有特殊情節(jié),也可以這么玩,隨自己~

用java實(shí)現(xiàn)一個(gè)數(shù)據(jù)結(jié)構(gòu)!

import java.io.IOException;

import java.util.Scanner;

public class LinkList {

private static Scanner san = new Scanner(System.in);

public static void main(String[] args) throws IOException {

List list = new List();

for (int i = 1; i = 10; i++) {

System.out.print("請(qǐng)輸入第" + i + "個(gè)數(shù): ");

list.add(san.nextInt());

list.print();

}

System.out.println("輸入的數(shù)據(jù)如下: ");

list.print();

}

}

class node {

int data;

node next = this; // 指向自己

}

class List {

private node header = new node();

// 循環(huán)鏈表的尾部添加數(shù)據(jù)

public node add(int data) {

node current = new node();

node temp = header;

while (temp.next != header)

temp = temp.next;

current.data = data;

current.next = temp.next;

temp.next = current;

return current;

}

// 查詢某個(gè)數(shù)字的位置 如果不在 返回-1;

public int search(int data) {

node temp = header;

int n = 0;

while (temp.next != header) {

temp = temp.next;

n++;

if (temp.data == data)

break;

}

if (temp.data == data)

return n;

else

return -1;

}

// 打印出整個(gè)鏈表

public void print() {

node temp = header;

while (temp.next != header) {

temp = temp.next;

System.out.print(temp.data + " ");

}

System.out.println();

}

// 插入數(shù)據(jù)

public node Insert(int pos, int data) {

node temp = header;

node current = new node();

for (int i = 0; i pos - 1; i++) {

if (temp.next != header) {

temp = temp.next;

} else

return null;

}

current.data = data;

if (temp.next != header) {

current.next = temp.next;

}

temp.next = current;

return current;

}

// 刪除某個(gè)數(shù)據(jù)

public node del(int data) {

node temp = header;

node oldtemp = null;

node current = null;

while (temp.next != header) {

oldtemp = temp;

temp = temp.next;

if (temp.data == data) {

current = temp;

break;

}

}

if (current == header)

return null;

oldtemp.next = current.next;

return current;

}

}

網(wǎng)站欄目:java數(shù)據(jù)結(jié)構(gòu)代碼大全 java數(shù)據(jù)結(jié)構(gòu)代碼大全
轉(zhuǎn)載來源:http://www.rwnh.cn/article36/ddosipg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計(jì)虛擬主機(jī)、微信公眾號(hào)商城網(wǎng)站、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)
渝中区| 九寨沟县| 石林| 东明县| 通城县| 元朗区| 龙游县| 保德县| 吉水县| 神木县| 读书| 巍山| 克什克腾旗| 科技| 陆良县| 遂溪县| 平南县| 维西| 龙山县| 洛扎县| 乌拉特后旗| 泰和县| 台中市| 祁门县| 汨罗市| 南部县| 武义县| 临江市| 措美县| 漳州市| 武威市| 嘉荫县| 桂林市| 准格尔旗| 雷山县| 阿瓦提县| 武夷山市| 罗江县| 西华县| 广南县| 和田市|