Hi Leute ,
ich brauche wieder mal eure hilfe.
Undzwar muss ich ein Projekt machen wo ich meine Abfahrtstation und die Zielstation in einem Textfield eingeben kann. Als Endprodukt soll denn in einem Textarea die Abfahrtstation + auch die zwischen Station anzeigen + Zielstation + die Reisedauer angezeigt werden.
So meine frage ist jetzt, kann man in einem Linkedlist durch eingabe in einem Textfield die bestimmten station suchen und in einem Textarea anzeigen lassen. Und außerdem will ich auch noch die Zeit berechnen lassen. z.B. will gebe ich in dem Textfield "Malostranska" ein und will nach "Florence". In dem Textarea soll er mir anzeigen welche die kürzeste route ist und wie lange die Reise dauren soll.
Hier meine LinkedList
ich brauche wieder mal eure hilfe.
Undzwar muss ich ein Projekt machen wo ich meine Abfahrtstation und die Zielstation in einem Textfield eingeben kann. Als Endprodukt soll denn in einem Textarea die Abfahrtstation + auch die zwischen Station anzeigen + Zielstation + die Reisedauer angezeigt werden.
So meine frage ist jetzt, kann man in einem Linkedlist durch eingabe in einem Textfield die bestimmten station suchen und in einem Textarea anzeigen lassen. Und außerdem will ich auch noch die Zeit berechnen lassen. z.B. will gebe ich in dem Textfield "Malostranska" ein und will nach "Florence". In dem Textarea soll er mir anzeigen welche die kürzeste route ist und wie lange die Reise dauren soll.
Hier meine LinkedList
Code:
package linklist;
import java.util.*;
public class SLinkedList {
protected StringNode head;
public SLinkedList() {
head = new StringNode();
}
public static void main(String[] args) {
// set up a linked list of 4 integers
SLinkedList greenline = new SLinkedList();
greenline.addFirst("Dejvicka",500);
greenline.addMid("Hradcanska",500,"Dejvicka");
greenline.addMid("Malostranska",500,"Hradcanska");
greenline.addMid("Staromestska",500,"Malostranska");
greenline.addMid("Mustek",500,"Staromestska");
greenline.addMid("Muzeum",500,"Mustek");
greenline.addMid("Namesti",500,"Muzeum");
greenline.addMid("Jirihoz Podebrad",500,"Namesti");
greenline.addMid("Flora",500,"Jirihoz Podebrad");
greenline.addMid("Zelivskeho",500,"Flora");
greenline.addMid("Stransnicka",500,"Zelivskeho");
greenline.addMid("Skalka",500,"Stransnicka");
greenline.addLast("Depo Hostivar");
printList(greenline);
SLinkedList yellowline = new SLinkedList();
yellowline.addFirst("Zlicin",500);
yellowline.addMid("Stodulky",500,"Zlicin");
yellowline.addMid("Luka",500,"Stodulky");
yellowline.addMid("Luziny",500,"Luka");
yellowline.addMid("Hurka",500,"Luziny");
yellowline.addMid("Nove Butovice",500,"Hurka");
yellowline.addMid("Jinonice",500,"Nove Butovice");
yellowline.addMid("Radlicka",500,"Jinonice");
yellowline.addMid("Smichovske Nadrazi",500,"Radlicka");
yellowline.addMid("Andel",500,"Smichovske Nadrazi");
yellowline.addMid("Karlovo Namesti",500,"Andel");
yellowline.addMid("Nardonitrada",500,"Karlovo Namesti");
yellowline.addMid("Mustek",500,"Nardonitrada");
yellowline.addMid("NR",500,"Mustek");
yellowline.addMid("Florence",500,"NR");
yellowline.addMid("Krizikova",500,"Florence");
yellowline.addMid("Invalidovna",500,"Krizikova");
yellowline.addMid("Palmovka",500,"Invalidovna");
yellowline.addMid("CM",500,"Palmovka");
yellowline.addMid("Vysocanska",500,"CM");
yellowline.addMid("Kolbenova",500,"Vysocanska");
yellowline.addMid("Hloubetin",500,"Kolbenova");
yellowline.addMid("Rajska Zahrada",500,"Hloubetin");
yellowline.addLast("Cerry Most");
printList(yellowline);
SLinkedList redline = new SLinkedList();
redline.addFirst("Ladvi",500);
redline.addMid("Kobylisy",500,"Ladvi");
redline.addMid("Nadrazi Holesovic",500,"Kobylisy");
redline.addMid("Vltavska",500,"Nadrazi Holesovic");
redline.addMid("Florence",500,"Vltavska");
redline.addMid("Hlavni",500,"Florence");
redline.addMid("Muzeum",500,"Hlavni");
redline.addMid("I.P.Pavlova",500,"Muzeum");
redline.addMid("Vysehrad",500,"I.P.Pavlova");
redline.addMid("Prazskeho Povstani",500,"Vysehrad");
redline.addMid("Pankrac",500,"Prazskeho Povstani");
redline.addMid("Budejovicka",500,"Pankrac");
redline.addMid("Kacerov",500,"Budejovicka");
redline.addMid("Roztyly",500,"Kacerov");
redline.addMid("Chodov",500,"Roztyly");
redline.addMid("Opatov",500,"Chodov");
redline.addLast("Haje");
printList(redline);
}
//add a new node to the head of the list
private void addFirst(String element,int distance) {
// make variable head point to new node
head = new StringNode(element,distance,head);
}
private void addLast(String element) {
StringNode tail;
tail = head;
while (tail.getNext() != null) {
tail = tail.getNext();
}
//insert new node at end of list
tail.setNext( new StringNode(element));
}
//add a new node after position of curnode
private void addMid(String element,int distance ,String entryafter) {
StringNode curnode;
curnode = head;
//go to last node and remember previous node at all times
while (curnode != null && curnode.getElement() != entryafter) {
curnode = curnode.getNext();
}
//if first occurrence of element entryafter was located then insert new node
if (curnode != null) {
StringNode newnode = new StringNode(element,distance,curnode.getNext());
curnode.setNext(newnode);
}
}
private boolean isEmpty() {
return head == null;
}
private void removeFirst() {
StringNode oldhead;
oldhead = head;
//remove first node from linked list
if (head != null) {
head = head.getNext();
oldhead.setNext(null);
}
else {
throw new NoSuchElementException();
}
}
private void removeLast() {
StringNode temp, previous;
temp = head;
previous = temp;
//go to last node and remember previous node at all times
while (temp != null && temp.getNext() != null) {
previous = temp;
temp = temp.getNext();
}
if (previous != null) {
//remove last node
previous.setNext(null);
}
else {
throw new NoSuchElementException();
}
}
//very similar to removeLast except we are looking for element i
private void removeMid(String element) {
StringNode temp, previous;
temp = head.getNext();
previous = null;
//go to node containing element and rermember previous node at all times
while (temp.getElement() != element && temp.getNext() != null) {
previous = temp;
temp = temp.getNext();
}
if (previous != null && temp.getNext() != null) {
//not first or last node so we can remove node defined by temp.
// set the previous node to that after temp
previous.setNext(temp.getNext());
temp.setNext(null);
}
else {
throw new NoSuchElementException();
}
}
public static void printList(SLinkedList thelist) {
StringNode temp;
if(thelist.isEmpty())
System.out.println("List is empty");
else {
temp = thelist.head;
while (temp != null) {
System.out.println(temp.getElement()+" "+temp.getDistance());
temp = temp.getNext();
}
System.out.println();
}
}
}