Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2021 juni - termin 2
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- 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();
- }
- }
- 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 = "";
- 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;
- }
- }
- 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 {
- return null;
- }
- } else {
- 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;
- }
- }
- return first;
- }
- }
- public class DeleteEvenDuplicates {
- public static void deleteEven(SLL<Integer> list1) {
- //todo: enter code here
- SLLNode<Integer> i= list1.getFirst();
- SLLNode<Integer> j= list1.getFirst();
- int counter=0;
- while(i!=null){
- int broj= i.element;
- j=list1.getFirst();
- counter=0;
- while(j!=null){
- if(j.element==broj) counter++;
- j=j.succ;
- }
- if(counter>0&&counter%2==0){
- j= list1.getFirst();
- while(j!=null){
- if(j.element==broj){
- list1.delete(j);
- }
- j=j.succ;
- }
- }
- i=i.succ;
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- SLL<Integer> list1 = new SLL<Integer>();
- //todo: enter code here
- String[] niza= scanner.nextLine().split("\\s+");
- for(int i=0;i<n;i++){
- list1.insertLast(Integer.parseInt(niza[i]));
- }
- deleteEven(list1);
- //todo: enter code here
- System.out.println(list1.toString());
- }
- }
- /*
- Test
- 9
- 1 10 2 3 5 2 10 3 3
- 1 3 5 3 3
- Test
- 10
- 15 3 15 2 4 9 3 15 8 15
- 2 4 9 8
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement