-
Notifications
You must be signed in to change notification settings - Fork 0
/
FollowTheList2.java
84 lines (75 loc) · 1.28 KB
/
FollowTheList2.java
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
import java.util.Scanner;
public class FollowTheList2 {
public class Node{
int data;
Node next;
}
private Node head;
private Node tail;
private int size;
public void Add1(int data) {
if(size==0) {
Node s=new Node();
s.data=data;
s.next=null;
head=tail=s;
size++;
}
else {
Node temp=new Node();
temp.data=data;
temp.next=null;
tail.next=temp;
tail=temp;
size++;
}
}
public void dis() {
Node temp=head;
while(temp!=null) {
System.out.println(temp.data);
temp=temp.next;
}
}
public void del() {
if(size>1) {
head=head.next;
size--;
}
}
public int getindex(int index) {
Node temp=head;
for(int i=0;i<index;i++) {
temp=temp.next;
}
return temp.data;
}
public void printmiddle() {
if(size%2==0) {
System.out.println(getindex((size/2)-1));
System.out.println(getindex(size/2));
}
else {
System.out.println(getindex(size/2));
}
}
public static void main(String[] args) {
FollowTheList2 list=new FollowTheList2();
list.Add1(1);
Scanner scn=new Scanner(System.in);
int s=scn.nextInt();
for(int i=0;i<s;i++) {
int h=scn.nextInt();
if(h==1) {
int p=scn.nextInt();
list.Add1(p);
}
else if(h==2) {
list.del();
}
else {
list.printmiddle();
}
}
}
}