Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Дадена е еднострано поврзана листа SLL чии што јазли содржат по еден природен број. Да се трансформира листата така што на непарните позиции (сметајќи од лево на десно, со почеток 0) ќе се постави збирот од сите елементи кои се наоѓаат помеѓу позициите M и N.
- Влез: Во првиот ред од влезот е даден бројот на елементи во листата, а потоа самите елементи на листата секој во нов ред. По внесувањето на листата
- во посебен ред е даден бројот М, а во наредниот бројот N.
- Пример:
- Влез:
- 8 //број на елементи на листата
- 1 //елементите на листата ...
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 3 //бројот M
- 6 //бројот N
- Излез:
- 1 22 3 22 5 22 7 22
- Објаснување: M=3, N=6
- Елементите кои се наoѓаат во интервалот од 3-от до 6-от елемент се : 4, 5, 6, 7, нивниот збир е: 4 + 5+6+7 =22
- Со замена на јазлите во листата се добива 1 22 3 22 5 22 7 22
- Забелешка: При реализација на задачата не е дозволено менување на вредностите на јазлите во листата. Манипулациите се вршат врз цели јазли. На располагање од структурите имате само ЕДНА еднострано поврзана листа, SLL.
- Име на класа (Јава): SumList
- Sample input
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 3
- 6
- Sample output
- 1 22 3 22 5 22 7 22
- */
- //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;
- }
- // 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;
- }
- }
- public class SumList {
- private static void SumList(SLL<Integer> list, int m, int n) {
- SLLNode<Integer> current = list.getFirst();
- int index = 0, sum = 0;
- while (current != null) {
- if (index >= m && index <= n && current != null) {
- sum += current.element;
- }
- current = current.succ;
- ++index;
- }
- current = list.getFirst();
- index = 0;
- while (current != null) {
- if (index % 2 == 1 && current != null) {
- list.insertAfter(sum, current);
- list.delete(current);
- current = current.succ;
- }
- ++index;
- current = current.succ;
- }
- System.out.println(list);
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- SLL<Integer> list = new SLL<Integer>();
- int size = input.nextInt();
- for (int i = 0; i < size; i++)
- list.insertLast(input.nextInt());
- int m = input.nextInt();
- int n = input.nextInt();
- SumList(list, m, n);
- input.close();
- }
- }
Add Comment
Please, Sign In to add comment