Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ktiElementSaKrajaListe;
- public class JednostrukaLista<T> {
- private class Cvor<T> {
- private T info;
- private Cvor<T> next;
- public Cvor(T info,Cvor<T> next) {
- super();
- this.info=info;
- this.next=next;
- }
- public T getInfo() {
- return info;
- }
- public void setInfo(T info) {
- this.info = info;
- }
- public Cvor<T> getNext() {
- return next;
- }
- public void setNext(Cvor<T> next) {
- this.next = next;
- }
- }
- private Cvor<T> head;
- public Cvor<T> getHead() {
- return head;
- }
- public void setHead(Cvor<T> head) {
- this.head = head;
- }
- public JednostrukaLista() {
- head=null;
- }
- public boolean dodajNaPocetak(T info) {
- if(info == null) {
- return false;
- }
- head = new Cvor<T>(info,head);
- return true;
- }
- public boolean dodajNaKraj(T info) {
- if(info == null) {
- return false;
- }
- if(head == null) {
- return dodajNaPocetak(info);
- }
- Cvor<T> tmp = head;
- while(tmp.next != null) {
- tmp = tmp.next;
- }
- tmp.next = new Cvor<T>(info, null);
- return true;
- }
- public boolean dodajPre(T info,T key) {
- if(info == null) {
- return false;
- }
- Cvor<T> tmp = head;
- if(head.info.equals(key)) {
- return dodajNaPocetak(info);
- }
- while(tmp.next != null && !tmp.next.info.equals(key)) {
- tmp = tmp.next;
- }
- if(tmp.next == null) {
- return false;
- }
- tmp.next =new Cvor<T>(info,tmp.next);
- return true;
- }
- public boolean dodajPosle(T info,T key) {
- if(info == null) {
- return false;
- }
- Cvor<T> tmp = head;
- while(tmp != null && !tmp.info.equals(key)) {
- tmp = tmp.next;
- }
- if(tmp == null) {
- return false;
- }
- tmp.next = new Cvor<T>(info, tmp.next);
- return true;
- }
- public void ispisiListu() {
- if(head == null) {
- System.out.println("Lista je prazna");
- return;
- }
- Cvor<T> tmp = head;
- while(tmp != null) {
- System.out.print(tmp.info);
- if(tmp.next != null) {
- System.out.print(" -> ");
- }
- else {
- System.out.println();
- }
- tmp = tmp.next;
- }
- }
- public boolean Izbrisi(T key) {
- if(head == null || key == null) {
- return false;
- }
- if(head.info.equals(key)) {
- head = head.next;
- }
- Cvor<T> tmp = head;
- while(tmp.next != null && tmp.next.info.equals(key)) {
- tmp = tmp.next;
- }
- if(tmp.next == null)
- {
- return false;
- }
- tmp.next = tmp.next.next;
- return true;
- }
- public void Invertuj() {
- if(head == null || head.next == null) {
- return ;
- }
- Cvor<T> first = null;
- Cvor<T> second = head;
- while(second != null) {
- Cvor<T> third = second.next;
- second.next = first;
- first = second;
- second = third;
- }
- head = first;
- }
- public int ktiElementSaKrajaListe(JednostrukaLista<Integer> lista, int k) {
- Cvor<Integer> tmp = lista.getHead();
- }
- public static void main(String[] args) {
- JednostrukaLista<Integer> lista = new JednostrukaLista<>();
- lista.dodajNaKraj(5);
- lista.dodajNaKraj(4);
- lista.dodajNaKraj(2);
- lista.dodajNaKraj(1);
- lista.dodajNaKraj(8);
- lista.dodajNaKraj(6);
- lista.dodajNaKraj(10);
- lista.ispisiListu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement