Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SortedList {
- public Node first;
- public SortedList() {
- }
- public SortedList cons(int n) {
- Node node = this.first;
- if(this.first == null) {
- this.first = new Node(n, null);
- return this;
- }
- while(node.next != null) {
- node = node.next;
- }
- Node a = new Node(n, null);
- node.setNext(a);
- this.Sort();
- System.out.println("Sorted call");
- return this;
- }
- public int head() {
- return first.next.value;
- }
- public SortedList tail() {
- SortedList l = new SortedList();
- Node node = this.first;
- while(node.next != null) {
- node = node.next;
- }
- l.cons(node.value);
- return l;
- }
- public boolean isNil() {
- if(this.first == null)
- return true;
- else
- return false;
- }
- public int length() {
- Node node = this.first;
- int i = 0;
- while (node.next != null) {
- node = node.next;
- ++i;
- }
- return i;
- }
- public SortedList append(SortedList l) {
- Node node = this.first;
- while(node.next != null) {
- node = node.next;
- }
- Node node2 = l.first;
- while(node2.next != null) {
- this.cons(node2.next.value);
- node2 = node2.next;
- }
- this.Sort();
- return this;
- }
- public int last() {
- Node node = this.first;
- while(node.next != null) {
- node = node.next;
- }
- return node.value;
- }
- public SortedList init() {
- SortedList l = new SortedList();
- Node node = l.first;
- while(node.next != null) {
- node = node.next;
- l.cons(node.value);
- }
- return l;
- }
- public void Sort() {
- int temp;
- for(int i=1; i<this.length(); i++) {
- for(int j=0; j<this.length()-i; j++) {
- if(this.getbyIndexVal(j)>this.getbyIndexVal(j+1)) {
- System.out.println("Switched Values");
- temp=this.getbyIndexVal(j);
- this.getbyIndexNode(j).setValue(this.getbyIndexVal(j+1));
- this.getbyIndexNode(j+1).setValue(temp);
- }
- }
- }
- }
- public int getbyIndexVal(int i) {
- if(i >= this.length()) return 0;
- int j = 0;
- Node node = this.first;
- while(i != j) {
- node = node.next;
- j++;
- }
- return node.value;
- }
- public Node getbyIndexNode(int i) {
- if(i >= this.length()) return first;
- int j = 0;
- Node node = this.first;
- while(i != j) {
- node = node.next;
- j++;
- }
- return node;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement