-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
181 lines (175 loc) · 5.23 KB
/
SinglyLinkedList.java
File metadata and controls
181 lines (175 loc) · 5.23 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.Scanner;
public class SinglyLinkedList{
class Node{
int data;
Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}
public Node head = null;
public Node tail = null;
public void insert_begin(int data){
Node newNode = new Node(data);
newNode.data=data;
if(head==null){
head = newNode;
tail = newNode;
tail.next=null;
}else{
newNode.next=head;
head=newNode;
System.out.println(data+" inserted successfully");
}
}
public void insert_end(int data){
Node newNode = new Node(data);
newNode.data=data;
if(head==null){
head = newNode;
tail = newNode;
tail.next=null;
}else{
tail.next=newNode;
tail=newNode;
System.out.println(data+" inserted at the end");
}
}
public void insert_at_pos(int data, int pos){
Node newNode = new Node(data);
newNode.data=data;
if(pos==1){
newNode.next=head;
head=newNode;
}
Node temp = head;
for(int i=1;i<pos-1;i++){
temp=temp.next;
}
newNode.next=temp.next;
temp.next=newNode;
System.out.println(data+" inserted at position "+pos);
}
public void delete_begin(){
if(head==null){
System.out.println("List is empty");
}
else if(head.next==null){
System.out.println(head.data+" deleted successfully");
head=null;
tail=null;
}
else{
System.out.println(head.data+" deleted successfully");
head=head.next;
}
}
public void delete_end(){
if (head==null) {
System.out.println("List is empty");
}
else if(head.next==null){
System.out.println(tail.data+" deleted successfully");
head=tail=null;
}
else{
Node temp = head;
while (temp.next!=tail) {
temp=temp.next;
}
System.out.println(tail.data+" deleted successfully");
tail=temp;
tail.next=null;
}
}
public void delete_at_pos(int pos){
int count=1;
Node temp = head;
Node prev = null;
if(head==null){
System.out.println("List is empty");
}
else if(head.next==null){
head=null;
}else if(pos==0){
head=head.next;
}
else{
while(temp!=null && count!=pos){
prev=temp;
temp=temp.next;
count++;
}
prev.next=temp.next;
System.out.println(temp.data+" deleted successfully");
}
}
public void display(){
Node temp = head;
if(head==null){
System.out.println("List is empty");
}else{
while(temp!=null){
System.out.print(temp.data+" --->");
temp=temp.next;
}
System.out.println("NULL");
}
}
public static void main(String[] args) {
SinglyLinkedList ll = new SinglyLinkedList();
Scanner sc = new Scanner(System.in);
int choice = 0;
while (choice!=9) {
System.out.println("1. Insert at begin\n2. Insert at end\n3. Insert at position\n4. Delete at begin\n5. Delete at end\n6. Delete at position\n7. Display\n8. Exit\nEnter your choice");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the data");
int data = sc.nextInt();
ll.insert_begin(data);
ll.display();
break;
case 2:
System.out.println("Enter the data");
data = sc.nextInt();
ll.insert_end(data);
ll.display();
break;
case 3:
System.out.println("Enter the data");
data = sc.nextInt();
System.out.println("Enter the position");
int pos = sc.nextInt();
ll.insert_at_pos(data, pos);
ll.display();
break;
case 4:
ll.delete_begin();
ll.display();
break;
case 5:
ll.delete_end();
ll.display();
break;
case 6:
System.out.println("Enter the position");
pos = sc.nextInt();
ll.delete_at_pos(pos);
ll.display();
break;
case 7:
ll.display();
break;
case 8:
sc.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
}