Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tse;
- import java.util.Scanner;
- class SLLNode<E> {
- public E element;
- public SLLNode<E> succ;
- public SLLNode() {
- }
- public SLLNode(E element, SLLNode<E> succ) {
- super();
- this.element = element;
- this.succ = succ;
- }
- }
- class SLL<E> {
- public SLLNode<E> first;
- public SLL() {
- this.first = null;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- 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 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;
- }
- public void show() {
- SLLNode<E> n = first;
- while (n != null) {
- System.out.print(n.element + " ");
- n = n.succ;
- }
- System.out.println();
- }
- 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 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 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 class sadasd {
- public static void main(String[] args) {
- Scanner s=new Scanner (System.in);
- SLL<Vozac> sll= new SLL<>();
- int n=s.nextInt();
- for(int i=0;i<n;i++)
- {
- Vozac b = new Vozac();
- b.ime=s.next();
- b.gorivo=s.nextInt();
- sll.insertLast(b);
- }
- SLLNode<Vozac> dvizi=sll.first;
- int m=s.nextInt();
- for(int j=0;j<m;j++){
- Vozac d = new Vozac();
- dvizi=sll.first;
- d.ime=s.next();
- d.gorivo=s.nextInt();
- while(dvizi.succ!=null)
- {
- if(dvizi.element.ime.equals(d.ime))
- {
- SLLNode<Vozac> tmp=dvizi;
- int gor=d.gorivo;
- for(int i=0;i<gor;i++)
- {
- if(tmp==null)
- {
- break;
- }
- tmp=tmp.succ;
- }
- d.gorivo=dvizi.element.gorivo-d.gorivo;
- sll.insertAfter(d,tmp);
- sll.delete(dvizi);
- dvizi=dvizi.succ;
- break;
- }else{
- dvizi=dvizi.succ;
- }
- }
- SLLNode<Vozac>pecati =sll.first;
- while(pecati!=null)
- {
- System.out.print(pecati.element.ime + "(" + pecati.element.gorivo + ")-> ");
- pecati=pecati.succ;
- }
- System.out.print("\n");
- }
- }
- }
- class Vozac
- {
- public String ime;
- public int gorivo;
- public Vozac(String ime,int gorivo) {
- super();
- this.ime = ime;
- this.gorivo=gorivo;
- }
- public Vozac() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement