Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //2020 prv kolokvium - termin 1
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- class SLL<E> {
- private SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- 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;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + " ";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + " ";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- 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 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 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 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 SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> find(E o) {
- if (first != null) {
- SLLNode<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;
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- public class FindPairs {
- public static void findPairs(SLL<Integer> list) {
- SLLNode<Integer> temp1 = list.getFirst();
- SLLNode<Integer> temp2 = list.getFirst();
- while (temp1.succ != null) {
- temp2 = temp1.succ;
- while (true) {
- if(temp2==null) break;
- if (temp1.element % 2 == temp2.element % 2) {
- if (temp1.element % 2 == 1) {
- temp1.element -= temp2.element;
- list.delete(temp2);
- break;
- } else {
- temp1.element += temp2.element;
- list.delete(temp2);
- break;
- }
- }
- temp2=temp2.succ;
- }
- temp1=temp1.succ;
- if(temp1==null) break;
- }
- }
- public static void main (String[]args){
- SLL<Integer> list = new SLL<Integer>();
- Scanner input = new Scanner(System.in);
- int n = Integer.parseInt(input.nextLine());
- String[] broevi = input.nextLine().split("\\s+");
- for (int i = 0; i < n; i++) {
- list.insertLast(Integer.parseInt(broevi[i]));
- }
- findPairs(list);
- System.out.println(list);
- }
- }
- /*
- TestCase1
- 8
- 1 2 3 4 5 6 7 8
- -2 6 -2 14
- TestCase2
- 10
- 7 12 24 2 6 32 11 44 8 10
- -4 36 8 76 18
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement