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