Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**(1 Термин):
- Дадена еднострано поврзана листа, се бара N пати да се избрише средината. Ако листата е со парен број елементи од 2та средишни елементи се брише помалиот, а ако се исти се брише првиот.
- Влез: првата линија број на елементи на листата,вториот ред елементите на листата и во третиот ред број колку пати да се избрише средината
- Sample input: Sample output:
- 5 1 4 5
- 1 2 3 4 5
- 2
- */
- // 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 setFirst(SLLNode<E> first) {
- this.first = first;
- }
- 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 BrisiSredina {
- public static void removeMiddleNode(SLL<Integer> list, int timesDeleted) {
- while (timesDeleted > 0) {
- SLLNode<Integer> current = list.getFirst();
- int counter = 1;
- if (timesDeleted >= list.length()) {
- list.setFirst(null);
- break;
- } else {
- while (current != null) {
- int listLen = list.length();
- if (listLen % 2 != 0) {
- if (counter == (listLen / 2) + 1) {
- list.delete(current);
- }
- } else if (listLen % 2 == 0) {
- if (counter == listLen / 2) {
- if (current.element <= current.succ.element) {
- list.delete(current);
- } else if (current.element > current.succ.element) {
- list.delete(current.succ);
- }
- }
- }
- ++counter;
- current = current.succ;
- }
- --timesDeleted;
- }
- }
- System.out.println(list);
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- SLL<Integer> list = new SLL<Integer>();
- int n = input.nextInt();
- for (int i = 0; i < n; ++i)
- list.insertLast(input.nextInt());
- removeMiddleNode(list, input.nextInt());
- input.close();
- }
- }
Add Comment
Please, Sign In to add comment