Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hsa.Stdin;
- class LinkedListAssignment
- {
- public static void main (String [] args)
- {
- // Create a List for this course
- List CompSci = new List ();
- System.out.print ("Enter a name to add to the class list: ");
- String input = Stdin.readString ();
- //Continuously add names to list until user types "zzz" or "ZZZ"
- while (input.compareTo ("zzz") != 0 && input.compareTo ("ZZZ") != 0) //or compare to
- {
- CompSci.insert (input);
- System.out.print ("Enter a name to add to the class list: ");
- input = Stdin.readString ();
- }
- // print list
- CompSci.printList ();
- }
- }
- class List
- {
- // This class contains methods that manipulate a linked list
- // whose nodes contain strings in their name fields
- // List object contains only a head whose purpose is to
- // point to the first object in the list
- Student head;
- public List ()
- {
- // a basic constructor
- head = null;
- }
- public void printList ()
- {
- // print the contents of a list
- Student temp = head;
- while (temp != null)
- {
- System.out.println (temp.name);
- temp = temp.link;
- }
- }
- public void insert (String s)
- {
- // insert a new node containing s in its name field into a list whose nodes
- // are kept in increasing lexicographic order
- Student prev = null;
- Student temp = head;
- Student newStudent = new Student (n, null);
- if (head == null)
- {
- head = newstudent;
- }
- else
- {
- while (temp.link != null && temp.name.compareTo (newStudent.name) > 0)
- {
- previous = temp;
- temp = temp.ink;
- }
- if
- {
- prev != null
- }
- {
- prev.link = newStudent;
- }
- newStudent.link = temp;
- {
- }
- class Student
- {
- // an inner class for nodes of a list
- String name;
- Student link;
- public Student (String word, Student next)
- {
- // a simple constructor
- name = word;
- link = next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement