forked from dhanushbathula747/Basic-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularlink.java
More file actions
139 lines (131 loc) · 2.7 KB
/
Copy pathCircularlink.java
File metadata and controls
139 lines (131 loc) · 2.7 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
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 addNode(int data) {
Node temp;
Node newNode = new Node(data);
if(head == null) {
head = newNode;
newNode.next = head;
return;
}
else {
for(temp = head; temp.next != head; temp = temp.next);
temp.next = newNode;
newNode.next = head;
}
}
public void addNodeBeg(int data) {
Node temp;
Node newNode = new Node(data);
if(head == null) {
System.out.println("List is empty");
}
else {
for(temp = head;temp.next != head; temp = temp.next);
newNode.next = head;
head = newNode;
temp.next = newNode;
}
}
public void addNodePos(int data,int pos) {
Node temp = head;
Node newNode = new Node(data);
if(head == null && pos == 1) {
newNode.next = head;
head = newNode;
return;
}
else{
int i=1;
while(i<pos-1) {
temp = temp.next;
i++;
}
newNode.next = temp.next;
temp.next = newNode;
}
}
public void addNodeLast(int data) {
Node temp;
Node newNode = new Node(data);
if(head == null) {
newNode.next = head;
head = newNode;
}
for(temp = head;temp.next!= head;temp = temp.next);
temp.next = newNode;
newNode.next = head;
}
public void DeleteFront() {
Node temp;
if(head == null) {
System.out.println("List is empty");
} else {
for(temp = head; temp.next != head ; temp = temp.next);
temp.next = head.next;
head = head.next;
}
}
public void DeleteLast() {
Node temp;
if(head == null) {
System.out.println("List is empty");
} else {
for(temp = head ; temp.next.next != head; temp = temp.next);
temp.next = head;
}
}
public void DeletePos(int pos) {
Node temp = head;
if(head == null) {
System.out.println("List is empty");
} else {
int i = 1;
while(i < pos-1) {
temp = temp.next;
i++;
}
temp.next = temp.next.next;
}
}
public void print() {
Node temp;
if(head == null) {
System.out.println("List is empty");
return;
} else {
for(temp = head; temp.next!= head; temp = temp.next) {
System.out.println(temp.data);
}
System.out.println(temp.data);
}
}
}
public class Circularlink {
public static void main(String[] args) {
Linkedlist L = new Linkedlist();
L.addNode(1);
L.addNode(2);
L.addNode(3);
L.addNode(4);
L.addNodeBeg(5);
L.addNodePos(6,4);
L.addNodeLast(9);
L.DeleteFront();
L.DeleteLast();
L.DeletePos(3);
L.print();
}
}