-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathMain.java
More file actions
148 lines (144 loc) · 3.75 KB
/
Main.java
File metadata and controls
148 lines (144 loc) · 3.75 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
// Linked list implemtation add, delete from (first ,last, between) Linked list
class Main{
Node head=null;
// create Node class
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
// to add node at head
void addfirst(int data){
Node newNode=new Node(data);
if(head==null){
head=newNode;
}
else{
newNode.next=head;
head=newNode;
}
}
// to print linked list
void print(){
if(head==null){
System.out.print("Empty list");
}
else{
Node currNode=head;
while(currNode!=null){
System.out.print(currNode.data);
currNode=currNode.next;
}
}
}
// search element in linked list from last to delete
void search(int data){
if(head==null){
System.out.print("No");
}
else{
Node currNode=head;
int count=0;
while(currNode!=null){
count=count+1;
if(currNode.data==data){
System.out.print("Node found at position ->"+ currNode.data);
break;
}
currNode=currNode.next;
}
if(currNode==null){
System.out.print("Node not available");
}
}
}
// method to add node in middle
void addbtw(int data,int dpos){
Node newNode=new Node(data);
Node currNode=head;
if(head==null ){
head=newNode;
}
while(currNode!=null){
if(currNode.data==dpos){
newNode.next=currNode.next;
currNode.next=newNode;
break;
}
currNode=currNode.next;
}
if(currNode==null){
System.out.print("Element does not exist enter valid element");
}
return;
}
// method to delete node from middle
void deletebtw(int data){
if(head==null){
return;
}
else{
if(head.data==data){
head=head.next;
}
else{
Node prevNode=head;
Node currNode=head.next;
while(currNode!=null){
if(currNode.data==data){
prevNode.next=currNode.next;
currNode.next=null;
break;
}
currNode=currNode.next;
prevNode=prevNode.next;
}
if(currNode==null){
System.out.println("Element does not exist in list");
}
}
}
}
// method to count no. of nodes in linked list
int count(){
Node currNode=head;
int l=0;
while(currNode!=null){
l=l+1;
currNode=currNode.next;
}
return l;
}
//method to get element from the end of linked list
int nodefromlast(int n){
int q=count();
int z=1,lastn=0;
if (head==null || q<n || n==0 ||n<0){
return -1;
}
else{
lastn=q-n+1;
Node currNode=head;
while(z< lastn){
currNode=currNode.next;
z=z+1;
}
return (currNode.data);
}
}
public static void main(String args[]){
Main ob=new Main();
ob.addfirst(4);
ob.addfirst(3);
ob.addfirst(2);
ob.addfirst(1);
ob.addbtw(7,3);
ob.deletebtw(3);
System.out.println(ob.count());
System.out.println(ob.nodefromlast(0));
ob.print();
}
}