对于栈,我给你总结下:
创新互联公司长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为叶城企业提供专业的网站建设、做网站,叶城网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
栈(stack):限定仅在表尾进行插入或删除操作的线。
表尾-栈顶(top),表头-栈底(bootom)
特点:先进后出(filo),或后进先出(lifo)。
至于你的问题实现代码如下(复制可以直接使用):
public static void main(String[] args) {
StackNumber a = new StackNumber();
a.add(1);
a.add(2);
a.add(3);
a.add(4);
StackNumber b = new StackNumber();
b.add(5);
b.add(6);
b.add(7);
b.add(8);
//上面是给栈a、b添加元素
//下面两个for循环可以分别遍历出栈a、b
for (int i = 0; i a.size(); i++) {
System.out.println(a.get(i));
}
System.out.println("--------------------");
for (int i = 0; i b.size(); i++) {
System.out.println(b.get(i));
}
System.out.println("--------------------");
//将 A 中的 4转移到 B中去 然后把 3 POP出来
//第一步:先将A中的4抛出来(现在4是在栈顶,可以直接抛出,如果不在栈顶,则要另写方法)
/*这句可以取到元素“4”在栈中的位置,然后移除
* Number log= a.search(4);
System.out.println("CCC"+log);
a.remove(a.search(3));*/
a.pop();
//将元素4加载到B栈
b.push(4);
//第二步:把 3 POP出来
a.pop();
for (int i = 0; i a.size(); i++) {
System.out.println(a.get(i));
}
System.out.println("--------------------");
for (int i = 0; i b.size(); i++) {
System.out.println(b.get(i));
}
System.out.println("--------------------");
}
---------Queue接口来描述队列,存取数据(先进先出)原则---------
public class DemoQueue {
public static void main(String[] args) {
QueueString queue = new LinkedListString();
queue.offer("a"); // offer()
queue.offer("b");
queue.offer("c");
queue.offer("d");
queue.offer("e");
System.out.println(queue);
System.out.println(queue.poll()); // poll()取出元素并删除
System.out.println(queue);
System.out.println(queue.peek()); // peek()取出元素但不删除
System.out.println(queue);
}
}
// [a, b, c, d, e]
// a
// [b, c, d, e]
// b
// [b, c, d, e]
---------Deque接口描述栈,存取数据(先进后出)原则---------
public class DemoDeque {
public static void main(String[] args) {
DequeString deque = new LinkedListString();
deque.push("a"); // push()
deque.push("b");
deque.push("c");
deque.push("d");
deque.push("e");
System.out.println(deque);
System.out.println(deque.pop());// 取出并删除栈顶元素 pop()
System.out.println(deque);
System.out.println(deque.peek());// 取出栈顶元素但不删除 peek()
System.out.println(deque);
}
}
//[e, d, c, b, a]
//e
//[d, c, b, a]
//d
//[d, c, b, a]
---------递归---------
public class Hee {
public static void main(String[] args) {
int n=6;
int y=f(n); //调用f()方法
System.out.println(y); //21
}
public static int f(int n) {
if(n==1){ //递归的结束条件
return 1;
}
return n+f(n-1);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DSExample {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
@Override public void run(){
final JFrame frame = new JFrame("Java数据结构");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Box hbox = Box.createHorizontalBox();
final JButton stackButton = new JButton("堆栈");
final JButton queueButton = new JButton("队列");
final JButton stringButton = new JButton("串");
final JButton recurButton = new JButton("递归");
final JButton treeButton = new JButton("树");
final JButton graphButton = new JButton("图");
final JButton opButton = new JButton("数据操作");
final JButton expButton = new JButton("表达式");
hbox.add(stackButton);
hbox.add(queueButton);
hbox.add(stringButton);
hbox.add(recurButton);
hbox.add(treeButton);
hbox.add(graphButton);
hbox.add(opButton);
hbox.add(expButton);
final Box vbox = Box.createVerticalBox();
vbox.add(new JLabel("代码区域"));
final JTextArea codeArea = new JTextArea();
vbox.add(new JScrollPane(codeArea));
final JPanel opPanel = new JPanel(new GridLayout(4,2));
final JButton pushButton = new JButton("数据入队");
final JButton popButton = new JButton("数据出队");
final JButton headButton = new JButton("队头元素");
final JButton tailButton = new JButton("队尾元素");
final JTextField pushData = new JTextField();
final JTextField popData = new JTextField();
final JTextField headData = new JTextField();
final JTextField tailData = new JTextField();
opPanel.add(pushData);
opPanel.add(pushButton);
opPanel.add(popData);
opPanel.add(popButton);
opPanel.add(headData);
opPanel.add(headButton);
opPanel.add(tailData);
opPanel.add(tailButton);
opPanel.setBorder(BorderFactory.createTitledBorder("操作"));
frame.add(hbox, BorderLayout.PAGE_START);
frame.add(opPanel, BorderLayout.LINE_END);
frame.add(vbox, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}
}
首先你是不是安装了jdk呢?这是java运行需要的环境,你可以到网上找的下,然后你对环境配置,具体我给你个实例你自己看看
第一步:下载jdk
第二步:安装和配置你的jdk:执行jdk的安装程序,然后设置按照路径进行安装即可。
安装jdk以后,需要配置一下环境变量,在我的电脑-属性-高级-环境变量-系统变量中添加以下环境变量(假定你的jdk安装在c:\jdk1.6):
JAVA_HOME=c:\jdk1.6
classpath=. ;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;(.;一定不能少,因为它代表当前路径)
path=%JAVA_HOME%\bin
接着可以写一个简单的java程序来测试JDK是否已安装成功:
public class Test{
public static void main(String args[]){
System.out.println("HelloWorld.");
}
}
将上面的这段程序保存为文件名为Test.java的文件。
然后打开命令提示符窗口,输入命令cd C:\Documents and Settings\Administrator\桌面(就是cd 加上test文件的存放位置)到你的Test.java所在目录,然后键入下面的命令
javac Test.java
编译成功后再输入:
java Test
此时如果看到打印出来This is a test program.的话说明安装成功了,如果没有打印出这句话,你需要仔细检查一下你的配置情况。
package game24.datastructure.list;
/**
* 链表的结点
* @author luoweifu
*
*/
class Node{
Object data; //数据元素
Node next; //后驱结点
public Node() {
this(null);
}
public Node(Object data) {
this.data = data;
this.next = null;
}
}
/**
* 带头结点的链式链表,下标从0开始;
* @author Administrator
*
*/
public class SinglyLinkedListE{
Node head; //头结点
int size; //链表的大小
public SinglyLinkedList() {
head = new Node();
size = 0;
}
public SinglyLinkedList(E[] datas) {
int n = datas.length;
head = new Node();
Node p = head;
for(int i=0; in; i++) {
p.next = new Node(datas[i]);
p = p.next;
}
size = n;
}
public SinglyLinkedList(SinglyLinkedList list) {
head = list.head;
size = list.size();
}
public void add(Object e) {
Node p;
if(0 == size) {
p = head;
} else {
p = index(size-1);
}
p.next = new Node(e);
size ++;
}
public void concat(SinglyLinkedList list) {
Node lastNode = this.index(size - 1);
lastNode.next = list.index(0);
size += list.size();
}
public void clear() {
head.next = null;
size = 0;
}
public Object get(int i) {
Node p = index(i);
return p.data;
}
private Node index(int i) {
Node p = null;
if(i=0 isize){
p = head;
for(int j=0; j=i; j++) {
p = p.next;
}
}
return p;
}
public int indexOf(Object e) {
Node p = head.next;
int i = 0;
while(!p.data.equals(e)) {
p = p.next;
i++;
}
if(isize)
return i;
else
return -1;
}
public void insert(int i, Object e) {
Node p = index(i);
Node p2 = new Node(e);
p2.next = p.next;
p.next = p2;
size ++;
}
public boolean isEmpty() {
if(size ==0)
return true;
else
return false;
}
public int lastIndexOf(Object e) {
int i = size-1;
while(!get(i).equals(e)) {
i--;
}
if(i=0)
return i;
else
return -1;
}
public void remove(int i) {
if(i=0 isize) {
Node p = null;
if(i == 0)
p = head;
else {
p = index(i-1);
}
p.next = index(i).next;
}
size --;
}
public void set(int i, Object e) {
Node p = index(i);
p.data = e;
}
public int size() {
return size;
}
@Override
public boolean equals(Object obj) {
SinglyLinkedList list = (SinglyLinkedList)obj;
if(this == obj size == list.size) {
return true;
}
return false;
}
/**
* 测试线性表
* @param args
*/
public static void main(String args[]) {
//List list = new LinkList();
//List list = new DoubleLinkList();
SinglyLinkedList list1 = new SinglyLinkedList();
for(int i=0; i10; i++) {
list1.add(new Integer(i));
}
Integer [] a = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110};
SinglyLinkedList list = new SinglyLinkedList(a);
list.remove(9);
System.out.print("size:" + list.size() + "\n");
System.out.println("isEmpty:" + list.isEmpty());
System.out.print("第7个位置的元素:" + list.get(7) + "\n");
list.concat(list1);
for(int i=0; ilist.size(); i++) {
System.out.print(list.get(i) + " ");
}
list.add(21);
list.add(22);
list.insert(3, new Integer(5));
System.out.print("size:" + list.size() + "\n");
System.out.print("第一次出现5的索引:" + list.indexOf(5) + "\n");
System.out.print("最后一次出现5的索引:" + list.lastIndexOf(5) + "\n");
list.set(0, new Integer(30));
for(int i=0; ilist.size(); i++) {
System.out.print(list.get(i) + " ");
}
SinglyLinkedList list2 = list;
System.out.println("\n is equels? " + list2.equals(list));
}
}
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, "没有找到有关节点");
}
}
}
}