Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- 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();
- }
- public void print() {
- SLLNode<E> ptr = this;
- while (ptr.succ != null) {
- System.out.println(ptr.element + " ");
- ptr = ptr.succ;
- }
- System.out.println(ptr.element);
- }
- }
- 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 Iterator<E> iterator() {
- // Return an iterator that visits all elements of this list, in left-to-right order.
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null) {
- throw new NoSuchElementException();
- }
- E nextElem = place.element;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- //Not implemented
- }
- }
- public void mirror() {
- if (first != null) {
- //m=nextsucc, p=tmp,q=next
- 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 void merge(SLL<E> in) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- }
- tmp.succ = in.getFirst();
- } else {
- first = in.getFirst();
- }
- }
- }
- public class fvgd {
- public static void smeni(SLL<Integer> lista,int x){
- SLLNode<Integer> dvizi=lista.getFirst();
- SLLNode<Integer> pomali=null;
- SLLNode<Integer> pogolemi=null;
- int f1=1;
- int f2=1;
- while(dvizi!=null) {
- if(dvizi.element<x && f1==1) {
- pomali=dvizi;
- f1=0;
- }
- else if(dvizi.element>=x && f2==1) {
- pogolemi=dvizi;
- f2=0;
- }
- if(f1==0 && f2==0) {
- break;
- }
- System.out.println("1");
- dvizi=dvizi.succ;
- }
- dvizi=lista.getFirst();
- SLLNode<Integer> prvoPomal=pomali;
- SLLNode<Integer> prvoPogolem=pogolemi;
- while(dvizi.succ!=null) {
- System.out.println("element:" + dvizi.element);
- if(dvizi.element<x) {
- System.out.println("islov1");
- pomali.succ=dvizi;
- pomali=pomali.succ;
- System.out.println("pomali: "+pomali.element);
- System.out.println("dvizi: "+dvizi.element);
- System.out.println("divizi.succ"+dvizi.succ.element);
- System.out.println("divizi.succ.succ"+dvizi.succ.succ.element);
- }
- else {
- System.out.println("uslov2");
- pogolemi.succ=dvizi;
- pogolemi=pogolemi.succ;
- }
- System.out.println("2");
- dvizi=dvizi.succ;
- System.out.println("zavrsuvam so ciklus");
- }
- if(prvoPogolem==null) {
- pomali.succ=null;
- dvizi=prvoPomal;
- while(dvizi!=null) {
- if(dvizi.succ==null) {
- System.out.println(dvizi.element);
- break;
- }
- System.out.println(dvizi.element+"->");
- System.out.println("3");
- dvizi=dvizi.succ;
- }
- }
- else if(prvoPomal==null) {
- pogolemi.succ=null;
- dvizi=prvoPogolem;
- while(dvizi!=null) {
- if(dvizi.succ==null) {
- System.out.println(dvizi.element);
- break;
- }
- System.out.println(dvizi.element+"->");
- System.out.println("4");
- dvizi=dvizi.succ;
- }
- }
- else {
- pomali.succ=prvoPogolem;
- pogolemi.succ=null;
- dvizi=prvoPomal;
- while(dvizi!=null) {
- if(dvizi.succ==null) {
- System.out.println(dvizi.element);
- break;
- }
- System.out.println(dvizi.element+"->");
- System.out.println("5");
- dvizi=dvizi.succ;
- }
- }}
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- SLL<Integer> lista = new SLL<Integer>();
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- String line = br.readLine();
- String[] pom = line.split(" ");
- for (int i = 0; i < N; i++) {
- lista.insertLast(Integer.parseInt(pom[i]));
- }
- int x = Integer.parseInt(br.readLine());
- SLLNode<Integer> prv=lista.getFirst();
- if(lista.length()==1){
- System.out.print(prv.element);
- }
- else{
- smeni(lista,x);
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement