Skip to content

Commit 1f1276d

Browse files
committed
in mess
1 parent 627f3b9 commit 1f1276d

File tree

3 files changed

+77
-21
lines changed

3 files changed

+77
-21
lines changed

group20/2421586846/DS/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>DS</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

group20/2421586846/DS/src/basic/LinkedList.java

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,49 @@ public class LinkedList implements List {
55

66
private Node head;
77

8-
public void add(Object o){
8+
private int size;
9+
public void addError(Object o){
910
Node newNode= new Node();
1011
newNode.data = o;
1112
newNode.next = null;
1213

1314
if (head == null ){
1415
head= newNode;
16+
size++;
17+
return;
1518
}
1619

1720
Node currentNode = head;
21+
/*
1822
while (currentNode.next != null ){
1923
currentNode = currentNode.next ;
2024
}
21-
25+
*/
26+
for (int i=0;i< size;i++){
27+
currentNode = currentNode.next ;
28+
}
2229

2330
currentNode.next =newNode;
24-
31+
size++;
32+
}
33+
public void add(Object o){
34+
if (size==0) {
35+
head=new Node();
36+
head.data=o;
37+
size++;
38+
return;
39+
}
40+
Node last=head;
41+
for (int i = 0; i < size-1; i++) {
42+
last=last.next;
43+
}
44+
Node added=new Node();
45+
last.next=added;
46+
added.data=o;
47+
size++;
2548
}
2649
public void add(int index , Object o){
27-
int oldSize = size();
28-
if (index >= oldSize ||index < 0) {
50+
if (index >= size ||index < 0) {
2951
return;
3052
}
3153

@@ -85,17 +107,8 @@ public Object remove(int index){
85107
}
86108

87109
public int size(){
88-
if (head == null){
89-
return 0;
90-
}
91110

92-
Node currentNode = head;
93-
int i =0;
94-
while (currentNode.next != null){
95-
currentNode =currentNode.next;
96-
i++;
97-
}
98-
return i;
111+
return size;
99112
}
100113

101114
public void addFirst(Object o){
@@ -104,7 +117,7 @@ public void addFirst(Object o){
104117
newNode.next = head;
105118

106119
head = newNode;
107-
120+
size++;
108121
}
109122
public void addLast(Object o){
110123
Node currentNode = head;
@@ -118,26 +131,29 @@ public void addLast(Object o){
118131
newNode.next =null;
119132

120133
currentNode.next = newNode;
121-
134+
size++;
122135
}
123136
public Object removeFirst(){
137+
138+
size--;
124139
if (head ==null){
125140
return null;
126141
}
127142
else {
128143
Node firstNode = head;
129144
head = firstNode.next;
130145
return firstNode;
131-
}
146+
}
132147
}
133148
public Object removeLast(){
134-
int size = size();
149+
135150
if (head ==null ){
136151
return null;
137152
}
138153
if (size ==1){
139154
Node tempnode = head;
140155
head = null;
156+
size--;
141157
return tempnode.data;
142158
}
143159
Node currentNode = head.next;
@@ -149,8 +165,9 @@ public Object removeLast(){
149165
}
150166
PrevNode.next= null;
151167

152-
168+
size--;
153169
return currentNode.data ;
170+
154171
}
155172
public Iterator iterator(){
156173
return null;

group20/2421586846/DS/src/basic/Test.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@
66
public class Test {
77

88
public static void main(String[] args) {
9-
9+
TestLinkedList();
10+
11+
}
12+
public static void TestLinkedList(){
13+
LinkedList ll = new LinkedList();
14+
ll.add(1);
15+
ll.add(2);
16+
ll.add(3);
17+
int size = ll.size();
18+
for (int i=0;i< size;i++){
19+
20+
System.out.println(ll.get(0));
21+
}
22+
}
23+
24+
public static void TestArrayList(){
1025
// TODO Auto-generated method stub
1126
ArrayList arraylist1= new ArrayList();
1227
arraylist1.add(3);
@@ -24,7 +39,14 @@ public static void main(String[] args) {
2439
for (int i = 0 ;i < arraylist1.size();i++){
2540
System.out.println(arraylist1.get(i));
2641
}
42+
System.out.println("ok");
43+
arraylist1.remove(0);
44+
45+
arraylist1.remove(1);
2746

47+
for (int i = 0 ;i < arraylist1.size();i++){
48+
System.out.println(arraylist1.get(i));
49+
}
2850
/*
2951
int[] arr = {1,2,3,4,5};
3052

0 commit comments

Comments
 (0)