Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- //
- // ***ITS WORKING***
- //
- //
- //
- import java.io.*;
- import java.util.Scanner;
- class LinkedList{
- protected LinkedListElement first;
- protected LinkedListElement last;
- LinkedList()
- {
- makenull();
- }
- public void makenull()
- {
- first = new LinkedListElement(null, null);
- last = null;
- }
- public String write()
- {
- String izhod = "";
- LinkedListElement el;
- el = first.next;
- while(el != null) {
- izhod += el.element;
- if(el.next != null) { izhod += ","; }
- el = el.next;
- }
- return izhod;
- }
- int length()
- {
- LinkedListElement el = first.next;
- int i = 0;
- while(el != null) {
- ++i;
- el = el.next;
- }
- return i;
- }
- boolean deleteNth(int n)
- {
- LinkedListElement el = first;
- LinkedListElement el2 = el;
- LinkedListElement el1;
- while(n > 0) {
- if(el == null) { return false; }
- el = el.next;
- if(n != 1) { el2 = el; }
- --n;
- }
- el1 = el.next;
- if(el1.next != null) {
- el.next = el1.next;
- } else {
- el.next = null;
- last = el2;
- }
- return true;
- }
- public void addLast(Object obj)
- {
- LinkedListElement el = new LinkedListElement(obj, null);
- LinkedListElement elZadnji;
- if(last != null) {
- elZadnji = last.next;
- elZadnji.next = el;
- last = elZadnji;
- } else {
- first.next = el;
- last = first;
- last.next = el;
- }
- }
- void addFirst(Object obj)
- {
- LinkedListElement el = new LinkedListElement(obj, null);
- LinkedListElement elPrvi;
- if(first.next != null) {
- elPrvi = first.next;
- el.next = elPrvi;
- first.next = el;
- } else {
- first.next = el;
- last = first;
- last.next = el;
- }
- }
- }
- class LinkedListElement{
- Object element;
- LinkedListElement next;
- LinkedListElement(Object obj)
- {
- element = obj;
- next = null;
- }
- LinkedListElement(Object obj, LinkedListElement nxt)
- {
- element = obj;
- next = nxt;
- }
- }
- public class Naloga3 {
- //public static LinkedList list;
- public static PrintWriter out;
- public static void main(String[] args) throws IOException{
- Scanner vhod = new Scanner(new File(args[0]));
- String a = "";
- out = new PrintWriter(args[1]);
- if(vhod.hasNext()) { a = vhod.next(); }
- int dol = a.length();
- String [] tab = a.split(",");
- LinkedList list = new LinkedList();
- list.addFirst(Integer.parseInt(tab[0]));
- for(int i = 1; i < tab.length; ++i) {
- list.addLast(Integer.parseInt(tab[i]));
- }
- //out.write(list.write());
- dol = vhod.nextInt();
- for(int i = 0; i < dol; i++) {
- a = vhod.next();
- tab = a.split(",");
- switch(tab[0]) {
- case "o":
- ohrani(tab[1], Integer.parseInt(tab[2]), list);
- break;
- case "z":
- zdruzi(tab[1], list);
- break;
- case "p":
- preslikaj(tab[1], Integer.parseInt(tab[2]), list);
- break;
- }
- //list.write();
- //izpisi(list);
- }
- //out.write("\n");
- vhod.close();
- out.close();
- }
- public static void preslikaj(String op, int val, LinkedList list) throws IOException {
- LinkedListElement el = list.first.next;
- if(op.contentEquals("*")) {
- while(el != null) {
- el.element = (Integer) (el.element) * val;
- el = el.next;
- }
- } else {
- while(el != null) {
- el.element = (Integer) (el.element) + val;
- el = el.next;
- }
- }
- izpisi(list);
- }
- public static void ohrani(String op, int val, LinkedList list) throws IOException {
- LinkedListElement el = list.first.next;
- int i = 0;
- if(op.contentEquals(">")) {
- while(el != null) {
- int vrednost = (Integer) (el.element);
- if(vrednost <= val) { list.deleteNth(i); --i; }
- ++i;
- el = el.next;
- //list.write();
- }
- } else if(op.contentEquals("<")) {
- while(el != null) {
- int vrednost = (Integer) (el.element);
- if(vrednost >= val) { list.deleteNth(i); --i; }
- ++i;
- el = el.next;
- //list.write();
- }
- } else if(op.contentEquals("=")){
- while(el != null) {
- int vrednost = (Integer) (el.element);
- if(vrednost != val) { list.deleteNth(i); --i; }
- ++i;
- el = el.next;
- //list.write();
- }
- }
- izpisi(list);
- }
- public static void zdruzi(String op, LinkedList list) throws IOException {
- int vrednost = -1;
- LinkedListElement el = list.first.next;
- if(el != null) {
- if(op.contentEquals("*")) {
- vrednost = 1;
- while(el != null) {
- vrednost *= (Integer) (el.element);
- el = el.next;
- }
- } else {
- vrednost = 0;
- while(el != null) {
- vrednost += (Integer) (el.element);
- el = el.next;
- }
- }
- }
- list.first.next = new LinkedListElement(vrednost);
- list.last = list.first;
- list.last.next = list.first.next;
- izpisi(list);
- }
- public static void izpisi(LinkedList list) throws IOException{
- LinkedListElement el;
- el = list.first.next;
- while(el != null) {
- out.write(""+(el.element));
- if(el.next != null) { out.write(","); }
- el = el.next;
- }
- out.write("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement