-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
176 lines (166 loc) · 4.78 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
176 lines (166 loc) · 4.78 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
import java.util.Scanner;
public class SinglyLinkedList {
class Node{
int data; //value
Node next; //address of the next node
Node(int data){
this.data=data;
this.next=null;
}}
Node head; //points to the first node
int size; //total nodes
public void insertFront(int data){
Node newNode = new Node(data); //creating a new node for operations
newNode.next=head; //linking it to the current node
head=newNode; //make the newNode the new head
size++; //time complexity is O(1)
}
public void insertEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
size++;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
size++;
}
public void insertPosition(int data, int pos) {
if (pos < 1) {
System.out.println("Invalid position.");
return;
}
Node newNode = new Node(data);
if (pos == 1) {
newNode.next = head;
head = newNode;
size++;
return;
}
Node temp = head;
for (int i = 1; i < pos - 1 && temp != null; i++) {
temp = temp.next;
}
if (temp == null) {
System.out.println("Position out of bounds!");
return;
}
newNode.next = temp.next;
temp.next = newNode;
size++;
}
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
System.out.println("Size is "+size);
}
public Node get(int index){
Node node = head;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node;
}
public void deleteFirst(){
if (head==null){
System.out.println("List already empty.");
return; }
int val = head.data;
System.out.println("Deleted: "+val);
head=head.next;
size--;
}
public void deleteLast(){
if(size<=1){
deleteFirst();
}
if (head.next == null) {
System.out.println("Deleted: " + head.data);
head = null;
return;
}
Node temp=head;
while(temp.next.next!=null){
temp=temp.next; }
System.out.println("Deleted: "+temp.next.data);
temp.next=null;
size--;
}
public void deletePosition(int pos){
if(head==null){
System.out.println("List is empty.");
return;
}
if(pos==0){
System.out.println("Deleted: "+head.data);
head=head.next;
size--;
return;
}
if (pos < 0 || pos >= size) {
System.out.println("Invalid position!");
return;
}
Node prev=head;
for (int i = 0; i < pos-1; i++) {
prev=prev.next;
}
Node toDel=prev.next;
System.out.println("Deleted: "+toDel.data);
prev.next=toDel.next;
size--;
}
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
Scanner sc = new Scanner(System.in);
int choice;
do{
System.out.println("Enter 1 for inserting at the front\n2 for inserting at the end\n3 for inserting in the middle\n4 to delete at the beginning\n5 to delete at the end\n6 to delete at a position\n7 to display\n8 to exit: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to insert: ");
int val1 = sc.nextInt();
list.insertFront(val1);
break;
case 2:
System.out.print("Enter value to insert: ");
int val2 = sc.nextInt();
list.insertEnd(val2);
break;
case 3:
System.out.println("Enter the value to insert: ");
int val3 = sc.nextInt();
System.out.println("Enter the position to insert at: ");
int pos = sc.nextInt();
list.insertPosition(val3, pos);
break;
case 4:
list.deleteFirst();
break;
case 5:
list.deleteLast();
break;
case 6:
System.out.println("Enter position to delete that node: ");
int index= sc.nextInt();
list.deletePosition(index-1);
break;
case 7:
list.display();
break;
case 8:
System.out.println("Exit code...");
break;
default:
System.out.println("Invalid choice.");
}} while(choice!=8);
}}