Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Компанија Problem 2 (1 / 22)
- Податоците за плати на вработените во една компанија привремено се чуваат во двострано поврзана листа. Во секој јазол од листата се чува единствен ID на вработениот и неговата плата. Потребно е да се отстранат сите вработени со помали плати од даден износ, а остатокот да се прикажат во опаѓачки редослед во однос на ID-то.
- Во првиот ред од влезот е даден бројот на вработени, потоа наизменично се дадени ID-та и платата за секој од вработените и во последниот ред е износот во однос на кој ќе се отстрануваат вработените. На излез се печати листа (ID, плата) во опаѓачки редослед според ID-то на секој од вработените.
- Доколку нема вработени со плата поголема од дадената да се испечати: nema
- Име на класата: DLLKompanija
- Sample input
- 3
- 1111
- 19000
- 2222
- 22000
- 1155
- 60000
- 30000
- Sample output
- 1155 60000
- import java.util.Scanner;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- class SLL<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, int p) {
- SLLNode<E> ins = new SLLNode<E>(o,p, first);
- first = ins;
- }
- public void insertAfter(E o,int p, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o,p, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o,int p, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if(first==before){
- this.insertFirst(o,p);
- return;
- }
- //ako first!=before
- while (tmp.succ != before)
- tmp = tmp.succ;
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o,p, 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, int p) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode<E> ins = new SLLNode<E>(o,p, null);
- tmp.succ = ins;
- } else {
- insertFirst(o,p);
- }
- }
- 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> 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();
- }
- }
- }
- class SLLNode<E> {
- protected E element;
- protected int plata;
- protected SLLNode<E> succ;
- public SLLNode(E elem,int plata, SLLNode<E> succ) {
- this.element = elem;
- this.plata = plata;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- public class SLLKompanija {
- public static void company(SLL<Integer> lista,int iznos){
- int suma = 0;
- SLLNode<Integer> tmp = lista.getFirst();
- while(tmp!=null) {
- if(tmp.plata<iznos) {
- lista.delete(tmp);
- }
- tmp = tmp.succ;
- }
- //System.out.print(lista.length());
- tmp = lista.getFirst();
- while(tmp!=null) {
- SLLNode<Integer> tmp2 = tmp.succ;
- while(tmp2!=null) {
- if(tmp.element<tmp2.element) {
- int smeniid = tmp.element;
- tmp.element = tmp2.element;
- tmp2.element = smeniid;
- int smeniplata = tmp.plata;
- tmp.plata = tmp2.plata;
- tmp2.plata = smeniplata;
- System.out.println(lista);
- }
- tmp2 = tmp2.succ;
- }
- tmp = tmp.succ;
- }
- System.out.print(lista);
- // tmp = lista.getFirst();
- // if(tmp==null) {System.out.print("nema");}else {
- // while(tmp!=null) {
- // System.out.print(tmp.element + " " + tmp.plata + "\n");
- // tmp = tmp.succ;
- // }
- // }
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- SLL<Integer> lista = new SLL<Integer>();
- for(int i=0;i<n;i++) {
- lista.insertLast(input.nextInt(), input.nextInt());
- }
- int iznos = input.nextInt();
- company(lista,iznos);
- }
- }
Add Comment
Please, Sign In to add comment