Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mirror;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.IOException;
- 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> {
- 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()
- {
- SLLNode<E> current = first;
- String s = new String();
- while(current!=null)
- {
- s = s + current.element + "->";
- current = current.succ;
- }
- return s;
- }
- 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 Mirror {
- public static void mirror(SLL<Integer> lista)
- {
- SLLNode<Integer> current = lista.first;
- SLLNode<Integer> sled = null;
- SLLNode<Integer> pred = null;
- while(current != null)
- {
- sled = current.succ;
- current.succ = pred;
- pred = current;
- current = sled;
- }
- lista.first = pred;
- }
- /* public static String prevrti_Opseg(SLL<Integer> lista,int n, int m)
- {
- SLLNode<Integer> current = lista.first;
- SLLNode<Integer> N = null;
- SLLNode<Integer> M = null;
- int i = 1;
- while(current != null)
- {
- if( i == n)
- {
- N = current;
- }
- else if ( i == m)
- {
- M = current;
- }
- i++;
- current = current.succ;
- }
- return "N e: " + N.element + " \n M e: " + M.element;
- }
- */
- public static void moment(SLL<Integer> lista, int x)
- {
- SLLNode<Integer> current = lista.first;
- SLLNode<Integer> p = current;
- SLLNode<Integer> rezerva = null;
- int flag = 0, i = 1;
- while(current.succ != null)
- {
- if(current.succ.element == x && flag == 0)
- {
- rezerva = current.succ;
- SLLNode<Integer> next = current.succ;
- current.succ = next.succ;
- flag = 1;
- i++;
- }
- else
- {
- current = current.succ;
- if(flag == 0)
- {
- i++;
- }
- }
- }
- if(flag == 0)
- {
- System.out.println("-1");
- }
- // INSERT FIRST THE ELEMENT EQUALS TO X
- /*
- SLLNode<Integer> ins = new SLLNode<Integer>(rezerva.element, lista.first);
- lista.first = ins;
- System.out.println("pozicija: "+ i);
- */
- // ===> DA GO STAVI NA KRAJ
- //INSERT LAST THE LEMENT EQUALS TO X
- SLLNode<Integer> move = lista.first;
- while(move.succ != null)
- {
- move = move.succ;
- }
- SLLNode<Integer> ins = new SLLNode<Integer>(rezerva.element, null);
- move.succ = ins;
- System.out.println("move e: " + move.element);
- // move.succ = rezerva;
- System.out.println("move.succ e: " + move.succ.element);
- // move = rezerva;
- }
- public static void removeFirst(SLL<Integer> lista)
- {
- SLLNode<Integer> current = lista.first;
- current = current.succ;
- lista.first = current;
- }
- public static void main(String[] args) throws IOException {
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- SLL<Integer> lista = new SLL();
- int broevi, N, M;
- broevi = Integer.parseInt(input.readLine());
- N = Integer.parseInt(input.readLine());
- //M = Integer.parseInt(input.readLine());
- System.out.println("Vnesi: ");
- String line = input.readLine();
- String[] pom = line.split(" ");
- for(int i = 0; i < broevi; i++)
- {
- lista.insertLast(Integer.parseInt(pom[i]));
- }
- // prevrti_Opseg(lista, 3, 5);
- // ====> FUNKCIJAVA TREBA DA GO NAJDE ELEMENTOT I DA GO STAVI NA POCETOK/KRAJOT OD LISTATA
- moment(lista, N);
- System.out.println(lista);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement