Advertisement
Guest User

Untitled

a guest
May 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. package Algorithms.lists.sortedList;
  2.  
  3. /**
  4. * Created by Ruslan Zhdan on 29.05.2016.
  5. */
  6. public class Link
  7. {
  8. public long dData;
  9. public Link next;
  10.  
  11. public Link(long dData)
  12. {
  13. this.dData = dData;
  14. }
  15.  
  16. public void displayLink(){
  17. System.out.print(dData + " ");
  18. }
  19. }
  20.  
  21. class SortedList{
  22. private Link first;
  23.  
  24. public SortedList(){
  25. first = null;
  26. }
  27.  
  28. public boolean isEmpty(){
  29. return first == null;
  30. }
  31.  
  32. public void insert(long key){
  33. Link newLink = new Link(key);
  34. Link previous = null;
  35. Link current = first;
  36.  
  37. while (current != null && key > current.dData){
  38. previous = current;
  39. current = current.next;
  40. }
  41.  
  42. if (previous == null)
  43. first = newLink;
  44. else
  45. previous.next = newLink;
  46. newLink.next = current;
  47. }
  48.  
  49. public Link remove(){
  50. Link temp = first;
  51. first = first.next;
  52. return temp;
  53. }
  54.  
  55. public void displayList(){
  56. System.out.print("List (first-->last): ");
  57. Link current = first;
  58. while (current != null){
  59. current.displayLink();
  60. current = current.next;
  61. }
  62. System.out.println("");
  63. }
  64. }
  65.  
  66. class SortedListApp{
  67. public static void main(String[] args)
  68. {
  69. SortedList theSortedlist = new SortedList();
  70. theSortedlist.insert(20);
  71. theSortedlist.insert(40);
  72.  
  73. theSortedlist.displayList();
  74.  
  75. theSortedlist.insert(10);
  76. theSortedlist.insert(30);
  77. theSortedlist.insert(50);
  78.  
  79. theSortedlist.displayList();
  80.  
  81. theSortedlist.remove();
  82.  
  83. theSortedlist.displayList();
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement