-Statement-

Untitled

Jun 5th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. 1.  public class Data {
  2. 2.  private int info;
  3. 3.  public Data(int info) { this.info = info; }
  4. 4.  public void show() { System.out.println(info); }
  5. 5.  public int compare(Data d) {
  6. 6.  if (this.info < d.info)   return -1;
  7. 7.  else  return 1;
  8. 8.  }}
  9. 9.  abstract public class Node {
  10. 10. abstract public Node insert(Data d);
  11. 11. abstract public void show();
  12. 12. }
  13. 13. public class Head extends Node {
  14. 14. private Node next;
  15. 15. public Head() { next = new Tail(); }
  16. 16. public Node insert(Data d){ next=next.insert(d); return this; }
  17. 17. public void show() { next.show(); }
  18. 18. }
  19. 19. public class Internal extends Node {
  20. 20. private Node next;
  21. 21. private Data d;
  22. 22. public Internal(Data d, Node nxt) { next = nxt;  this.d = d; }
  23. 23. public Node insert(Data d){
  24. 24. if (this.d.compare(d)==-1){
  25. 25. next=next.insert(d);
  26. 26. return this;
  27. 27. }
  28. 28. else {
  29. 29. Internal tmp = new Internal(d,this);
  30. 30. return tmp;
  31. 31. }}
  32. 32. public void show() { d.show(); next.show(); }
  33. 33. }
  34. 34. public class Tail extends Node {
  35. 35. public Tail() {}
  36. 36. public Node insert(Data d){ return new Internal(d, this); }
  37. 37. public void show() {}
  38. 38. }
  39. 39. public class List {
  40. 40. private Head  head;
  41. 41. public List(){ head = new Head(); }
  42. 42. public void insert(Data d){ head.insert(d);}
  43. 43. public void show() { head.show();}
  44. 44. }
  45. 45. public class Program{
  46. 46. static public void main(String[] args) {
  47. 47. List  l = new List();
  48. 48. l.insert(new Data(5));
  49. 49. l.insert(new Data(9));
  50. 50. l.insert(new Data(1));
  51. 51. l.insert(new Data(8));
  52. 52. l.show();
  53. 53. }
  54. 54. }
  55.  
  56. א. ציירו את הרשימה המקושרת שמתקבלת כתוצאה מהפעלת התוכנית.
  57. ב. ציינו את הקריאות הפולימורפיות בתוכנית (כולל הסבר).
  58. ג. איזה שינוי צריך לערוך בקוד כדי שתיווצר רשימה מקושרת מעגלית.
Advertisement
Add Comment
Please, Sign In to add comment