Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //2019 prv kolokvium - termin 2
- import java.util.Scanner;
- class DLLNode<E> {
- protected E element;
- protected DLLNode<E> pred, succ;
- public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
- this.element = elem;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public DLLNode<E> find(E o) {
- if (first != null) {
- DLLNode<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;
- }
- public void insertFirst(E o) {
- DLLNode<E> ins = new DLLNode<E>(o, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(E o) {
- if (first == null)
- insertFirst(o);
- else {
- DLLNode<E> ins = new DLLNode<E>(o, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(E o, DLLNode<E> after) {
- if(after==last){
- insertLast(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(E o, DLLNode<E> before) {
- if(before == first){
- insertFirst(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- if (first != null) first.pred = null;
- if (first == null)
- last = null;
- return tmp.element;
- } else
- return null;
- }
- public E deleteLast() {
- if (first != null) {
- if (first.succ == null)
- return deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.element;
- }
- }
- // else throw Exception
- return null;
- }
- public E delete(DLLNode<E> node) {
- if(node==first){
- deleteFirst();
- return node.element;
- }
- if(node==last){
- deleteLast();
- return node.element;
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- return node.element;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- if(tmp.succ==null){
- ret+=tmp;
- break;
- }
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public String toStringR() {
- String ret = new String();
- if (last != null) {
- DLLNode<E> tmp = last;
- ret += tmp + "<->";
- while (tmp.pred != null) {
- tmp = tmp.pred;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- public void izvadiDupliIPrebroj(){
- }
- }
- public class mergeOddEven {
- public static void merge_odd_even(DLL<Integer> list, DLL<Integer> list1, DLL<Integer> list2){
- DLLNode<Integer> temp1= list1.getFirst();
- DLLNode<Integer> temp2= list2.getFirst();
- while(temp1!=null&&temp2!=null){
- if(temp1.element%2==0){
- temp1=temp1.succ;
- continue;
- }
- if(temp2.element%2==1){
- temp2=temp2.succ;
- continue;
- }
- if(temp1.element<temp2.element){
- list.insertLast(temp1.element);
- temp1=temp1.succ;
- }
- else{
- list.insertLast(temp2.element);
- temp2=temp2.succ;
- }
- }
- while(temp1!=null){
- if(temp1.element%2==1){
- list.insertLast(temp1.element);
- }
- temp1=temp1.succ;
- }
- while(temp2!=null){
- if(temp2.element%2==0){
- list.insertLast(temp2.element);
- }
- temp2=temp2.succ;
- }
- }
- public static void main(String[] args) {
- Scanner input= new Scanner(System.in);
- String[] dolzina= input.nextLine().split("\\s+");
- int n= Integer.parseInt(dolzina[0]);
- int m= Integer.parseInt(dolzina[1]);
- DLL<Integer> list1= new DLL<>();
- DLL<Integer> list2= new DLL<>();
- String[] broevi_list1= input.nextLine().split("\\s+");
- for(int i=0;i<n;i++){
- list1.insertLast(Integer.parseInt(broevi_list1[i]));
- }
- String[] broevi_list2= input.nextLine().split("\\s+");
- for(int i=0;i<m;i++){
- list2.insertLast(Integer.parseInt(broevi_list2[i]));
- }
- DLL<Integer> list= new DLL<>();
- merge_odd_even(list,list1,list2);
- System.out.println(list.toString());
- // DLLNode<Integer> temp= list.getLast();
- // while(temp!=null){
- // if(temp.pred==null){
- // System.out.print(temp.element);
- // break;
- // }
- // System.out.print(temp.element+"<->");
- // temp=temp.pred;
- // }
- }
- }
- /*
- TestCase1
- 6 6
- 5 8 9 12 24 101
- 1 2 6 9 28 303
- 2<->5<->6<->9<->28<->101
- TestCase2
- 5 5
- 2 4 7 9 11
- 1 4 12 16 21
- 4<->7<->9<->11<->12<->16
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement