forked from iamAnki/Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddLastInLinkedList.java
More file actions
56 lines (47 loc) · 1.14 KB
/
AddLastInLinkedList.java
File metadata and controls
56 lines (47 loc) · 1.14 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
import java.io.*;
import java.util.*;
public class Main {
public static class Node {
int data;
Node next;
}
public static class LinkedList {
Node head;
Node tail;
int size;
void addLast(int val) {
Node temp = new Node();
temp.data = val;
temp.next = null;
if (size == 0) {
head = tail = temp;
} else {
tail.next = temp;
tail = temp;
}
size++;
}
}
public static void testList(LinkedList list) {
for (Node temp = list.head; temp != null; temp = temp.next) {
System.out.println(temp.data);
}
System.out.println(list.size);
if (list.size > 0) {
System.out.println(list.tail.data);
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
LinkedList list = new LinkedList();
String str = br.readLine();
while(str.equals("quit") == false){
if(str.startsWith("addLast")){
int val = Integer.parseInt(str.split(" ")[1]);
list.addLast(val);
}
str = br.readLine();
}
testList(list);
}
}