forked from dhanushbathula747/Basic-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingelyLinkedlist.java
More file actions
209 lines (193 loc) · 3.57 KB
/
Copy pathSingelyLinkedlist.java
File metadata and controls
209 lines (193 loc) · 3.57 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import java.util.*;
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class Linkedlist {
Node head;
public Linkedlist() {
this.head = null;
public void InsertBeg(int data) {
Node temp;
Node newNode = new Node(data);
if(head == null) {
head = newNode;
}
else {
for(temp = head; temp.next != null;temp = temp.next);
newNode.next = head;
head = newNode;
}
}
public void Insertpos(int data,int pos) {
Node temp = head;
int i;
Node newNode = new Node(data);
if(head == null)
{
head = newNode;
}
else {
for(i=1;i<pos-1;i++) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
}
}
public void InsertLast(int data) {
Node temp = head;
int i;
Node newNode = new Node(data);
if(head == null)
{
head = newNode;
}
else {
for(temp = head;temp.next!=null;temp = temp.next);
temp.next = newNode;
}
}
public void SortIns(int data) {
Node temp = head;
Node newNode = new Node(data);
if(head == null || head.data >= newNode.data) {
newNode.next = head;
head = newNode;
} else {
while(temp.next.data < newNode.data) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
}
}
public void isLastelement(int data){
if(head == null) {
System.out.println("The list is empty ");
}
else {
Node temp = head;
while(temp.next != null) {
temp = temp.next;
if(temp.next == null) {
System.out.println( +temp.data+"is the last element");
}
}
}
}
public void Isempty() {
if(head == null)
{
System.out.println("List is empty");
}
}
public void DeleteBeg() {
Node temp = head ;
if(head == null) {
System.out.println("List is empty");
} else {
head = head.next;
temp.next = null;
}
}
public void DeleteLast() {
Node temp ;
if(head == null) {
System.out.println("List is empty");
}
for(temp = head;temp.next.next!=null;temp = temp.next);
temp.next = null;
}
public void DeletePos(int pos) {
Node temp = head;
int i = 1;
if(head == null) {
System.out.println("List is empty");
} else {
while(i<pos-1) {
temp = temp.next;
i++;
}
if(pos-1 == i){
temp.next = temp.next.next;
}
}
}
public void evenPositioned() {
int pos = 1;
Node temp = head;
while(temp.next != null) {
temp = temp.next;
pos++;
}
temp = head;
while(temp.next != null){
if(pos % 2 == 0) {
System.out.println(temp.data);
}
pos++;
temp = temp.next;
}
}
boolean isPalindrome() {
int i = 0;
int j;
Node first;
Node last;
int c = 1;
Node temp = head;
while(temp.next != null) {
c ++;
temp = temp.next;
}
while(i != c/2) {
first = last = this.head;
for(j = 0; j < i; j++ ) {
first = first.next;
}
for(j = 0; j < c - i - 1; j++) {
last = last.next;
}
if(first.data != last.data) {
return false;
} else {
i++;
}
}
return true;
}
public void print() {
Node temp;
for(temp = head; temp!=null ;temp = temp.next){
System.out.println(temp.data);
}
}
}
class SingelyLinkedlist {
public static void main(String[] args) {
Linkedlist L = new Linkedlist();
L.InsertBeg(10);
L.InsertLast(20);
L.InsertLast(30);
L.InsertLast(40);
L.InsertLast(60);
L.DeleteBeg();
L.print();
L.DeletePos(3);
L.DeletePos(2);
L.print();
L.SortIns(25);
L.SortIns(5);
L.isLastelement(70);
System.out.println(L.isPalindrome());
System.out.println("the even positioned elements are");
L.evenPositioned();
System.out.println("The element in the list are");
L.print();
}
}