-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_list_methods
More file actions
85 lines (77 loc) · 1.42 KB
/
Copy pathLinked_list_methods
File metadata and controls
85 lines (77 loc) · 1.42 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
package linkedlist;
import java.util.Scanner;
public class linked {
static void display (Node head) {
Node p = head;
while(p!=null) {
System.out.println("--> "+p.getData());
p = p.getNext();
}
}
Node deleteposLL(Node head, int pos) {
int i = 0;
Node p = head;
Node q = p;
if(pos>i){ // if not start node
while(p != null){
p = p.getNext();
if( i == pos)break; // has it reached position?
q = p;
}
q.setNext(p.getNext());
p = null;
return head;
}
else { // if start node
head = p.getNext();
p = null;
return head;
}
}
static Node createLL(int values) {
Scanner sc = new Scanner(System.in);
Node head = null;
Node p = null;
Node q;
for(int i = 0;i<values;i++) {
System.out.println("Enter values:");
int tmp_val = sc.nextInt();
if(head == null) {
head = new Node(tmp_val);
p=head;
}
else if(p!= null) {
q = new Node(tmp_val);
p.setNext(q);
while(p.getNext() != null) {
p =p.getNext();
}
}
}
return head;
}
public static void main(String[] args) {
Node LLHead = createLL(5);
display(LLHead);
}
}
class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}