Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import hsa.Stdin;
  2. class LinkedListAssignment
  3. {
  4. public static void main (String [] args)
  5. {
  6. // Create a List for this course
  7. List CompSci = new List ();
  8.  
  9.  
  10. System.out.print ("Enter a name to add to the class list: ");
  11. String input = Stdin.readString ();
  12.  
  13. //Continuously add names to list until user types "zzz" or "ZZZ"
  14. while (input.compareTo ("zzz") != 0 && input.compareTo ("ZZZ") != 0) //or compare to
  15. {
  16. CompSci.insert (input);
  17. System.out.print ("Enter a name to add to the class list: ");
  18. input = Stdin.readString ();
  19.  
  20. }
  21.  
  22. // print list
  23. CompSci.printList ();
  24.  
  25. }
  26. }
  27.  
  28. class List
  29. {
  30. // This class contains methods that manipulate a linked list
  31. // whose nodes contain strings in their name fields
  32.  
  33.  
  34.  
  35. // List object contains only a head whose purpose is to
  36. // point to the first object in the list
  37. Student head;
  38.  
  39.  
  40. public List ()
  41. {
  42. // a basic constructor
  43.  
  44. head = null;
  45. }
  46.  
  47.  
  48. public void printList ()
  49. {
  50. // print the contents of a list
  51.  
  52. Student temp = head;
  53. while (temp != null)
  54. {
  55. System.out.println (temp.name);
  56. temp = temp.link;
  57. }
  58. }
  59.  
  60.  
  61. public void insert (String s)
  62. {
  63. // insert a new node containing s in its name field into a list whose nodes
  64. // are kept in increasing lexicographic order
  65. Student prev = null;
  66. Student temp = head;
  67. Student newStudent = new Student (n, null);
  68. if (head == null)
  69. {
  70. head = newstudent;
  71. }
  72. else
  73. {
  74. while (temp.link != null && temp.name.compareTo (newStudent.name) > 0)
  75. {
  76. previous = temp;
  77. temp = temp.ink;
  78. }
  79. if
  80. {
  81. prev != null
  82. }
  83. {
  84. prev.link = newStudent;
  85. }
  86. newStudent.link = temp;
  87. {
  88.  
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95. class Student
  96. {
  97. // an inner class for nodes of a list
  98.  
  99. String name;
  100. Student link;
  101.  
  102. public Student (String word, Student next)
  103. {
  104. // a simple constructor
  105.  
  106. name = word;
  107. link = next;
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement