数据结构之线性表

1.线性表的基本操作定义

 1 package com.neusoft.List;
 2 
 3 /**
 4  * @author SimonsZhao 
 5  * 线性表的基本操作 
 6  *1.线性表置空
 7  *2.线性表判空
 8  *3.求线性表长度
 9  *4.得到第i个元素的值
10  *5.线性表第i个元素之前插入一个元素
11  *6.删除并返回线性表中的第i个元素
12  *7.线性表中首次出现指定数据元素的位置序号
13  *8.输出线性表中的元素
14  */
15 public interface IList {
16     //1.线性表置空
17     public void clear();
18     //2.线性表判空
19     public boolean isEmpty();
20     //3.求线性表长度
21     public int length();
22     //4.得到第i个元素的值
23     public Object get(int i);
24     //5.线性表第i个元素之前插入一个元素
25     public void insert(int i,Object x);
26     //6.删除并返回线性表中的第i个元素
27     public void remove(int i);
28     //7.线性表中首次出现指定数据元素的位置序号
29     public int indexOf(Object x);
30     //8.输出线性表中的元素
31     public void display();
32     
33 }

2.以物理存储的顺序表方式实现Ilist接口并实现其中的所有方方法

 1 package com.neusoft.List;
 2 
 3 public class SequenceList implements IList{
 4     public Object[] listElem;
 5     public int currentLen;//线性表长度
 6     public  SequenceList(int maxSize) {
 7         currentLen =0;//线性表长度置空
 8         listElem = new Object[maxSize];
 9         //为顺序表分配存储空间
10     }
11     @Override
12     public void clear() {
13         currentLen=0;//置顺序表的当前长度为0
14     }
15 
16     @Override
17     public boolean isEmpty() {
18         return currentLen==0;
19     }
20 
21     @Override
22     public int length() {
23         return currentLen;
24     }
25     //4.得到第i个元素的值
26     @Override
27     public Object get(int i) {
28         if (i<0 || i>currentLen-1) {
29             System.out.println("第"+i+"个元素不存在");
30         }
31         return listElem[i];
32     }
33     @Override
34     public void insert(int i, Object x) {//i指的是在第几个位置插入
35         if (i<0 || i>currentLen) {
36             System.out.println("第"+i+"个元素不存在");
37         }
38         if (currentLen==listElem.length) {
39             System.out.println("顺序表容量已满");
40         }
41         for (int j = currentLen; j >i; j--) {
42             listElem[j]=listElem[j-1];
43         }
44         listElem[i]=x;
45         currentLen++;
46     }
47 
48     @Override
49     public void remove(int i) {
50         // 将顺序表上的第i个元素a从顺序表中删除
51         if (i<0 ||i>=currentLen-1) {
52             System.out.println("删除位置不合法");
53         }
54         for (int j = i; j < currentLen-1; j++) {
55             listElem[j]=listElem[j+1];
56         }
57         currentLen--;
58     }
59 
60     @Override
61     public int indexOf(Object x) {
62         //查找满足条件的数据元素首次出现的位置
63         //显示顺序表中待比较的数据元素,其起始值指示顺序表中第0个元素
64         int j =0;
65         while (j<currentLen&&!listElem[j].equals(x)) {
66             j++;
67         }
68         if (j<currentLen) {
69             return j;//返回值为x的数据元素在线性表中的位置
70         }else {
71             return -1;//顺序表中不存在该元素。
72         }
73     }
74 
75     @Override
76     public void display() {
77         for (int i = 0; i < currentLen; i++) {
78             System.out.println(listElem[i]+" ");
79         }
80         System.out.println();
81     }
82 }
SequenceList顺序表实现类

相关文章:

  • 2022-12-23
  • 2021-08-16
  • 2021-10-05
  • 2021-08-31
  • 2021-11-09
  • 2021-11-21
  • 2021-04-10
  • 2021-05-02
猜你喜欢
  • 2021-11-10
  • 2021-05-09
  • 2021-10-26
  • 2022-03-01
  • 2022-12-23
  • 2021-11-27
  • 2021-04-08
相关资源
相似解决方案