generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLL1.java
118 lines (116 loc) · 2.77 KB
/
LL1.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
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.Scanner;
class node{
int data;
node next;
public node(int data){
this.data = data;
next = null;
}
public void display_node(){
System.out.println(this.data);
}
}
class sll{
node head;
public sll(){
head = null;
}
public void insert_at(int a, int pos){
node nl = new node(a);
node current;
if(head == null & pos == 1){
head = nl;
nl.next = null;
}
else if(pos == 1){
nl.next = head;
head = nl;
}
else{
current = head;
for(int i = 0;i<pos-2;i++){
current = current.next;
}
if(current.next == null){
current.next = nl;
nl.next = null;
}
else{
nl.next = current.next;
current.next = nl;
}
}
}
public node delete_first(){
node temp;
if(head == null){
System.out.println("List empty");
return null;
}
else if(head.next == null){
temp = head;
head = null;
return temp;
}
else{
temp = head;
head = head.next;
return temp;
}
}
public node delete_at(int pos){
node temp;
node current = head;
if(head == null){
System.out.println("List empty");
return null;
}
else if(pos == 1 & head.next == null){
temp = head;
head = null;
return temp;
}
else if(pos==1){
temp = head;
head = head.next;
return temp;
}
else{
for(int i =0;i<pos-2;i++){
current = current.next;
}
temp = current.next;
current.next = current.next.next;
return temp;
}
}
public void count(){
int c = 0;
for(node current = head;current.next != null;current = current.next){
c++;
}
System.out.println("Count = "+ c);
}
public void display_list(){
for(node current = head;current != null;current = current.next){
current.display_node();
}
}
}
class LL1{
public static void main(String args[]){
sll list = new sll();
list.insert_at(0, 1);
list.display_list();
System.out.println("efhgf");
list.delete_first();
list.insert_at(0,1);
list.insert_at(1,2);
list.insert_at(2,3);
list.insert_at(3,2);
list.display_list();
System.out.println("efhgf");
list.delete_at(2);
list.display_list();
}
}