-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17_SinglyLinkedList.java
More file actions
195 lines (184 loc) · 5.41 KB
/
17_SinglyLinkedList.java
File metadata and controls
195 lines (184 loc) · 5.41 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.util.Scanner;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class SinglyLinkedList {
Node head = null;
Scanner sc = new Scanner(System.in);
void insertAtStart() {
System.out.print("Enter data: ");
int data = sc.nextInt();
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
System.out.println("Inserted at beginning.");
}
void insertAtEnd() {
System.out.print("Enter data: ");
int data = sc.nextInt();
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = newNode;
}
System.out.println("Inserted at end.");
}
void insertAtPosition() {
System.out.print("Enter position: ");
int pos = sc.nextInt();
System.out.print("Enter data: ");
int data = sc.nextInt();
Node newNode = new Node(data);
if (pos == 1) {
newNode.next = head;
head = newNode;
return;
}
Node temp = head;
for (int i = 1; i < pos - 1; i++) {
if (temp == null) {
System.out.println("Invalid position!");
return;
}
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
System.out.println("Inserted at position.");
}
void deleteAtStart() {
if (head == null) {
System.out.println("List is empty.");
return;
}
head = head.next;
System.out.println("Deleted from beginning.");
}
void deleteAtEnd() {
if (head == null) {
System.out.println("List is empty.");
return;
}
if (head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next.next != null)
temp = temp.next;
temp.next = null;
System.out.println("Deleted from end.");
}
void deleteAtPosition() {
System.out.print("Enter position: ");
int pos = sc.nextInt();
if (head == null) {
System.out.println("List empty.");
return;
}
if (pos == 1) {
head = head.next;
return;
}
Node temp = head;
for (int i = 1; i < pos - 1; i++) {
if (temp.next == null) {
System.out.println("Invalid position.");
return;
}
temp = temp.next;
}
temp.next = temp.next.next;
System.out.println("Deleted from position.");
}
void search() {
System.out.print("Enter element to search: ");
int key = sc.nextInt();
Node temp = head;
int pos = 1;
boolean found = false;
while (temp != null) {
if (temp.data == key) {
System.out.println("Element found at position " + pos);
found = true;
break;
}
temp = temp.next;
pos++;
}
if (!found)
System.out.println("Element not found.");
}
void display() {
if (head == null) {
System.out.println("List is empty.");
return;
}
Node temp = head;
System.out.print("Linked List: ");
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\n--- MENU ---");
System.out.println("1. Insert at start");
System.out.println("2. Insert at end");
System.out.println("3. Insert at position");
System.out.println("4. Delete at start");
System.out.println("5. Delete at end");
System.out.println("6. Delete at position");
System.out.println("7. Search");
System.out.println("8. Display");
System.out.println("9. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
list.insertAtStart();
break;
case 2:
list.insertAtEnd();
break;
case 3:
list.insertAtPosition();
break;
case 4:
list.deleteAtStart();
break;
case 5:
list.deleteAtEnd();
break;
case 6:
list.deleteAtPosition();
break;
case 7:
list.search();
break;
case 8:
list.display();
break;
case 9:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
} while (choice != 9);
}
}