Skip to content

Commit 1102187

Browse files
complete circular linklist but not remove last element.
1 parent 44aa89b commit 1102187

File tree

3 files changed

+127
-5
lines changed

3 files changed

+127
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.class
1+
*.class
2+
/.vscode/*

CircularlyLinkedList.java

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,127 @@
1-
public class CircularlyLinkedList{
2-
1+
class CircularLikedList <E>{
2+
//create a node
3+
private static class Node<E> {
4+
private E element;
5+
private Node<E> next;
6+
public Node (E e, Node<E> n){
7+
element = e;
8+
next = n;
9+
}
10+
public E getElement(){
11+
return element;
12+
}
13+
public Node<E> getNext(){
14+
return next;
15+
}
16+
public void setNext(Node<E> n){
17+
next = n;
18+
}
19+
}
20+
private Node<E> tail = null;
21+
private int size = 0;
22+
public void CircularLikedList() {}
23+
//access methods
24+
public int size(){return size;}
25+
public boolean isEmpty(){return size == 0;}
26+
public E getFirst(){
27+
if (isEmpty()) {
28+
return null;
29+
}
30+
return tail.getNext().getElement();
31+
}
32+
public E getLast(){
33+
if (isEmpty()) {
34+
return null;
35+
}
36+
return tail.getElement();
37+
}
38+
//update methods
39+
public void rotate(){
40+
if (tail != null) {
41+
tail = tail.getNext();
42+
}
43+
}
44+
public void addFirst(E e){
45+
if (size == 0) {
46+
tail = new Node<>(e, null);
47+
tail.setNext(tail);
48+
} else{
49+
Node<E> newest = new Node<>(e, tail.getNext());
50+
tail.setNext(newest);
51+
}
52+
size++;
53+
}
54+
public void addLast(E e){
55+
addFirst(e);
56+
tail = tail.getNext();
57+
}
58+
public E removeFirst(){
59+
if (isEmpty()) {
60+
return null;
61+
}
62+
Node<E> head = tail.getNext();
63+
if (head == tail) {
64+
tail = null;
65+
}else{
66+
tail.setNext(head.getNext());
67+
}
68+
size--;
69+
return head.getElement();
70+
}
71+
public E removeLast(){
72+
if (isEmpty()) {
73+
System.out.println("Empty!");
74+
return null;
75+
}
76+
if (tail.next==tail) {
77+
tail = null;
78+
return null;
79+
}
80+
Node<E> secondLast = tail;
81+
Node<E> rmElem = tail;
82+
while( secondLast.next != rmElem){
83+
secondLast = secondLast.next;
84+
tail = secondLast;
85+
System.out.println("tail: "+tail.getElement());
86+
}
87+
remove(tail);
88+
return rmElem.getElement();
89+
}
90+
public void display() {
91+
if (tail == null) {
92+
System.out.print("Circular Linked List is empty!");
93+
return;
94+
}
95+
Node<E> temp = tail;
96+
97+
do{
98+
System.out.print(temp.element + " ");
99+
temp = temp.next;
100+
} while (temp != tail);
101+
System.out.println();
102+
}
103+
104+
}
105+
//main class
106+
public class CircularlyLinkedList<E>{
3107
public static void main(String[] args){
4-
// code here
108+
CircularLikedList<Integer> cll = new CircularLikedList<>();
109+
// cll.addFirst(5);
110+
// cll.addFirst(4);
111+
// cll.addFirst(3);
112+
cll.addFirst(2);
113+
cll.addFirst(1);
114+
cll.addLast(3);
115+
System.out.println("First Element: "+cll.getFirst());
116+
System.out.println("Last Element: "+cll.getLast());
117+
System.out.println("Total Element: "+cll.size());
118+
cll.display();
119+
System.out.println("remove Last Element: "+cll.removeLast());
120+
// String remove = "Removed element: "+cll.removeFirst();
121+
// System.out.println("After remove first element.");
122+
System.out.println("Total Element: "+cll.size());
123+
// System.out.println(remove);
124+
cll.display();
125+
5126
}
6127
}

SignlyLinkedList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public void SignlyLinkedList(){}
77
public int size(){return size;}
88
public boolean isEmpty() {return size==0;}
99

10-
public static class Node<E> {
10+
private static class Node<E> {
1111
private E element;
1212
private Node<E> next;
1313
public Node (E e, Node<E> n){

0 commit comments

Comments
 (0)