-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBag.java
More file actions
77 lines (72 loc) · 1.9 KB
/
Bag.java
File metadata and controls
77 lines (72 loc) · 1.9 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
package com.wang.code.one;
/**
* @author WANGJJ
* @date 2020/04/23
*/
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Bag<Item> implements Iterable<Item> {
private int N;
private Node<Item> first;
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
public Bag() {
first = null;
N = 0;
}
public boolean isEmpty() {
return first == null;
}
public int size() {
return N;
}
public void add(Item item) {
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
N ++;
}
@Override
public Iterator<Item> iterator() {
return new ListIterator<Item>(first);
}
private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current;
public ListIterator(Node<Item> first) {
current = first;
}
@Override
public boolean hasNext() { return current != null; }
@Override
public void remove() { throw new UnsupportedOperationException(); }
@Override
public Item next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
Item item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args) {
Bag<String> bag = new Bag<String>();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=0;i<n;i++) {
String item = in.next();
bag.add(item);
}
System.out.println("size of bag = " + bag.size());
for(String s : bag) {
System.out.println(s);
}
for (String s : bag) {
System.out.println(s);
}
}
}