java数据结构和算法源代码(数据结构与算法分析java版)
admin 发布:2022-12-19 12:35 138
本篇文章给大家谈谈java数据结构和算法源代码,以及数据结构与算法分析java版对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、求程序代码(java版的数据结构)
- 2、Java 与 算法+数据结构 (100分)
- 3、java(算法与数据结构)tree
- 4、java(树的内容)算法与数据结构
- 5、算法 数据结构编程(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 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];
}//已经找到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[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)){//找到始发地
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 changdu ; i++){//对于已经找到的顶点寻找最小的往后的距离。
if(found[i]){//找到了的。
Jiao temp = vertex[i];
while(temp!=null){//vertex的邻接顶点~~
//先看temp的号码~
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){//找到下一个的长度
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, "始点与终点不能相同");
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 与 算法+数据结构 (100分)
说数据结构没用那是不可能的,但是要看你做什么了。
比如说你要血java,如果你想搞网站方面的话就简单了。
数据结构基本可以不用学,因为在web应用中,能用到的算法的地方少之又少,几乎就那么几个,想记不住都难。
但是如果你要往软件方面和手软方面发展的话就要学一部分了,但是这东西学是学不到的,能学到的只不过是思路,到时候自己发挥一下,想个算法就行了,算法这东西说难不难,难的东西有,但是没有你能用到的。
像你这样的情况我想说两点:
首先,说你想从事算法类的工作,那么选择什么样的语言都是一样的,算法肯定有,但是用到的都不多。刚进公司的时候一般是用不到算法的,因为算法都是别人想的,你也许有好的算法,但是别人不一定采用,但是你的算法基础不要丢掉,因为等你当了项目经理后这个是必不可少的。
其次,你要知道,在学计算机的路上,很少有人能学什么就做什么,大家都在被社会潮流推动,想要不掉队就只能随波逐流。因为毕竟我们都不想一辈子写代码。大家都是拿这东西做个跳板。
学java的路很长,但是也很有趣,希望你能学好。我想以你的算法基础,以后想成为专业精英不是问题。加油吧。
java(算法与数据结构)tree
代码实现[一]部分
package ChapterEight;
class Tree {
class Node {
public long value;
public Node leftChild;
public Node rightChild;
public Node(long value) {
this.value = value;
leftChild = null;
rightChild = null;
}
}
public Node root;
public Tree() {
root = null;
}
// 向树中插入一个节点
public void insert(long value) {
Node newNode = new Node(value);
// 树是空的
if (root == null)
root = newNode;
else {
Node current = root;
Node parentNode;
while (true) {
parentNode = current;
if (value current.value) {
current = current.leftChild;
// 要插入的节点为左孩子节点
if (current == null) {
parentNode.leftChild = newNode;
return;
}
} else {
// 要插入的节点为右孩子节点
current = current.rightChild;
if (current == null) {
parentNode.rightChild = newNode;
return;
}
}
}
}
}
// 先续遍历树中的所有节点
public void preOrder(Node currentRoot) {
if (currentRoot != null) {
System.out.print(currentRoot.value + " ");
preOrder(currentRoot.leftChild);
preOrder(currentRoot.rightChild);
}
}
// 中续遍历树中的所有节点
public void inOrder(Node currentNode) {
if (currentNode != null) {
inOrder(currentNode.leftChild);
System.out.print(currentNode.value + " ");
inOrder(currentNode.rightChild);
}
}
// 后续遍历树中的所有节点
public void postOrder(Node currentNode) {
if (currentNode != null) {
postOrder(currentNode.leftChild);
postOrder(currentNode.rightChild);
System.out.print(currentNode.value + " ");
}
}
public void traverse(int traverseType) {
switch (traverseType) {
case 1:
preOrder(root);
break;
case 2:
inOrder(root);
break;
case 3:
postOrder(root);
break;
default:
break;
}
// 依据树节点的值删除树中的一个节点
public boolean delete(int value) {
// 遍历树过程中的当前节点
Node current = root;
// 要删除节点的父节点
Node parent = root;
// 记录树的节点为左孩子节点或右孩子节点
boolean isLeftChild = true;
while (current.value != value) {
parent = current;
// 要删除的节点在当前节点的左子树里
if (value current.value) {
isLeftChild = true;
current = current.leftChild;
}
// 要删除的节点在当前节点的右子树里
else {
isLeftChild = false;
current = current.rightChild;
}
// 在树中没有找到要删除的节点
if (current == null)
return false;
}
// 要删除的节点为叶子节点
if (current.leftChild == null current.rightChild == null) {
// 要删除的节点为根节点
if (current == root)
root = null;
// 要删除的节点为左孩子节点
else if (isLeftChild)
parent.leftChild = null;
// 要删除的节点为右孩子节点
else
parent.rightChild = null;
}
// 要删除的节点有左孩子节点,没有右孩子节点
else if (current.rightChild == null) {
// 要删除的节点为根节点
if (current == null)
root = current.leftChild;
// 要删除的节点为左孩子节点
else if (isLeftChild)
parent.leftChild = current.leftChild;
// 要删除的节点为右孩子节点
else
parent.rightChild = current.leftChild;
}
// 要删除的节点没有左孩子节点,有右孩子节点
else if (current.leftChild == null) {
// 要删除的节点为根节点
if (current == root)
root = root.rightChild;
// 要删除的节点为左孩子节点
else if (isLeftChild)
parent.leftChild = current.rightChild;
// 要删除的节点为右孩子节点
else
parent.rightChild = current.rightChild;
}
// 要删除的接节点既有左孩子节点又有右孩子节点
else {
Node successor = getSuccessor(current);
// 要删除的节点为根节点
if (current == root)
root = successor;
// 要删除的节点为左孩子节点
else if (isLeftChild)
parent.leftChild = successor;
// 要删除的节点为右孩子节点
else
parent.rightChild = successor;
}
return true;
}
// 找到要删除节点的替补节点
private Node getSuccessor(Node delNode) {
// 替补节点的父节点
Node successorParent = delNode;
// 删除节点的替补节点
Node successor = delNode;
Node current = delNode.rightChild;
while (current != null) {
// successorParent指向当前节点的上一个节点
successorParent = successor;
// successor变为当前节点
successor = current;
current = current.leftChild;
}
// 替补节点的右孩子节点不为空
if (successor != delNode.rightChild) {
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild;
}
return successor;
}
}
public class TreeApp {
public static void main(String[] args) {
Tree tree = new Tree();
tree.insert(8);
tree.insert(50);
tree.insert(45);
tree.insert(21);
tree.insert(32);
tree.insert(18);
tree.insert(37);
tree.insert(64);
tree.insert(88);
tree.insert(5);
tree.insert(4);
tree.insert(7);
System.out.print("PreOrder : ");
tree.traverse(1);
System.out.println();
System.out.print("InOrder : ");
tree.traverse(2);
System.out.println();
System.out.print("PostOrder : ");
tree.traverse(3);
System.out.println();
System.out.println(tree.delete(7));
System.out.print("PreOrder : ");
tree.traverse(1);
System.out.println();
System.out.print("InOrder : ");
tree.traverse(2);
System.out.println();
System.out.print("PostOrder : ");
tree.traverse(3);
System.out.println();
}
}
java(树的内容)算法与数据结构
其实有两种方式:
第一种就是递归 就像现在比较老的树形菜单。这种方式应该string类型应该是存不了的。就是自定义一个类型A 里面有一个成员变量 listA。 这种结构就是list里面嵌套list,你有多少级就有多少层。
第二种其实要做处理,就是把原数据按一定规则排序放到一个list里面,这里面不会再嵌套list。list排完序就如你的效果图一样。第一个 一级节点 》》其子节点;然后第二个一级节点》》其子节点,etc。 但是这种结构要有存的时候要循环一遍排成上述的顺序,取的时候还需要判断哪个是下一个不同级节点的开始。
js前台展示比较简单,根据父id直接添加就行了,原数据什么都不用做。但是java里这种方式不行。
算法 数据结构编程(java语言)
private void sort(int[] list)
{
int[] sortlist=new int[21];
for(int i=1;i=20;i++)
sortlist[i]=-1;
for(int i=0;ilist.Length;i++)
{
sortlist[list[i]]=list[i];
}
for(int i=0;isortlist.Length;i++)
{
if(sortlist[i]!=-1)
{
输出sortlist[i];
}
}
因为已知最大值,所以遍历算法计算次数为常数,所以算法复杂度为1
关于java数据结构和算法源代码和数据结构与算法分析java版的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-18软文发布平台,软文发布平台分析
- 05-18网络营销案例具体分析,网络营销案例具体分析模板
- 05-15seo分析报告,做seo的分析工具有哪些
- 05-07aso优化分析,aso优化方法
- 05-07pb超级报表源代码(pb报表工具)[20240507更新]
- 05-07简历源代码可以上传照片的简单介绍[20240507更新]
- 05-07广告切换源代码免费下载(广告切换源代码免费下载安装)[20240507更新]
- 05-06阁楼网源代码(阁楼是什么网站)[20240506更新]
- 05-06源代码管理资源管理器(资源管理器运行代码)[20240506更新]
- 05-06人脸识别源代码pdf的简单介绍[20240506更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接