Advertisement
korobushk

SLList

Apr 1st, 2021 (edited)
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. public class ControlFlow {
  2.    public static void main(String[] args) {
  3.         /* Creates a list of one Interger, namely 10 */
  4.         SLList L = new SLList();
  5.  
  6.         L.addLast(20);
  7.         L.addFirst(23);
  8.         L.addFirst(12);
  9.         L.displayList();
  10.     }
  11.  
  12. }
  13.  
  14.    
  15. public class SLList {
  16.     public static class IntNode {
  17.         public int item;
  18.         public IntNode next;
  19.  
  20.         public IntNode(int i, IntNode n) {
  21.             item = i;
  22.             next = n;
  23.         }
  24.         public void displayNode(){
  25.             System.out.println("{ "+ item + " } ");
  26.         }
  27.     }
  28.  
  29.     private IntNode sentinel;
  30.     private int size;
  31.  
  32.     public SLList() {
  33.         sentinel = new IntNode(63,null);
  34.  
  35.         size = 0;
  36.     }
  37.  
  38.     public SLList(int x) {
  39.         sentinel = new IntNode(63,null);
  40.         sentinel.next = new IntNode(x,null);
  41.  
  42.         size += 1;
  43.     }
  44.  
  45.     /**
  46.      * Adds an item to the front of the list.
  47.      */
  48.     public void addFirst(int x) {
  49.         sentinel.next=new IntNode(x,sentinel.next);
  50.         size += 1;
  51.  
  52.     }
  53.  
  54.     /**
  55.      * Retrieves the front item from the list.
  56.      */
  57.     public int getFirst() {
  58.         return sentinel.next.item;
  59.     }
  60.  
  61.     /**
  62.      * Adds an item to the end of the list.
  63.      */
  64.     public void addLast(int x) {
  65.         size += 1;
  66.         IntNode p = sentinel;
  67.  
  68.         while (p.next != null) {
  69.             p = p.next;
  70.         }
  71.         p.next = new IntNode(x, null);
  72.  
  73.  
  74.     }
  75.  
  76.     public int size3() {
  77.         return size;
  78.     }
  79.  
  80.     /**
  81.      * Returns the number of items in the list using recursion.
  82.      */
  83.     public int size() {
  84.         IntNode p = sentinel;
  85.         int count = 1;
  86.  
  87.         while (p.next != null) {
  88.             p = p.next;
  89.             count++;
  90.         }
  91.  
  92.         return count;
  93.     }
  94.  
  95.     public static int size2(IntNode p) {
  96.         if (p.next == null) {
  97.             return 1;
  98.         }
  99.         return 1 + size2(p.next);
  100.  
  101.     }
  102.  
  103.     public int size2() {
  104.         return size2(sentinel);
  105.  
  106.     }
  107.  
  108.     public void displayList(){
  109.         System.out.println("List (first --> last) ");
  110.         IntNode current = sentinel;
  111.         while(current != null){
  112.             current.displayNode();
  113.             current = current.next;
  114.         }
  115.         System.out.println();
  116.     }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement