题目描述


已知链表类的定义如下,实现各个成员函数。主函数中输入数据(以0结束)利用Insert函数依次将数据插入到表的1号位置,利用DispList按照逻辑次序输出表中元素,再输入一个要查找的元素,利用查找函数Locate查找其在表中的位置,最后利用Reverse函数将数据逆序,再利用DispList输出。
template
class LinkList
{
public:
LinkList( ); //建立只有头结点的空链表
~LinkList(); //析构函数
int Length(); //求单链表的长度
int Locate(T x); //求单链表中值为x的元素序号
void Insert(int i, T x); //在单链表中第i个位置插入元素值为x的结点
void Reverse( ); //reverse list
void DispList( ); //遍历单链表,按序号依次输出各元素
private:
Node *first; //单链表的头指针
};
输入样例说明:
例如输入数据为:1 2 3 4 5 6 0 3,即将1,2,3,4,5,6插入表中的1号位置,得到逻辑次序为6,5,4,3,2,1的顺序表,3为在表中待查找的数据,3的位置为4。
若输入:1 2 3 4 5 6 0 13
13在表中无,则输出:No found
即输出结果:
The length:6
The elements:
6 5 4 3 2 1
No found
The length:6
The elements:
1 2 3 4 5 6

输入

输出

样例输入

1 2 3 4 5 6 0 3

样例输出

The length:6
The elements:
6 5 4 3 2 1
Found position:4
The length:6
The elements:
1 2 3 4 5 6

提示

来源

提交

  1. import java.util.Scanner;
  2. class Node {
  3. public Object data;
  4. public Node next;
  5. public Node() {
  6. this(null,null);
  7. }
  8. public Node(Object data) {
  9. this(data,null);
  10. }
  11. public Node(Object data, Node next) {
  12. this.data = data;
  13. this.next = next;
  14. }
  15. }
  16. class LinkList{
  17. private Node head;
  18. public LinkList() {
  19. this.head = new Node();
  20. }
  21. public int length() {
  22. Node p = head.next;
  23. int length = 0;
  24. while(p!=null){
  25. p = p.next;
  26. ++length;
  27. }
  28. return length;
  29. }
  30. public Object get(int i) throws Exception {
  31. Node p = head.next;
  32. int j = 0;
  33. while (p != null && j < i) {
  34. p = p.next;
  35. ++j;
  36. }
  37. if (j > i || p == null) {
  38. throw new Exception("No found");
  39. }
  40. return p.data;
  41. }
  42. public void insert(int i, Object x) throws Exception {
  43. Node p = head;
  44. int j = -1;
  45. while (p != null && j<i-1 ) {
  46. p = p.next;
  47. ++j;
  48. }
  49. if (j > i-1 || p == null)
  50. throw new Exception("插入位置不合法");
  51. if (j > i-1 || p == null)
  52. throw new Exception("插入位置不合法");
  53. Node s = new Node(x);
  54. s.next=p.next;
  55. p.next=s;
  56. }
  57. public void display() {
  58. System.out.println("The length:"+length());
  59. System.out.println("The elements:");
  60. Node node = head.next;
  61. while (node != null) {
  62. System.out.print(node.data + " ");
  63. node = node.next;
  64. }
  65. System.out.println();
  66. }
  67. public void reverse() {
  68. if(head==null || head.next==null){
  69. return;
  70. }
  71. Node pre = null;
  72. Node cur = null;
  73. Node next = null;
  74. cur = head.next;
  75. next = cur.next;
  76. cur.next = null;
  77. pre = cur;
  78. cur = next;
  79. while(cur.next != null){
  80. next = cur.next;
  81. cur.next = pre;
  82. pre = cur;
  83. cur = next;
  84. }
  85. cur.next = pre;
  86. head.next = cur;
  87. }
  88. }
  89. public class Main {
  90. public static void main(String[] args) {
  91. LinkList L = new LinkList();
  92. Scanner sc = new Scanner(System.in);
  93. while(sc.hasNext()){
  94. int d = sc.nextInt();
  95. if(d==0)break;
  96. try {
  97. L.insert(0,d);
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. L.display();
  103. try {
  104. System.out.println("Found position:"+L.get(sc.nextInt()-1));
  105. } catch (Exception e) {
  106. System.out.println("No found");
  107. }
  108. L.reverse();
  109. L.display();
  110. }
  111. }