Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Дадена е еднострана/двострана листа која содржи едноцифрени броеви и меѓу нив е и -1. Да се модифицира листата на тој начин што ќе се форматираат броеви од тие цифри се додека не се стигне до бројот -1. Кога ќе се стигне до тој број се прeкинува и се брише -1 и се продолжува во форматирање на броеви од наредните цифри (постапката се повторува).
- Влез:
- 14
- 1 2 3 -1 5 -1 6 7 4 8 9 -1 9 8
- 123 5 67489 98
- 19
- 1 2 3 -1 5 6 -1 3 4 -1 7 4 8 9 -1 9 -1 8 -1
- Излез:
- 123 56 34 7489 9 8
- */
- //CODE
- 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 = "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;
- }
- 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;
- }
- }
- public class CombineNumbers {
- private static void changeList(SLL<Integer> list, int n) {
- SLLNode<Integer> current = list.getFirst();
- SLLNode<Integer> start = list.getFirst();
- int count = 0, total = 0;
- while (current.succ != null && count < n) {
- if (current.element != -1) {
- total = total * 10 + current.element;
- }
- if (current.element == -1 && count < n - 1) {
- while (start != current) {
- list.delete(start);
- start = start.succ;
- }
- start = current.succ;
- list.insertBefore(total, current);
- list.delete(current);
- total = 0;
- }
- ++count;
- current = current.succ;
- }
- if (current.element == -1)
- list.delete(current);
- System.out.println(list);
- }
- public static void main(String[] args) {
- SLL<Integer> list = new SLL<Integer>();
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- for (int i = 0; i < n; i++) {
- list.insertLast(input.nextInt());
- }
- changeList(list, n);
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement