Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kompanija;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class SLLNode {
- protected int id;
- protected int plata;
- protected SLLNode succ;
- public SLLNode(int id, int plata, SLLNode succ) {
- this.id = id;
- this.plata = plata;
- 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 plata) {
- SLLNode ins = new SLLNode(id, plata, first);
- first = ins;
- }
- public void insertLast(int id, int plata) {
- if (first != null) {
- SLLNode tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode ins = new SLLNode(id, plata, null);
- tmp.succ = ins;
- } else {
- insertFirst(id, plata);
- }
- }
- public SLLNode getFirst() {
- return first;
- }
- public SLL brisi_pomali_od(int iznos) {
- // Vasiot kod tuka
- SLL lista = new SLL();// imam nova prazna lista
- SLLNode nodeFirst = first;// imam node od tekovnata lista vrz koj go
- // povikuvame metodot
- boolean flag;
- while (nodeFirst != null) {
- flag = true;
- if (nodeFirst.plata < iznos) {// ako platata e pomala od iznosot
- nodeFirst = nodeFirst.succ;// smeni go pointerot na sledniot
- // element
- flag = false;
- }
- else if (flag) {// ako platata ne e pomala od iznosot ke ostane true
- lista.insertLast(nodeFirst.id, nodeFirst.plata);// vnesi go
- // tekovniot
- // element vo
- // listata
- nodeFirst = nodeFirst.succ;// pointer na sledniot element
- }
- }
- return lista;// treba da vrati lista SLL
- }
- public SLL sortiraj_opagacki() {// sortiraj opagjacki spored ID
- // Vasiot kod tuka
- SLL listaSortirana = new SLL();
- SLLNode i = first;
- SLLNode j = first;
- SLLNode temp = listaSortirana.getFirst();
- boolean flag = false;
- int maxId;
- int maxPlata;
- while (i.succ != null) {// dodeka ne ja pomine prviot listata
- maxPlata = i.plata;// vo sekoja nova iteracija zemaj go kako
- // najgolem
- maxId = i.id;// vo sekoja nova iteracija zemaj go kako najgolem
- flag = false;
- j = i.succ;// pri nova iteracija na i, pomesti go j vo desno
- while (j != null) {// dodeka j ne dojde do kraj
- if (maxId < j.id) {// ako maksimalniot id e pomal od id-to na j
- maxId = j.id;// stavi go id-to na j kako maksimalno
- maxPlata = j.plata;
- temp = j;// zacuvaj go j-tiot jazel koj ima maksimalen id
- flag = true;
- }
- j = j.succ;// odi na sledniot j do kraj
- } // kraj na while za J
- if (flag) {// ako sme nasle maksimalen element
- temp.id = i.id;
- temp.plata = i.plata;
- }
- listaSortirana.insertLast(maxId, maxPlata);
- i = i.succ;// pred nova iteracija, zgolemi go i da pokazuva na
- // sledniot
- }
- listaSortirana.insertLast(i.id, i.plata);
- return listaSortirana;
- }
- public void pecati(SLL lista) {
- SLLNode p = lista.first;
- while (p != null) {
- System.out.println(p.id + " " + p.plata);
- 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);// broj na vraboteni
- for (int i = 0; i < N; i++) {// odi do broj na vraboteni
- s = stdin.readLine();
- String s1 = stdin.readLine();
- lista1.insertLast(Integer.parseInt(s), Integer.parseInt(s1));// smestuvaj
- // vo
- // lista
- // soodvetno
- }
- s = stdin.readLine();// vcitaj iznos
- lista1 = lista1.brisi_pomali_od(Integer.parseInt(s));// otstrani
- // elementi so
- // pomala plata
- // od vneseniot
- // iznos
- if (lista1.getFirst() == null) {
- System.out.println("nema");
- }
- else if (lista1 != null) {
- lista1 = lista1.sortiraj_opagacki();// povikaj metod za sortiranje
- // po opagjacki redosled
- lista1.pecati(lista1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement