-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChain.java
More file actions
118 lines (116 loc) · 3.29 KB
/
Chain.java
File metadata and controls
118 lines (116 loc) · 3.29 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
import java.util.Iterator;
import java.util.Scanner;
public class Chain<Item> implements Iterable<Item>{
private Node first;
private int N;
private class Node{
Item item;
Node next;
}
public boolean isEmpty(){return first==null;}
public int size(){return N;}
public void push(Item item){
Node oldfirst=first;
first=new Node();
first.item=item;
first.next=oldfirst;
N++;
}
public Item pop(){
Item item=first.item;
first=first.next;
N--;
return item;
}
public void delete(int k){ //index=k
if (k>=N){
System.out.println("index out of range");
}
else {
if (k==0){
first=first.next;
N--;
}
else{
Node previous=first;
Node current=previous.next;
Node suivant=current.next;
for (int i=0;i<k-1;i++){
previous=current;
current=previous.next;
suivant=current.next;
//previous.next=suivant;
}
previous.next=suivant;
current=null;
N--;
}
}
}
public boolean find(Item k){
Node current=first;
while (current.next!=null){
Item a=current.item;
if (a.equals(k)){ //.equals
return true;
}
current=current.next;
}
return false;
}
public void removeAfter(Item s){
Node current=first;
while (current.next!=null){
Item a=current.item;
if (a.equals(s)){ //.equals
current.next=null;
break;
}
current=current.next;
}
N--;
}
public void insertAfter(Item x,Item y){
Node current=first;
while (current.next!=null){
Item a=current.item;
if (a.equals(x)){ //.equals
Node additional=new Node();
additional.item=y;
additional.next=current.next;
current.next=additional;
break;
}
current=current.next;
}
N++;
}
public Iterator<Item> iterator(){
return new ListIterator();
}
private class ListIterator implements Iterator<Item>{
private Node current=first;
public boolean hasNext(){return current!=null;}
public void remove(){}
public Item next(){
Item item =current.item;
current=current.next;
return item;
}
}
public static void main(String[] args){
Chain<String> s =new Chain<String>();
for (int i=0;i<10;i++){
s.push(Integer.toString(i));
}
//System.out.println(s);
// for (int i=0;i<10;i++){
// System.out.println(s.pop());
// }
//s.delete(10);
System.out.println(s.find("5"));
s.removeAfter("5");
s.insertAfter("7", "0");
for (String item:s){System.out.println(item + '1');}
}
}