Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // septemvri 2022
- 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;
- if(tmp.succ==null){
- ret+= tmp;
- break;
- }
- 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 listTransformation {
- public static void main(String[] args) {
- Scanner input= new Scanner(System.in);
- int n= Integer.parseInt(input.nextLine());
- SLL<Integer> lista= new SLL<Integer>();
- String[] broevi= input.nextLine().split("\\s+");
- for(int i=0;i<n;i++){
- lista.insertLast(Integer.parseInt(broevi[i]));
- }
- SLL<Integer> lista_parni= new SLL<Integer>();
- SLL<Integer> lista_neparni= new SLL<Integer>();
- SLLNode<Integer> temp= lista.getFirst();
- while(temp!=null){
- if(temp.element%2==0){
- lista_parni.insertLast(temp.element);
- lista.delete(temp);
- }
- else{
- lista_neparni.insertLast(temp.element);
- lista.delete(temp);
- }
- temp=temp.succ;
- }
- System.out.println(lista_neparni.toString());
- System.out.println(lista_parni.toString());
- }
- }
- /*
- TestCase
- 14
- 1 5 2 3 0 6 4 3 7 9 1 4 6 8
- 1 -> 5 -> 3 -> 3 -> 7 -> 9 -> 1
- 2 -> 0 -> 6 -> 4 -> 4 -> 6 -> 8
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement