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;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- /**
- *
- * @author Damjan
- */
- public class SLLKompanija {
- public static void main(String[] args) throws IOException {
- SLL<Vraboten> kompanija = new SLL<>();
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String input = reader.readLine();
- int vraboteni = Integer.parseInt(input);
- for (int i = 0; i < vraboteni; ++i) {
- input = reader.readLine();
- String plata = reader.readLine();
- kompanija.insertLast(new Vraboten(Integer.parseInt(input), Integer.parseInt(plata)));
- }
- //input = reader.readLine(); // plata spored koja da brise
- SLL<Vraboten> sorted = sortiraj_opagacki(kompanija);
- //brisiPomaliOd(sorted, Integer.parseInt(input));
- SLLNode<Vraboten> pecati = sorted.getFirst();
- if (pecati == null) {
- System.out.println("nema");
- } else {
- while (pecati != null) {
- System.out.println(pecati.element.toString());
- pecati = pecati.succ;
- }
- }
- }
- static void brisiPomaliOd(SLL<Vraboten> lista, int iznos) {
- SLLNode<Vraboten> jazol = lista.getFirst();
- while (jazol != null) {
- if (jazol.element.getPlata() < iznos) {
- lista.delete(jazol);
- }
- jazol = jazol.succ;
- }
- }
- static SLL<Vraboten> sortiraj_opagacki(SLL<Vraboten> lista) {
- SLL<Vraboten> sortirana = new SLL<>();
- SLLNode<Vraboten> jazol = lista.getFirst();
- SLLNode<Vraboten> jazol1 = jazol.succ;
- while (jazol != null && jazol1 != null) {
- if (jazol.element.compareTo(jazol1.element) < 0) {
- sortirana.insertLast(jazol.element);
- jazol = jazol.succ;
- } else {
- sortirana.insertLast(jazol1.element);
- jazol1 = jazol.succ;
- }
- }
- if (jazol != null) {
- while (jazol1 != null) {
- sortirana.insertLast(jazol.element);
- jazol1 = jazol.succ;
- }
- }
- if (jazol1 != null) {
- while (jazol1 != null) {
- sortirana.insertLast(jazol.element);
- jazol1 = jazol1.succ;
- }
- }
- return sortirana;
- }
- }
- class Vraboten implements Comparable<Vraboten> {
- private int id;
- private int plata;
- public Vraboten(int id, int plata) {
- this.id = id;
- this.plata = plata;
- }
- public int getId() {
- return id;
- }
- public int getPlata() {
- return plata;
- }
- @Override
- public String toString() {
- return String.format("%d %d", id, plata);
- }
- @Override
- public int compareTo(Vraboten o) {
- return Integer.compare(id, o.id);
- }
- }
- class SLLNode<E extends Comparable<E>> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class SLL<E extends Comparable<E>> {
- private SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else {
- return 0;
- }
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + " ";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + " ";
- }
- } else {
- ret = "Prazna lista!!!";
- }
- return ret;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- public void insertAfter(E o, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == before) {
- this.insertFirst(o);
- return;
- }
- //ako first!=before
- while (tmp.succ != before) {
- tmp = tmp.succ;
- }
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o, before);
- tmp.succ = ins;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- }
- public void insertLast(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- }
- SLLNode<E> ins = new SLLNode<E>(o, null);
- tmp.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public E deleteFirst() {
- if (first != null) {
- SLLNode<E> tmp = first;
- first = first.succ;
- return tmp.element;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == node) {
- return this.deleteFirst();
- }
- while (tmp.succ != node && tmp.succ.succ != null) {
- tmp = tmp.succ;
- }
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.element;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> find(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.element != o && tmp.succ != null) {
- tmp = tmp.succ;
- }
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public Iterator<E> iterator() {
- // Return an iterator that visits all elements of this list, in left-to-right order.
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E extends Comparable<E>> implements Iterator<E> {
- private SLLNode<E> place, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null) {
- throw new NoSuchElementException();
- }
- E nextElem = place.element;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- //Not implemented
- }
- }
- public void mirror() {
- if (first != null) {
- //m=nextsucc, p=tmp,q=next
- SLLNode<E> tmp = first;
- SLLNode<E> newsucc = null;
- SLLNode<E> next;
- while (tmp != null) {
- next = tmp.succ;
- tmp.succ = newsucc;
- newsucc = tmp;
- tmp = next;
- }
- first = newsucc;
- }
- }
- public void merge(SLL<E> in) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- }
- tmp.succ = in.getFirst();
- } else {
- first = in.getFirst();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment