Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class DLLNode<E>{
- DLLNode<E> prev;
- DLLNode<E> next;
- E ID;
- E plata;
- DLLNode( E ID, E plata, DLLNode<E> prev, DLLNode<E> next){
- this.prev = prev;
- this.next = next;
- this.ID = ID;
- this.plata = plata;
- }
- }
- class DLL<E>{
- DLLNode<E> first;
- DLLNode<E> last;
- DLL(){
- first = null;
- last = null;
- }
- public void insertFirst(E o, E o1) {
- DLLNode<E> ins = new DLLNode<E>(o,o1, null, first);
- if (first == null)
- last = ins;
- else
- first.prev = ins;
- first = ins;
- }
- public void insertLast(E o, E o1) {
- if (first == null)
- insertFirst(o, o1);
- else {
- DLLNode<E> ins = new DLLNode<E>(o, o1, last, null);
- last.next = ins;
- last = ins;
- }
- }
- public void izbrisi(E plata){
- DLLNode<E> tmp = first;
- while(tmp != null){
- if((Integer)tmp.plata < (Integer)plata)delete(tmp);
- tmp = tmp.next;
- }
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.next;
- if (first != null) first.prev = null;
- if (first == null)
- last = null;
- return tmp.ID;
- } else
- return null;
- }
- public E deleteLast() {
- if (first != null) {
- if (first.next == null)
- return deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.prev;
- last.next = null;
- return tmp.ID;
- }
- }
- // else throw Exception
- return null;
- }
- public E delete(DLLNode<E> node) {
- if(node==first){
- deleteFirst();
- return node.ID;
- }
- if(node==last){
- deleteLast();
- return node.ID;
- }
- node.prev.next = node.next;
- node.next.prev = node.prev;
- return node.ID;
- }
- public void podredipoId(){
- DLLNode<E> tmp = first;
- while(tmp !=null){
- DLLNode<E> tmp1 = tmp.next;
- while(tmp1 != null){
- if((Integer)tmp1.ID > (Integer)tmp.ID){
- E idd = tmp.ID;
- E plataa = tmp.plata;
- tmp.ID = tmp1.ID;
- tmp.plata = tmp1.plata;
- tmp1.ID = idd;
- tmp1.plata = plataa;
- }
- tmp1 = tmp1.next;
- }
- tmp = tmp.next;
- }
- }
- public void pecati(){
- DLLNode<E> tmp = first;
- int gol=0;
- while(tmp != null){
- System.out.println(tmp.ID+" "+tmp.plata);
- tmp = tmp.next;
- gol++;
- }
- if(gol == 0)System.out.println("nema");
- }
- }
- public class DLLKompanija{
- public static void main(String [] args){
- Scanner m = new Scanner(System.in);
- int cit = m.nextInt();
- DLL<Integer> vraboteni = new DLL<>();
- for(int i=0; i<cit; i++){
- int id = m.nextInt();
- int plata = m.nextInt();
- vraboteni.insertLast(id, plata);
- }
- int proveri = m.nextInt();
- vraboteni.izbrisi(proveri);
- vraboteni.podredipoId();
- vraboteni.pecati();
- }
- }
Add Comment
Please, Sign In to add comment