java数据结构和算法------线性表(顺序表结构)

 1 package iYou.neugle.list;

 2 

 3 public class MySeqList<T> {

 4     private int initMaxSize = 10;

 5     private T[] list;

 6     private int listLen = 0;

 7 

 8     public MySeqList() {

 9         this.Init();

10     }

11 

12     public MySeqList(int size) {

13         this.initMaxSize = size;

14         this.Init();

15     }

16 

17     public void Init() {

18         this.list = (T[]) new Object[this.initMaxSize];

19     }

20 

21     // 顺序表添加操作

22     public boolean Add(T data) {

23         if (this.listLen < this.initMaxSize) {

24             this.list[listLen] = data;

25             this.listLen++;

26             System.out.println("插入成功");

27             return true;

28         }

29         System.out.println("顺序表数据已满");

30         return false;

31     }

32 

33     // 顺序表插入操作

34     public boolean Insert(int p, T data) {

35         if (p < 0 || p > this.initMaxSize - 1) {

36             System.out.println("插入数据位置非法");

37             return false;

38         }

39         if (this.listLen == this.initMaxSize) {

40             System.out.println("顺序表数据已满");

41             return false;

42         }

43         // 容错处理

44         if (p > this.listLen) {

45             p = this.listLen;

46             this.list[p] = data;

47         } else {

48             for (int i = this.listLen - 1; i >= p; i--) {

49                 this.list[i + 1] = this.list[i];

50             }

51             this.list[p] = data;

52         }

53         this.listLen++;

54         System.out.println("插入成功");

55         return true;

56     }

57 

58     // 顺序表删除操作

59     public boolean Delete(int p) {

60         if (p < 0 || p > this.initMaxSize - 1) {

61             System.out.println("删除数据位置非法");

62             return false;

63         }

64         for (int i = p; i <= this.listLen - 1; i++) {

65             this.list[i] = this.list[i + 1];

66         }

67         this.listLen--;

68         System.out.println("删除成功");

69         return true;

70     }

71 

72     // 顺序表按下标查找

73     public T Get(int p) {

74         if (p < 0 || p > this.initMaxSize - 1) {

75             System.out.println("查找数据不存在");

76             return null;

77         }

78 

79         System.out.println("查找成功,数据为:" + this.list[p]);

80         return this.list[p];

81     }

82 

83     // 顺序表大小

84     public int SizeOf() {

85         return this.listLen;

86     }

87 

88     public void Print() {

89         System.out.print("[");

90         for (int i = 0; i < this.list.length; i++) {

91             System.out.print(this.list[i]);

92             if (i < this.list.length - 1) {

93                 System.out.print(",");

94             }

95         }

96         System.out.println("]");

97     }

98 }

 

你可能感兴趣的:(java)