第一章 线性表

   3. 结点类的描述:

 1 //结点类的描述
 2 public class Node{
 3 
 4     public Object data;
 5     public Node next;
 6     
 7     public Node(){
 8         this(null, null);
 9     }
10     public Node(Object data){
11         this(data, null);
12     }
13     public Node(Object data, Node next){
14         this.data = data;
15         this.next = next;
16     }
17 }
18 
19 //单链表的描述
20 import java.util.Scaner;
21 public class LinkList implement IList{
22     
23     public Node head;
24     
25     public LinkList(){
26         head = new Node();
27     }
28     public LinkList(int n, boolean order) throws Exception{
29         this();
30         if(order)
31             creat1(n);
32         else
33             creat2(n);
34     }
35     public void create1(int n) throws Exception{
36         ...
37     }
38     public void display(){
39         Node node = head.next;
40         while(node.next != null){
41             System.out.print(node.data + ' ');//printIn("单链表的数据是:" + node.data);
42             node = node.next;
43         System.out.printIn();
44     }
45     public Object get(int i) throws Exception{
46         Node node = head.next;
47         int j = 0;
48         while(j < i && node != null)//(j <= i && node.next == null){
49             node = node.next;
50             ++j;
51         }
52         if(j > i || node = null)//(j < i || node.next == null)
53             throw new Exception("第" + i + "个元素不存在");
54         return node.data;
55     }
56     /*public void create1(int n){
57         Node node = head;
58         while(j < n){
59             node.next = node;
60         }
61     }
62     */
63     
64 }
View Code

   2. 顺序表前驱:-- NOT BUG FREE

 1 public class Example2{
 2     public static void main(String[] args) throws Exception{
 3         int n = 10;
 4         SqList F = new SqList(80);
 5         for (int j = 0; j < 81; j++)
 6             F.insert(j, j);
 7         System.out.printIn{"请输入要查找的第i个元素前驱值i"};
 8         int i = new Scanner(System.in).nextInt();
 9         if (0 < i && i < 80)
10             System.out.printIn{i + "的前驱元素是:" + F.get(i - 2)};
11         else
12             System.out.printIn{"直接前驱不存在"};
13     }
14 }
View Code

    1. 实现建立顺序表并查找元素,输出其位置:-- NOT BUG FREE

 1 public class Test2{
 2     public static void main(){
 3         int j = 0;
 4         SqList F = new SqList(10);
 5         F.insert(0, 'a');
 6         F.insert(1, 'z');
 7         F.insert(2, 'd');
 8         F.insert(3, 'm');
 9         F.insert(4, 'z');
10         F.indexOf('z');
11         if(j != -1)
12             System.out.printIn("字母'z'的位置为:" + j);
13         else
14             System.out.printIn("顺序表中没有'z'");
15     }
16 }
View Code
 1 public class Test2 {
 2 
 3       public static void main(String[] args) throws Exception {
 4 
 5          SqList F = new SqList(10);
 6          F.insert(0, 'a');
 7          F.insert(1, 'z');
 8          F.insert(2, 'd');
 9          F.insert(3, 'm');
10          F.insert(4, 'z');
11          int j = F.indexOf('z');
12          if (j != -1)
13              System.out.printIn("字母'z'的位置为:" + j);
14          else
15             System.out.printIn("顺序表中没有'z'");
16     }
17 }
原文地址:https://www.cnblogs.com/cenmny/p/6531816.html