-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.java
More file actions
167 lines (138 loc) · 4.2 KB
/
linkedlist.java
File metadata and controls
167 lines (138 loc) · 4.2 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
import java.util.Scanner;
class LinkedList {
// STEP 1: Node structure
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Head of the linked list
Node head = null;
// INSERT AT BEGINNING
void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
System.out.println("Inserted at beginning");
}
// INSERT AT END
void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
System.out.println("Inserted at end");
}
// INSERT AT POSITION
void insertAtPosition(int data, int position) {
if (position == 1) {
insertAtBeginning(data);
return;
}
Node newNode = new Node(data);
Node temp = head;
for (int i = 1; i < position - 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 " + position);
}
// DELETE BY VALUE
void delete(int data) {
if (head == null) {
System.out.println("List is empty");
return;
}
if (head.data == data) {
head = head.next;
System.out.println("Node deleted");
return;
}
Node temp = head;
Node prev = null;
while (temp != null && temp.data != data) {
prev = temp;
temp = temp.next;
}
if (temp == null) {
System.out.println("Value not found");
} else {
prev.next = temp.next;
System.out.println("Node deleted");
}
}
// TRAVERSE
void traverse() {
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");
}
// MAIN METHOD
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList list = new LinkedList();
while (true) {
System.out.println("\n--- MENU ---");
System.out.println("1. Insert at Beginning");
System.out.println("2. Insert at End");
System.out.println("3. Insert at Position");
System.out.println("4. Delete");
System.out.println("5. Traverse");
System.out.println("6. Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value: ");
list.insertAtBeginning(sc.nextInt());
break;
case 2:
System.out.print("Enter value: ");
list.insertAtEnd(sc.nextInt());
break;
case 3:
System.out.print("Enter value: ");
int value = sc.nextInt();
System.out.print("Enter position: ");
int pos = sc.nextInt();
list.insertAtPosition(value, pos);
break;
case 4:
System.out.print("Enter value to delete: ");
list.delete(sc.nextInt());
break;
case 5:
list.traverse();
break;
case 6:
System.out.println("Program ended");
sc.close();
return;
default:
System.out.println("Invalid choice");
}
}
}
}