Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class SLL {
- private SLLNode first;
- class SLLNode implements Comparable<SLLNode> {
- protected int id;
- protected int salary;
- protected SLLNode succ;
- public SLLNode(int id, int salary, SLLNode succ) {
- this.id = id;
- this.salary = salary;
- this.succ = succ;
- }
- @Override
- public int compareTo(SLLNode node) {
- if (this.id < node.id) return -1;
- else if (this.id > node.id) return 1;
- return 0;
- }
- @Override
- public String toString() {
- return String.format("(%d,%d)", id, salary);
- }
- }
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public void insertFirst(int id, int salary) {
- SLLNode ins = new SLLNode(id, salary, first);
- first = ins;
- }
- public void insertLast(int id, int salary) {
- if (first != null) {
- SLLNode tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode ins = new SLLNode(id, salary, null);
- tmp.succ = ins;
- } else {
- insertFirst(id, salary);
- }
- }
- public SLLNode getFirst() {
- return first;
- }
- public SLL copy() {
- SLLNode first = getFirst();
- SLL result = new SLL();
- while (first != null) {
- result.insertLast(first.id, first.salary);
- first = first.succ;
- }
- return result;
- }
- @Override
- public String toString() {
- if (first != null) {
- SLLNode temp = first;
- StringBuffer stringBuffer = new StringBuffer();
- stringBuffer.append(temp + " -> ");
- while (temp.succ != null) {
- temp = temp.succ;
- stringBuffer.append(temp + " -> ");
- }
- stringBuffer.append("END\n");
- return stringBuffer.toString();
- } else {
- return "The linked list is empty.";
- }
- }
- public SLL delete_smaller_from(int value) {
- SLL result = new SLL();
- SLLNode first = this.getFirst();
- while (first != null) {
- if (first.salary >= value) {
- result.insertLast(first.id, first.salary);
- }
- first = first.succ;
- }
- return result;
- }
- public SLL sort_descending() {
- SLLNode first = getFirst();
- int temp;
- SLLNode previous = null, current = null, following = null;
- if (first == null) {
- System.out.println("nema");
- return this;
- }
- for (int i = 0; i < 10; i++) {
- first = getFirst();
- while (first != null && first.succ != null) {
- current = first;
- following = first.succ;
- if (current.id < following.id) {
- /* SWAP DATA */
- temp = current.id;
- current.id = following.id;
- following.id = temp;
- temp = current.salary;
- current.salary = following.salary;
- following.salary = temp;
- }
- //System.out.print(this.toString());
- first = first.succ;
- }
- }
- return this;
- }
- /* public SLL sort_descending() {
- SLLNode first = getFirst();
- if (first == null) {
- System.out.println("nema");
- return this;
- }
- SLLNode previous = null, current = null, following = null, temp = null;
- while (first.succ != null) {
- *//*previous = first;
- current.succ = first.succ;
- following.succ = current;*//*
- if (first.id < first.succ.id) {
- //keep a pointer to succ element of first
- current = first.succ;
- //make first point to succ element
- first.succ = current.succ;
- current.succ = first;
- first = current;
- //current has moved one step back it points to first.
- //so get it to the finished swap position
- current = current.succ;
- while (current.succ != null && current.succ.succ != null) {
- temp = current.succ.succ;
- current.succ.succ = temp.succ;
- temp.succ = current.succ;
- current.succ = temp;
- current = temp.succ;
- }
- }
- first = first.succ;
- }
- return this;
- }*/
- /*public SLL sort_descending() {
- if (first == null) {
- System.out.print("nema");
- return this;
- }
- boolean flag = true;
- while (flag) {
- flag = false;
- SLLNode current = first;
- SLLNode following = first;
- SLLNode temp;
- while (current.succ != null)
- if (current.id < current.succ.id) {
- flag = true;
- temp = current.succ;
- current.succ = temp.succ;
- temp.succ = current;
- if (current == first) following = first = temp;
- else following = following.succ = temp;
- } else {
- if (current != first) following = following.succ;
- current = current.succ;
- }
- }
- return this;
- }*/
- public void pecati(SLL lista) {
- SLLNode p = lista.first;
- while (p != null) {
- System.out.println(p.id + " " + p.salary);
- p = p.succ;
- }
- }
- }
- public class SLLKompanija {
- public static void main(String[] args) throws IOException {
- SLL lista1 = new SLL();
- BufferedReader stdin = new BufferedReader(new InputStreamReader(
- System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- for (int i = 0; i < N; i++) {
- s = stdin.readLine();
- String s1 = stdin.readLine();
- lista1.insertLast(Integer.parseInt(s), Integer.parseInt(s1));
- }
- s = stdin.readLine();
- lista1 = lista1.delete_smaller_from(Integer.parseInt(s));
- if (lista1 != null) {
- lista1 = lista1.sort_descending();
- lista1.pecati(lista1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement