㈠ 數據結構(java版)
packagegame24.datastructure.list;
/**
*鏈表的結點
*@authorluoweifu
*
*/
classNode{
Objectdata; //數據元素
Nodenext; //後驅結點
publicNode(){
this(null);
}
publicNode(Objectdata){
this.data=data;
this.next=null;
}
}
/**
*帶頭結點的鏈式鏈表,下標從0開始;
*@authorAdministrator
*
*/
publicclassSinglyLinkedList<E>{
Nodehead; //頭結點
intsize; //鏈表的大小
publicSinglyLinkedList(){
head=newNode();
size=0;
}
publicSinglyLinkedList(E[]datas){
intn=datas.length;
head=newNode();
Nodep=head;
for(inti=0;i<n;i++){
p.next=newNode(datas[i]);
p=p.next;
}
size=n;
}
publicSinglyLinkedList(SinglyLinkedListlist){
head=list.head;
size=list.size();
}
publicvoidadd(Objecte){
Nodep;
if(0==size){
p=head;
}else{
p=index(size-1);
}
p.next=newNode(e);
size++;
}
publicvoidconcat(SinglyLinkedListlist){
NodelastNode=this.index(size-1);
lastNode.next=list.index(0);
size+=list.size();
}
publicvoidclear(){
head.next=null;
size=0;
}
publicObjectget(inti){
Nodep=index(i);
returnp.data;
}
privateNodeindex(inti){
Nodep=null;
if(i>=0&&i<size){
p=head;
for(intj=0;j<=i;j++){
p=p.next;
}
}
returnp;
}
publicintindexOf(Objecte){
Nodep=head.next;
inti=0;
while(!p.data.equals(e)){
p=p.next;
i++;
}
if(i<size)
returni;
else
return-1;
}
publicvoidinsert(inti,Objecte){
Nodep=index(i);
Nodep2=newNode(e);
p2.next=p.next;
p.next=p2;
size++;
}
publicbooleanisEmpty(){
if(size==0)
returntrue;
else
returnfalse;
}
publicintlastIndexOf(Objecte){
inti=size-1;
while(!get(i).equals(e)){
i--;
}
if(i>=0)
returni;
else
return-1;
}
publicvoidremove(inti){
if(i>=0&&i<size){
Nodep=null;
if(i==0)
p=head;
else{
p=index(i-1);
}
p.next=index(i).next;
}
size--;
}
publicvoidset(inti,Objecte){
Nodep=index(i);
p.data=e;
}
publicintsize(){
returnsize;
}
@Override
publicbooleanequals(Objectobj){
SinglyLinkedListlist=(SinglyLinkedList)obj;
if(this==obj&&size==list.size){
returntrue;
}
returnfalse;
}
/**
*測試線性表
*@paramargs
*/
publicstaticvoidmain(Stringargs[]){
//Listlist=newLinkList();
//Listlist=newDoubleLinkList();
SinglyLinkedListlist1=newSinglyLinkedList();
for(inti=0;i<10;i++){
list1.add(newInteger(i));
}
Integer[]a={101,102,103,104,105,106,107,108,109,110};
SinglyLinkedListlist=newSinglyLinkedList(a);
list.remove(9);
System.out.print("size:"+list.size()+" ");
System.out.println("isEmpty:"+list.isEmpty());
System.out.print("第7個位置的元素:"+list.get(7)+" ");
list.concat(list1);
for(inti=0;i<list.size();i++){
System.out.print(list.get(i)+"");
}
list.add(21);
list.add(22);
list.insert(3,newInteger(5));
System.out.print("size:"+list.size()+" ");
System.out.print("第一次出現5的索引:"+list.indexOf(5)+" ");
System.out.print("最後一次出現5的索引:"+list.lastIndexOf(5)+" ");
list.set(0,newInteger(30));
for(inti=0;i<list.size();i++){
System.out.print(list.get(i)+"");
}
SinglyLinkedListlist2=list;
System.out.println(" isequels?"+list2.equals(list));
}
}
㈡ 用java實現數據結構
簡單而又麻煩的工作 呵呵
㈢ java數據結構
package com.designpattern.facade;
public class Main2 {
public static void main(String[] args) {
Facade facade = new Facade();
/**
* 一步操作就可以完成所有的准備工作
*/
facade.open();
}
㈣ 什麼地方有《數據結構-java版》的源代碼及課後習題答案下載
http://www.wnetw.com/jclub/resource/download.jsp?itemid=229
你試試,不過需要先注冊
㈤ Java演算法與數據結構代碼
第1題:我給你搭建演算法框架,具體需求,你只需往裡面寫Code即可:
publicclassProgram{
privatestaticfinalintN=6;
publicstaticvoidmain(String[]args){
Nodehead=newNode(-1,null);//定義頭指針,帶頭結點的單鏈表
for(inti=0;i<N;i++){
Nodee=newNode(i+1,null);
tailInsert(head,e);
}
//Test
Nodep=head;
while(p.getNext()!=null){
p=p.getNext();
}
}
/**
*@paramhead實施尾插法演算法的單鏈表頭指針
*@parame所需的元素
*/
privatestaticvoidtailInsert(Nodehead,Nodee){
Nodep=head;
while(p.getNext()!=null){
p=p.getNext();//尋訪單鏈表,直至到達單鏈表末尾
}
//實施尾插法
p.setNext(e);
}
}
classNode{
privateintid;//編號
privateNodenext;//單鏈表後繼指針
privateStringvote;//選票
publicNode(){}
publicNode(intid,Nodenext){
super();
this.id=id;
this.next=next;
}
publicNode(intid,Nodenext,Stringvote){
super();
this.id=id;
this.next=next;
this.vote=vote;
}
@Override
publicStringtoString(){
return"Node[id="+id+",next="+next+"]";
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicNodegetNext(){
returnnext;
}
publicvoidsetNext(Nodenext){
this.next=next;
}
}
第2題:參看我以前的回答:https://..com/question/431512924412893084
演算法思想已經寫的清楚得不能在清楚了。轉成Java就是小菜一碟。
㈥ 求程序代碼(java版的數據結構)
3個class,運行UI.java。
******
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("出現了自環!");
}
gps.graph.showGraph();
System.out.println(gps.graph.gap);
}
public class Graph{
public int Zuida = 50;
public int chang = 0;
public Jiao[] vertex;
public double gap;
public Graph(){
vertex = new Jiao[Zuida];
}
public Graph(int maxSize){
this.Zuida = maxSize;
vertex = new Jiao[maxSize];
}
public void addVertex(String name){
vertex[chang++] = 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 < chang ; 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[chang-1];
}
if(to == null){
this.addVertex(v2);
to = this.vertex[chang-1];
}//已經找到v1和v2;
//沒有檢測是否v1 v2邊已經存在!
//加入邊。
Jiao v1adj = new Jiao(v2);
v1adj.edge = edge;
Jiao v2adj = new Jiao(v1);
v2adj.edge = edge;
//添加聯系
//檢查聯系是否已經存在
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;
}
//假設要找的必然存在,不用想是否不在
public LinkedList getPath(String v1 ,String v2){
int count = 0;
//System.out.println(count++);
boolean found[] = new boolean[chang];
double distance[] = new double[chang];
int to = 0;
Jiao from = null;
for(int i = 0 ; i < chang ; i++){
found[i] = false;
distance[i] = MAX;
}
for(int i = 0 ; i < chang ; i++){
if(vertex[i].name.equals(v1)){//找到始發地
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++);
}
}
//必須先准備好路徑!
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");
//開始時from到所有都是最大值。
//找到標記了的節點
//找標記了的節點鄰接的未標記的節點。
//得到最短的邊,並標記。
//更新現有路徑
double min = MAX;
int address = -1;
int father = -1;
for(int i = 0 ; i < chang ; i++){//對於已經找到的頂點尋找最小的往後的距離。
if(found[i]){//找到了的。
Jiao temp = vertex[i];
while(temp!=null){//vertex的鄰接頂點~~
//先看temp的號碼~
int tempNumber = -1;
for(int j = 0 ; j < chang ; 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 < chang ; k++){
if(vertex[k].name.equals(list[i].get(j))){
nex = vertex[k];break;
}
}
while(pre.next!=null){//找到下一個的長度
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 < chang ; 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, "始點與終點不能相同");
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, "距離為數字值");
}
}
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("有環存在");
}
}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));
}//計算路徑長度
textField_1.setText(""+gps.graph.gap);
}catch(Exception e1){
JOptionPane.showMessageDialog(null, "沒有找到有關節點");
}
}
}
}
㈦ 關於數據結構(java)的一個代碼
描述棧抽象數據類型的SStack介面的聲明
public interfaceSStack<E> //棧介面
{
boolean isEmpty(); //判斷是否空棧,若空棧返回true
boolean push(E element); //元素element入棧,若操作成功返回true
E pop(); //出棧,返回當前棧頂元素,若棧空返回null
E get(); //取棧頂元素值,未出棧,若棧空返回null
}
順序棧類具體操作方法的聲明:
importdataStructure.linearList.SStack;
public classSeqStack<E> implements SStack<E>
//順序棧類
{
private Object value[]; //存儲棧的數據元素
private int top; //top為棧頂元素下標
public SeqStack(int capacity) //構造指定容量的空棧
{
this.value = newObject[Math.abs(capacity)];
this.top=-1;
}
public SeqStack() //構造默認容量的空棧
{
this(10);
}
public boolean isEmpty() //判斷是否空棧,若空棧返回true
{
return this.top==-1;
}
public boolean push(E element) //元素element入棧,若操作成功返回true
{
if (element==null)
return false; //空對象(null)不能入棧
if (this.top==value.length-1) //若棧滿,則擴充容量
{
Object[] temp = this.value;
this.value = newObject[temp.length*2];
for (int i=0; i<temp.length;i++)
this.value[i] = temp[i];
}
this.top++;
this.value[this.top] = element;
return true;
}
public E pop() //出棧,返回當前棧頂元素,若棧空返回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+"} ";
}
實例引用public static void main(String args[])
{
SeqStack<String> stack = newSeqStack<String>(20);
System.out.print("Push: ");
char ch='a';
for(int i=0;i<5;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 數據結構
大哥,你這是資料庫的問題.
你要是想用數據結構來解決的話也行,但是數據要是想長久保存的話,還是存入資料庫的好。
既然學java就要有面向對象的思想。
將學生看做一個對象,建立class student.將學號,姓名,成績做為屬性。學號與姓名用String存,成績可以用Hashtable來存,至於排序就 得把所以student對象的成績取出來進行排序
㈨ 我需要一本數據結構的演算法JAVA代碼實現的書。
java數據結構和演算法中文版(pdf)附源碼
http://58.251.57.206/down1?cid=&t=2&fmt=&usrinput=數據結構和演算法java&dt=2002000&ps=0_0&rt=0kbs&plt=0
迅雷試了下能下:http://www.winu.cn/attachment.php?aid=92883
㈩ 幫我詳細解釋一下這幾段java數據結構的代碼好么,越詳細越好
PX類沒有貼出來
這個類要給你解釋的意思是
三種演算法 每種演算法的 時間與空間復雜度
也就是比較哪個演算法更快 更好