Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SLLNode<E>
- {
- E element;
- SLLNode<E> succ;
- public SLLNode(E element, SLLNode<E> succ) {
- this.element = element;
- this.succ = succ;
- }
- }
- class SLL<E>
- {
- SLLNode<E> first;
- public SLL()
- {
- first = null;
- }
- public void insertFirst(E element)
- {
- SLLNode<E> nov = new SLLNode(element,first);
- first = nov;
- }
- public void insertLast(E element)
- {
- if(first==null)
- {
- insertFirst(element);
- }
- else
- {
- SLLNode<E> nov = new SLLNode(element,null);
- SLLNode<E> dvizi = first;
- while(dvizi.succ!=null)
- {
- dvizi = dvizi.succ;
- }
- dvizi.succ = nov;
- }
- }
- @Override
- public String toString()
- {
- String s = new String();
- SLLNode<E> dvizi = first;
- while(dvizi!=null)
- {
- s = s + dvizi.element + " ";
- dvizi = dvizi.succ;
- }
- return s;
- }
- }
- public class Test
- {
- public static void main(String[] args) {
- /*SLLNode<Integer> jazol1 = new SLLNode(5,null);
- SLLNode<String> jazol2 = new SLLNode("Dragan",null);
- SLLNode<Integer> jazol3 = new SLLNode(32,jazol1);*/
- SLL<Integer> lista = new SLL();
- lista.insertFirst(21);
- lista.insertFirst(1);
- lista.insertLast(4);
- lista.insertLast(12);
- System.out.println(lista);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement