Advertisement
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;
- 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 E deleteFirst() {
- if (first != null) {
- SLLNode tmp = first;
- first = first.succ;
- return tmp.element;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode node) {
- if (first != null) {
- SLLNode 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 SLL brisi_pomali_od(int iznos) {
- SLLNode jazol1=this.getFirst();
- SLL rezultat=new SLL();
- while(jazol1!=null)
- {
- if(jazol1.plata>=iznos)
- {
- rezultat.insertLast(jazol1.id,jazol1.plata);
- jazol1=jazol1.succ;
- }
- else {
- jazol1=jazol1.succ;
- }
- }
- return rezultat;
- }
- public SLL sortiraj_opagacki() {
- SLL list=new SLL();
- SLLNode jazol1=this.getFirst();
- SLLNode jazol2=null;
- SLLNode pom=this.getFirst();
- int tmp;
- int tmp1;
- while(jazol1!=null)
- {
- if(jazol1.succ!=null)
- {
- jazol2=jazol1.succ;
- }
- else break;
- while(jazol2!=null)
- {
- if(jazol1.id<jazol2.id)
- {
- tmp=jazol1.id;
- tmp1=jazol1.plata;
- //pom.id=jazol1.id;
- //pom.plata=jazol1.plata;
- jazol1.id=jazol2.id;
- jazol1.plata=jazol2.plata;
- jazol2.id=tmp;
- jazol2.plata=tmp1;
- jazol2=jazol2.succ;
- }
- else
- {
- jazol2=jazol2.succ;
- }
- }
- jazol1=jazol1.succ;
- }
- return this;
- }
- public void pecati (SLL lista)
- {
- SLLNode p=lista.first;
- while(p!=null)
- {
- System.out.println(p.id+" "+p.plata);
- p=p.succ;
- }
- }
- public void nema()
- {
- System.out.println("nema");
- }
- }
- 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);
- for (int i = 0; i < N; i++) {
- s=stdin.readLine();
- String s1=stdin.readLine();
- lista1.insertLast(Integer.parseInt(s),Integer.parseInt(s1));
- }
- s = stdin.readLine();
- lista1=lista1.brisi_pomali_od(Integer.parseInt(s));
- if(lista1!=null)
- {
- lista1=lista1.sortiraj_opagacki();
- lista1.pecati(lista1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement