

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


public class Node { // Data part private String name; // Link to next private Node next; // Constructors Node() {name=""; next=null;} Node(String n) {name=n; next=null;} Node(String n, Node p){name=n; next=p;} // Readers public String getName(){return name;} public Node getNext(){return next;} // Writers public void setName(String n){name=n;} public void setNext(Node p) {next=p;} public void setNode(String n, Node p) {name=n; next=p;} }
public class LList { // Reference to head of the list. private Node head; // List size. private int size; // Constructor LList(){head=null; size=0;} // Add new node with specified name to head of the list. public void addFirst(String s) { head = new Node(s,head); size++;} // Display list public void display() { System.out.println("List contains "+size+" nodes."); Node p = head; while (p != null) { System.out.println(p.getName()); p = p.getNext(); // Move to next node. } } }
public class ListEx { public static void main(String[] args) { // Create empty list. LList list=new LList(); // Add three nodes; list.addFirst("Larry"); list.addFirst("Curly"); list.addFirst("Moe"); // Display what we've created. list.display(); } }