-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkList.java
186 lines (161 loc) · 4.42 KB
/
LinkList.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
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
import java.util.Scanner;
/**
* Author: Hemant Tripathi
*/
public class LinkList {
public static void main(String[] args) {
boolean isResume = true;
Node headerNode = new Node();
while(isResume) {
System.out.println("Selection an option...");
System.out.println("1. Add a node at the beginning of list");
System.out.println("2. Add a node in the middle of list");
System.out.println("3. Add a node at the end of list");
System.out.println("4. Remove a node from the start of list");
System.out.println("5. Remove a node from the middle of list");
System.out.println("6. Remove a node from the end of list");
System.out.println("7. Show all nodes of the list");
System.out.println("8. Exit");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
switch(num) {
case 1:
System.out.println("Enter node into beginning of list:");
int data = in.nextInt();
Node startNode = new Node(data);
headerNode.addNodeToBeinning(startNode);
System.out.println("Node added successfully!");
break;
case 2:
System.out.println("Enter the data for Node:");
int nodeData = in.nextInt();
System.out.println("Enter the position of node in the list:");
int position = in.nextInt();
Node newNode = new Node(nodeData);
boolean isAdded = headerNode.addToPosition(newNode, position);
if(isAdded) {
System.out.println("Node added successfully!");
} else {
System.out.println("Unable to add the node!");
}
break;
case 3:
System.out.println("Enter the data for Node:");
int nodeValue = in.nextInt();
Node endNode = new Node(nodeValue);
headerNode.addToEnd(endNode);
System.out.println("Node added successfully!");
break;
case 4:
System.out.println("Remove node from beginning:");
boolean success = headerNode.removeNodeFromStart();
if(success) {
System.out.println("Removed first Node");
} else {
System.out.println("Unable to remove first Node.");
}
break;
case 5:
System.out.println("Removing node from a position");
System.out.println("Enter the position of node in the list:");
int nodePosition = in.nextInt();
boolean isRemoved = headerNode.removeNodeFromPosition(nodePosition);
if(isRemoved) {
System.out.println("Removed Node");
} else {
System.out.println("Unable to remove first Node.");
}
break;
case 6:
break;
case 7:
System.out.println("Displaying all nodes!");
headerNode.showAllNodes();
break;
case 8:
isResume = false;
break;
}
}
}
static class Node {
int data;
Node next = null;
public Node() {
}
public Node(int d) {
data = d;
}
public void addToEnd(Node endNode) {
Node header = this;
while(header.next != null) {
header = header.next;
}
header.next = endNode;
}
public boolean addToPosition(Node newNode, int position) {
Node header = this;
int counter = 0;
while(header.next != null && counter < position-1) {
counter++;
header = header.next;
}
if(counter < position-1) {
return false;
} else {
newNode.next = header.next;
header.next = newNode;
return true;
}
}
public void addNodeToBeinning(Node newNode) {
Node header = this;
newNode.next = header.next;
header.next = newNode;
}
public boolean removeNodeFromStart() {
Node header = this;
Node deleteNode = null;
if(header.next != null) {
deleteNode = header.next;
header = deleteNode.next;
this.next = header;
deleteNode.next = null;
deleteNode = null;
return true;
} else {
return false;
}
}
public boolean removeNodeFromPosition(int position) {
Node header = this;
Node deleteNode = null;
int counter = 0;
if(header.next != null) {
deleteNode = header.next;
}
while(header.next != null && counter < position-1) {
counter++;
header = header.next;
}
if(counter < position-1) {
return false;
} else {
deleteNode = header.next;
header = deleteNode.next;
this.next = header;
deleteNode.next = null;
deleteNode = null;
return true;
}
}
public void showAllNodes() {
Node header = this;
System.out.println("LinkList is: ");
while(header.next != null) {
header = header.next;
System.out.println(" "+header.data);
}
}
}
}