Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Подлисти
- Да се избришат сите подлисти на една листа во друга листа
- input:
- 20
- 7 6 7 6 5 1 3 7 6 7 6 7 6 5 4 7 6 7 6 5
- 5
- 7 6 7 6 5
- output:
- 1 3 7 6 4
- */
- //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 Podlisti {
- private static void brisiPodlisata(SLL<Integer> list, SLL<Integer> podlista) {
- SLLNode<Integer> inList = list.getFirst();
- SLLNode<Integer> inPodlist = podlista.getFirst();
- SLLNode<Integer> tmp = null;
- int count = 0, firstPos = 0, lastPos = 0, broi = 0;
- boolean brisi = false;
- while (inList != null) {
- if (inList.element == inPodlist.element) {
- firstPos = count;
- lastPos = count;
- tmp = inList;
- while (tmp != null && inPodlist != null) {
- if (inPodlist.element != tmp.element) {
- firstPos = 0;
- lastPos = 0;
- brisi = false;
- break;
- } else
- brisi = true;
- ++lastPos;
- inPodlist = inPodlist.succ;
- tmp = tmp.succ;
- broi = firstPos;
- }
- inPodlist = podlista.getFirst();
- if (brisi) {
- while (inList != tmp && (broi >= firstPos && broi < lastPos)) {
- list.delete(inList);
- inList = inList.succ;
- ++count;
- ++broi;
- }
- brisi = false;
- }
- }
- if (inList == null)
- break;
- ++count;
- inList = inList.succ;
- }
- System.out.println(list);
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- SLL<Integer> list = new SLL<Integer>();
- SLL<Integer> podlista = new SLL<Integer>();
- int n = input.nextInt();
- for (int i = 0; i < n; i++)
- list.insertLast(input.nextInt());
- int m = input.nextInt();
- for (int i = 0; i < m; i++)
- podlista.insertLast(input.nextInt());
- brisiPodlisata(list, podlista);
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement