-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStructure.java
More file actions
110 lines (94 loc) · 2.24 KB
/
DataStructure.java
File metadata and controls
110 lines (94 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package Library;
import java.util.*;
/**
* Name:Lidor Pahima
* DataStructure Class designed to manage collection of elements such as books library system
*/
public class DataStructure<T> {
private ArrayList<T> List = new ArrayList<T>();
/**
* Constructor an empty Data Structure
*/
public DataStructure() {}
/**
* Adds an element at specified index in data structure
* Also will check if index outofbound so exception is thrown
* @param data
* @param index
*/
public void add(T data, int index) {
if (index < 0 || index > List.size()) {
throw new IndexOutOfBoundsException("Worng index ");
}
List.add(index, data);
}
/**
* Adds elements for the end of the data structure
* @param data
*/
public void addToEnd(T data){
List.add(List.size(),data);
}
/**
* Delete elements from the data structure
* @param data
*/
public void delete(T data) {
if (List.contains(data)) {
List.remove(data);
} else {
System.out.println("Not found!");
}
}
/**
* Check if element contains in the data structure
* @param data
* @return
*/
public boolean contains(T data){
if(List.isEmpty()){
return false;
}else if(List.contains(data)){
return true;
}return false;
}
/**
* Return size of the data structure
* @return
*/
public int size(){
return List.size();
}
/**
* To string data structure
* @return
*/
public String toString(){
return List.toString();
}
/**
* Get element from index
* @param index
* @return
*/
public T getData(int index) {
return List.get(index);
}
/**
* Get index from element
* @param data
* @return
*/
public int getIndex(T data) {
if (List.contains(data)) {
return List.indexOf(data);
}
else return -1;
}
/**
* Clear Data structure
*/
public void clear() {
List.clear();
}
}