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 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;
- }
- }
- class SLL {
- private SLLNode first;
- 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 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 tmp;
- SLLNode current = null;
- SLLNode following = null;
- if(first == null){
- System.out.println("empty");
- return this;
- }
- for(int i = 0 ; i < 100; i++){
- first =getFirst();
- while(first!=null && first.succ != null){
- current = first;
- following = current.succ;
- if(current.id <following.id){
- tmp=current.id;
- current.id = following.id;
- following.id = tmp;
- tmp = current.salary;
- current.salary = following.salary;
- following.salary = tmp;
- }
- first=first.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