Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Компанија Problem 2 (1 / 4)
- Податоците за плати на вработените во една компанија привремено се чуваат во двострано поврзана листа. Во секој јазол од листата се чува единствен ID на вработениот и неговата плата. Потребно е да се отстранат сите вработени со помали плати од даден износ, а остатокот да се прикажат во опаѓачки редослед во однос на ID-то.
- Во првиот ред од влезот е даден бројот на вработени, потоа наизменично се дадени ID-та и платата за секој од вработените и во последниот ред е износот во однос на кој ќе се отстрануваат вработените. На излез се печати листа (ID, плата) во опаѓачки редослед според ID-то на секој од вработените.
- Доколку нема вработени со плата поголема од дадената да се испечати: nema
- Име на класата: DLLKompanija
- ========================================================================================================================================
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class DLLKompanija {
- public static class DLLNode<E> {
- protected int ID;
- protected int plata;
- protected DLLNode<E> pred, succ;
- public DLLNode(int ID, int plata, DLLNode<E> pred, DLLNode<E> succ) {
- this.ID = ID;
- this.plata = plata;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return ID + " " + plata;
- }
- }
- public static class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- /* public DLLNode<E> find(E o) {
- if (first != null) {
- DLLNode<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 void insertFirst(int id, int plata) {
- DLLNode<E> ins = new DLLNode<E>(id,plata, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(int id, int plata) {
- if (first == null)
- insertFirst(id,plata);
- else {
- DLLNode<E> ins = new DLLNode<E>(id, plata , last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(int id, int plata, DLLNode<E> after) {
- if(after==last){
- insertLast(id, plata);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(id, plata , after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(int id, int plata, DLLNode<E> before) {
- if(before == first){
- insertFirst(id, plata);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(id,plata, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- public void deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- if (first != null) first.pred = null;
- if (first == null)
- last = null;
- //return tmp.element;
- }
- //return null;
- }
- public void deleteLast() {
- if (first != null) {
- if (first.succ == null)
- deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- //return tmp.element;
- }
- }
- // else throw Exception
- //return null;
- }
- public void delete(DLLNode<E> node) {
- if(node==first){
- deleteFirst();
- return;
- //return node.element;
- }
- if(node==last){
- deleteLast();
- return;
- //return node.element;
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- //return node.element;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public String toStringR() {
- String ret = new String();
- if (last != null) {
- DLLNode<E> tmp = last;
- ret += tmp + "<->";
- while (tmp.pred != null) {
- tmp = tmp.pred;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- public void izvadiDupliIPrebroj(){
- }
- }
- public static void deleteElements(DLL<Integer> lista, int plata){
- // todo Proba da rabotam so pointeri
- DLLNode<Integer> p = lista.getFirst();
- while(p != null){
- if(p.plata < plata){
- DLLNode<Integer> temp = p;
- p = p.succ;
- lista.delete(temp);
- }else{
- p = p.succ;
- }
- }
- // sort the list
- if(lista.length() == 0){
- System.out.println("nema");
- return;
- }
- sortList(lista);
- DLLNode<Integer> pok = lista.getFirst();
- while(pok.succ != null){
- System.out.println( pok.ID + " " + pok.plata);
- pok = pok.succ;
- }
- System.out.println(pok.ID + " " + pok.plata);
- }
- public static void sortList(DLL<Integer> list){
- DLLNode<Integer> sortedFirst = null;
- DLLNode<Integer> sorted = null;
- DLLNode<Integer> max = null;
- DLLNode<Integer> pok = list.getFirst();
- while(list.length() != 0){
- max = list.getFirst();
- pok = list.getFirst();
- while(pok != null){
- if(pok.ID > max.ID){
- max = pok;
- }
- pok = pok.succ;
- }
- // go ima max;
- if(sortedFirst == null){
- sortedFirst = max;
- sorted = max;
- }else{
- sorted.succ = max;
- sorted = sorted.succ;
- }
- list.delete(max);
- }
- list.first = sortedFirst;
- }
- public static void main(String []args) throws IOException {
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(input.readLine());
- DLL<Integer> lista = new DLL<Integer>();
- for(int i = 0; i < n; i++){
- Integer id = Integer.parseInt(input.readLine());
- Integer plata = Integer.parseInt(input.readLine());
- lista.insertLast(id,plata);
- }
- Integer plata = Integer.parseInt(input.readLine());
- deleteElements(lista,plata);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement