-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList_2.java
More file actions
61 lines (56 loc) · 1.21 KB
/
Copy pathLinkedList_2.java
File metadata and controls
61 lines (56 loc) · 1.21 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
public class LinkedList_2 {
Node head;
class Node{
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}
public void push(int new_data){
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void insertAfter(Node prev_node, int new_data){
if(prev_node == null){
System.out.println("The Given Previous node cannot be null");
return;
}
Node new_node = new Node(new_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
public void append(int new_data){
Node new_node = new Node(new_data);
if(head == null){
head = new Node(new_data);
return;
}
new_node.next = null;
Node last = head;
while(last.next!=null)
last = last.next;
last.next = new_node;
return;
}
public void printList(){
Node tnode = head;
while(tnode!=null){
System.out.println(tnode.data+" ");
tnode = tnode.next;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList_2 lList = new LinkedList_2();
lList.append(6);
lList.push(7);
lList.push(1);
lList.append(4);
lList.insertAfter(lList.head.next, 8);
System.out.println("\n Created Linked List is : ");
lList.printList();
}
}