-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedlistTwoItem.java
More file actions
105 lines (84 loc) · 2.06 KB
/
LinkedlistTwoItem.java
File metadata and controls
105 lines (84 loc) · 2.06 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
public class LinkedlistTwoItem {
private Node first;
private int count;
private Node last;
public LinkedlistTwoItem() {
first = null;
//last = null;
count = 0;
}
public void prepend(String name, int gpa) {
first = new Node(name,gpa,first);
count++;
if (count == 1) {
last = first;
}
}
public int size(){
return 0;
}
public boolean isEmpty(){
return (first == null);
}
public void check(){
//System.out.println(first.item);
//System.out.println(last.next);
//System.out.println(first.next.next.item);
//System.out.println(first.next.next.next.item);
// first = first.next;
//System.out.println(last.item);
//System.out.println(last.next);
//last.next = first;
//last = first.next;
// Node traveller;
//for (traveller = first; traveller != null; traveller = traveller.next){
// System.out.println("Node = " + traveller.item);
//}
//System.out.println(traveller.next);
Node traveller = first;
while(traveller != null){
System.out.println("Name is " + traveller.Name + " and GPA is " + traveller.GPA);
traveller = traveller.next;
}
}
public void append(String name ,int gpa) {
Node tmp = new Node(name,gpa);
last.next = tmp;
last = tmp;
count++;
tmp = null;
}
public String toString() {
StringBuilder sb = new StringBuilder(count*10);
sb.append("List: size = "+count);
for (Node traveller = first; traveller != null; traveller = traveller.next) {
//this will invoke the String’s toString method!!
sb.append("\n\t"+"Name is " + traveller.Name + " and GPA is " + traveller.GPA);
}
return sb.toString();
}
public static void main(String[] args) {
LinkedlistTwoItem obj = new LinkedlistTwoItem();
obj.prepend("Kamel",10);
obj.prepend("Daniel",7);
obj.append("Ali",3);
System.out.println(obj);
// obj.check();
}
}
class Node {
int GPA;
String Name;
Node next;
Node(String Name, int GPA, Node next) {
this.GPA = GPA;
this.Name = Name;
this.next = next;
}
Node(String Name, int GPA) {
this(Name,GPA,null);
}
Node() {
this(null,0);
}
}