Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2023 prv kolokvium
- // difficulty level: HARD AF;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- public class FerRaspredelba
- {
- public static void raspredeli(SLL<Integer> original,SLL<SLL<Integer>> list_of_lists) {
- int length= original.length();
- int kolicnik= 1+length/10;
- int ostatok= length%10;
- int front=0,rear=0;
- SLLNode<Integer> temp= original.getFirst();
- if(ostatok!=0){
- while(rear<ostatok) {
- SLL<Integer> temp3= new SLL<Integer>();
- while (front < kolicnik) {
- temp3.insertLast(temp.element);
- front++;
- temp = temp.succ;
- if(front==kolicnik){
- list_of_lists.insertLast(temp3);
- break;
- }
- }
- rear++;
- front = 0;
- }
- front=0;
- rear=0;
- while(rear<10-ostatok){
- SLL<Integer> temp3= new SLL<Integer>();
- while (front < kolicnik-1) {
- temp3.insertLast(temp.element);
- front++;
- temp = temp.succ;
- if(front==kolicnik-1){
- list_of_lists.insertLast(temp3);
- break;
- }
- }
- rear++;
- front = 0;
- }
- }
- else{
- kolicnik= length/10;
- while(rear<10){
- SLL<Integer> temp3= new SLL<Integer>();
- while (front < kolicnik) {
- temp3.insertLast(temp.element);
- front++;
- temp = temp.succ;
- if(front==kolicnik){
- list_of_lists.insertLast(temp3);
- break;
- }
- }
- rear++;
- front = 0;
- }
- }
- }
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- SLL<SLL<Integer>> list_of_lists= new SLL<SLL<Integer>>();
- SLL<Integer> original_list= new SLL<Integer>();
- int n= Integer.parseInt(input.nextLine());
- String[] broevi = input.nextLine().split("\\s+");
- for(int i=0;i<n;i++)
- {
- original_list.insertLast(Integer.parseInt(broevi[i]));
- }
- raspredeli(original_list,list_of_lists);
- SLLNode<SLL<Integer>> temp= list_of_lists.getFirst();
- SLLNode<Integer> temp2= temp.element.getFirst();
- System.out.print("[");
- while(temp!=null){
- temp2= temp.element.getFirst();
- while(temp2!=null){
- if(temp.succ==null&&temp2.succ==null){
- System.out.print(temp2.element);
- break;
- }
- if(temp2.succ==null){
- System.out.print(temp2.element+", ");
- break;
- }
- System.out.print(temp2.element+" -> ");
- temp2=temp2.succ;
- }
- temp=temp.succ;
- }
- System.out.print("]");
- }
- }
- 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 = "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;
- }
- }
- /*
- Vlez:
- 14
- 1 5 2 3 0 6 4 3 7 9 1 4 6 8
- Izlez:
- [1 -> 5, 2 -> 3, 0 -> 6, 4 -> 3, 7, 9, 1, 4, 6, 8]
- Vlez:
- 20
- 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
- Izlez:
- [1 -> 2, 3 -> 4, 5 -> 6, 7 -> 8, 9 -> 10, 10 -> 9, 8 -> 7, 6 -> 5, 4 -> 3, 2 -> 1]
- Vlez:
- 10
- 1 6 0 5 1 6 0 5 1 6
- Izlez:
- [1, 6, 0, 5, 1, 6, 0, 5, 1, 6]
- Vlez:
- 32
- 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320
- Izlez:
- [10 -> 20 -> 30 -> 40, 50 -> 60 -> 70 -> 80, 90 -> 100 -> 110, 120 -> 130 -> 140, 150 -> 160 -> 170, 180 -> 190 -> 200, 210 -> 220 -> 230, 240 -> 250 -> 260, 270 -> 280 -> 290, 300 -> 310 -> 320]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement