Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.NoSuchElementException;
- 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.element + "->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp.element + "->";
- }
- } 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(int indeks) {
- SLLNode<E> pok1 = null;
- if (first != null) {
- SLLNode<E> tmp = first;
- int brojac = 1;
- while (brojac <= indeks && tmp != null) {
- if (brojac == indeks) {
- pok1 = tmp;
- return pok1;
- }
- tmp = tmp.succ;
- brojac++;
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return pok1;
- }
- public void izmeni(int m, int n) {
- SLLNode<E> pok1 = this.find(m);
- SLLNode<E> pok2 = this.find(n);
- int i = 0;
- while (this.find(n).element != pok1.element) {
- this.insertBefore(this.find(n).element, this.find(m + i));
- delete(this.find(n + 1));
- i++;
- }
- System.out.println(this.toString());
- }
- }
- public class Reverse {
- public static void main(String[] args)throws IOException{
- BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
- int n=Integer.parseInt(stdin.readLine());
- String[] pomniza=stdin.readLine().split(" ");
- SLL<Integer>lista=new SLL<>();
- for (int i=0;i<n;i++){
- lista.insertLast(Integer.parseInt(pomniza[i]));
- }
- String[] nm=stdin.readLine().split(" ");
- lista.izmeni(Integer.parseInt(nm[0]),Integer.parseInt(nm[1]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement