Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //juni 2023
- 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() {
- 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 void mirror() {
- if (first != null) {
- SLLNode<E> tmp = first;
- SLLNode<E> newsucc = null;
- SLLNode<E> next;
- while(tmp != null) {
- next = tmp.succ;
- tmp.succ = newsucc;
- newsucc = tmp;
- tmp = next;
- }
- first = newsucc;
- }
- }
- }
- public class keepMdeleteN {
- // TODO: implement function
- public static void keepDelete(SLL<Integer> list, int m, int n) {
- SLLNode<Integer> temp= list.getFirst();
- int counter_m=0;
- int counter_n=0;
- while(temp!=null){
- while(counter_m<m){
- if(temp.succ==null){
- temp=temp.succ;
- return;
- }
- temp=temp.succ;
- counter_m++;
- //System.out.println(list);
- continue;
- }
- counter_m=0;
- while(counter_n<n){
- if(temp.succ==null){
- list.delete(temp);
- temp=temp.succ;
- return;
- }
- list.delete(temp);
- temp=temp.succ;
- counter_n++;
- //System.out.println(list);
- continue;
- }
- counter_n=0;
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- int numElements;
- SLL<Integer> list1 = new SLL<Integer>();
- numElements = scan.nextInt();
- for(int i = 0; i<numElements; i++) {
- list1.insertLast(scan.nextInt());
- }
- int m = scan.nextInt();
- int n = scan.nextInt();
- keepDelete(list1,m,n);
- System.out.println(list1);
- }
- }
- /*
- 8
- 1 2 3 4 5 6 7 8
- 2 2
- 1->2->5->6
- 8
- 5 10 15 20 25 30 35 40
- 4 3
- 5->10->15->20->40
- 9
- 3 6 9 12 15 18 21 24 27
- 2 3
- 3->6->18->21
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement