Sie sind auf Seite 1von 4

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication1;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import java.util.Optional;
import java.util.List;
import java.util.Arrays;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.event.EventHandler;

class LinkedList{
private Node head;
private int size;
LinkedList(){
head=null;
size=0;
}
void AddFirst(int data) {
Node n = new Node(data, null);
n.setNext(head);
head=n;
size++;
}
void AddLast(int data) {
Node ptr = head;
while(ptr.getNext() != null) {
ptr = ptr.getNext();
}
Node n = new Node(data, null);
ptr.setNext(n);
size++;
}
void Display(){
Node ptr = head;
while(ptr.getNext() != null){
System.out.println(ptr.data);
}
}

public ObservableList<Node> getNodesList(){


ObservableList<Node> list = FXCollections.observableArrayList();

Node i = head;
while(i.getNext() != null){
i = i.getNext();
list.add(i);
}

return list;
}

public class JavaFXApplication1 extends Application {

void stack(Stage stage){


stack mystack = new stack();

HBox stackbox = new HBox();


Button pushbtn = new Button("Push");
Button popbtn = new Button("Pop");
Label msgpushed = new Label("");
Label lblpop = new Label("");
TextField val = new TextField("Enter value");

stackbox.setPadding(new Insets(15, 12, 15, 12));


stackbox.setSpacing(10);

pushbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
int pushval = Integer.parseInt(val.getText());
mystack.push(pushval);
msgpushed.setText("Value pushed: " + pushval);
}
});

popbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
lblpop.setText("Value popped: " + mystack.pop());

}
});

stackbox.getChildren().addAll(val,pushbtn, popbtn,msgpushed,lblpop);
stage.setScene(new Scene(stackbox, 500, 300));
stage.show();

public void linkListDS(Stage stage){

Button addbtn = new Button("Add");


TextField addfield = new TextField();
LinkedList l = new LinkedList();

TableView<Node> table = new TableView();


TableColumn<Node, Integer> data = new TableColumn("Data");
TableColumn<Node, Node> next = new TableColumn("Next Node");

addbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
int a = Integer.parseInt(addfield.getText());
l.AddFirst(a);
table.setItems(l.getNodesList());
}
});

data.setCellValueFactory(new PropertyValueFactory<>("data"));
next.setCellValueFactory(new PropertyValueFactory<>("next"));
table.getColumns().addAll(data, next);

HBox box = new HBox();


box.getChildren().addAll(addfield,addbtn,table);
stage.setScene(new Scene(box, 600, 300));
stage.show();

@Override
public void start(Stage primaryStage) {

final String [] arrayData = {"LinkedList", "Stack"};

List<String> dialogData;
dialogData = Arrays.asList(arrayData);
ChoiceDialog dialog = new ChoiceDialog(dialogData.get(0), dialogData);
dialog.setTitle("Data Structures");
dialog.setHeaderText("Select your choice");
Optional<String> result = dialog.showAndWait();
String selected = "";

if (result.isPresent()) {

selected = result.get();
switch(selected){
case "LinkedList":
System.out.println("LinkedList");
Stage stage = new Stage();
linkListDS(stage);
break;
case "Stack":
System.out.println("Stack");
Stage stagestack = new Stage();
stack(stagestack);
break;
default:
break;
}

}
//actionStatus.setText("Selection: " + selected);

// int m=Integer.parseInt("a");

// Button btn = new Button();


// btn.setText("Say 'Hello World'");
// btn.setOnAction(new EventHandler<ActionEvent>() {
//
// @Override
// public void handle(ActionEvent event) {
// System.out.println("Hello World!");
// }
// });
//

// Scene scene = new Scene(root, 300, 250);


//
// primaryStage.setTitle("Hello World!");
// primaryStage.setScene(scene);
// primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

launch(args);

Das könnte Ihnen auch gefallen