Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class DLLKompanija {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- Company company = new Company();
- for (int i = 0; i < n; i++) {
- int id = scanner.nextInt();
- int salary = scanner.nextInt();
- company.insertLast(id, salary);
- }
- int salary = scanner.nextInt();
- company.removeEmployeesWithLowerSalary(salary);
- company.sortByIds();
- System.out.println(company);
- }
- }
- class Employee {
- protected int ID;
- protected int salary;
- protected Employee pred, succ;
- public Employee(int ID, int salary, Employee pred, Employee succ) {
- this.ID = ID;
- this.salary = salary;
- this.pred = pred;
- this.succ = succ;
- }
- public int getID() {
- return ID;
- }
- public int getSalary() {
- return salary;
- }
- @Override
- public String toString() {
- return ID + " " + salary;
- }
- }
- class Company {
- Employee first, last;
- public Company() { first = last = null; }
- public void deleteList() { first = last = null; }
- public int length() {
- int ret;
- if (first != null) {
- Employee tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public void insertFirst(int ID, int salary) {
- Employee ins = new Employee(ID, salary, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(int ID, int salary) {
- if (first == null)
- insertFirst(ID, salary);
- else {
- Employee ins = new Employee(ID, salary, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(int ID, int salary, Employee after) {
- if (after == last) {
- insertLast(ID, salary);
- return;
- }
- Employee ins = new Employee(ID, salary, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public int deleteFirst() {
- if (first != null) {
- Employee tmp = first;
- first = first.succ;
- if(first != null) first.pred = null;
- if (first == null)
- last = null;
- return tmp.getID();
- } else
- return -1;
- }
- public int deleteLast() {
- if (first != null) {
- if (first.succ == null)
- return deleteFirst();
- else {
- Employee tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.getID();
- }
- }
- // else throw Exception
- return -1;
- }
- public int delete(Employee node) {
- if (node == first) {
- deleteFirst();
- return node.getID();
- }
- if (node == last) {
- deleteLast();
- return node.getID();
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- return node.getID();
- }
- public void removeEmployeesWithLowerSalary(int salary) {
- Employee current = first;
- while(current != null) {
- if(current.getSalary() < salary) {
- if(current == last) deleteLast();
- else delete(current);
- }
- current = current.succ;
- }
- }
- public void sortByIds() {
- Employee tmp1 = first;
- Employee tmp2 = null;
- while (tmp1 != null) {
- tmp2 = tmp1.succ;
- while (tmp2 != null) {
- if(tmp1.getID() < tmp2.getID()) {
- int id = tmp1.getID();
- int sal = tmp1.getSalary();
- tmp1.ID = tmp2.ID;
- tmp1.salary = tmp2.salary;
- tmp2.ID = id;
- tmp2.salary = sal;
- }
- tmp2 = tmp2.succ;
- }
- tmp1 = tmp1.succ;
- }
- }
- @Override
- public String toString() {
- StringBuilder stringBuilder = new StringBuilder();
- Employee it = first;
- while (it != null) {
- stringBuilder.append(it + "\n");
- it = it.succ;
- }
- if(stringBuilder.toString().length() == 0) return "nema";
- else return stringBuilder.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement