-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSec_Access.java
More file actions
155 lines (125 loc) · 2.97 KB
/
Sec_Access.java
File metadata and controls
155 lines (125 loc) · 2.97 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package stack1;
public class Sec_Access {
Nodes head;
int size;
Nodes last;
Nodes before;
Sec_Access(){
head = null;
size = 0;
}
public boolean isEmpty()
{return head == null;}
public int getSize()
{return size;}
//*******************************************//
//Takes out first item in the Queue or Stack*//
//*******************************************//
public int kick()
{
if(isEmpty())throw new RuntimeException("No items currently in Queue");
else
{
int tmp = head.getData();
head = head.next;
size--;
return tmp;
}
}
//**************************************************//
//toArray() should return an array with all elements//
//**************************************************//
public int[] toArray()
{
if(isEmpty())throw new RuntimeException("No items currently in Queue");
Nodes temp = head;
int i=0;
int [] array = new int[size];
while(temp != null)
{
array[i] = temp.getData();
temp = temp.next;
i++;
}
return array;
}
/*dump(string separator) should print on console an string with all
*values separated by the provided separator, if not separator is
*defined then it should return separated by commas
**/
public void dump(String sptor)
{
String dump = "";
if(sptor == ""){
sptor = ",";
}
while(!isEmpty()){
if(head.next == null) sptor = "";
dump = (dump + head.getData() + sptor);
head = head.next;
size--;
}
System.out.print(dump);
}
/*join(string separator) should return a string with
*all values separated by the provided separator, if not
*separator is defined then it should return separated by commas
***/
public String join(String sptor)
{
String dump = "";
if(sptor == ""){
sptor = ",";
}
while(!isEmpty()){
if(head.next == null) sptor = "";
dump = (dump + head.getData() + sptor);
head = head.next;
size--;
}
return dump;
}
//unique() returns all unique values, this means, the values which are not duplicated
public String unique()
{
if(isEmpty())throw new RuntimeException("No items in the list");
Nodes current = head;
String print ="";
while(current != null)
{
Nodes runner = current;
while(runner.next != null)
{
if(runner.next.getData() == current.getData())
runner.next = runner.next.next;
else
runner = runner.next;
}
current = current.next;
}
//print out values
while(!isEmpty()){
print = (print + head.getData()+ " ");
head = head.next;
}
return print;
}
//stack & queue (int elements) should remove number of elements given
public void removeItem(int d)
{
Nodes current = head;
Nodes prev = null;
if (head.getData() == d)
{
head = head.next;
return;
}
while(current != null && current.getData() != d)
{
prev = current;
current = current.next;
}
prev.next = current.next;
size--;
}
}