Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package examples;
- import java.io.IOException;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- 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 Examples {
- public static void prevrti(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;
- }
- /* ====> ZADACATA <===== */
- public static int example4(SLL<Integer> lista, int x)
- {
- SLLNode<Integer> current = lista.first;
- if (current.element == x)
- {
- return 1;
- }
- SLLNode<Integer> oldCurrent = lista.first;
- SLLNode<Integer> rezerva = null;
- int pos = 1, flag = 0;
- while (current != null)
- {
- SLLNode<Integer> sled = current.succ;
- if(sled.succ != null) // spravuvanje so NullPointerExcepton
- {
- if (sled.element == x)
- {
- rezerva = sled;
- current.succ = sled.succ;
- lista.first = rezerva;
- rezerva.succ = oldCurrent;
- pos++;
- return pos;
- }
- else
- {
- flag = 0;
- current = current.succ;
- pos++;
- }
- }
- else
- {
- break;
- }
- }
- if(flag == 0)
- {
- return -1;
- }
- else
- {
- return pos;
- }
- }
- 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]));
- }
- int pos = example4(lista, N);
- System.out.println(lista);
- System.out.println(pos);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement