Skip to content

Commit f52cc43

Browse files
commit to sll
1 parent 36de7c5 commit f52cc43

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

SignlyLinkedList.java

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,107 @@
1-
public class SignlyLinkedList<E>{
1+
class SignlyLL<E>{
2+
private Node<E> head = null;
3+
private Node<E> tail = null;
4+
private int size = 0;
5+
public void SignlyLinkedList(){}
6+
// access methods
7+
public int size(){return size;}
8+
public boolean isEmpty() {return size==0;}
29

10+
public static class Node<E> {
11+
private E element;
12+
private Node<E> next;
13+
public Node (E e, Node<E> n){
14+
element = e;
15+
next =n;
16+
}
17+
public E getElement(){
18+
return element;
19+
}
20+
public Node<E> getNext(){
21+
return next;
22+
}
23+
public void setNext(Node<E> n){
24+
next = n;
25+
}
26+
}
27+
28+
public void display() {
29+
Node<E> temp = head;
30+
while (temp != null) {
31+
System.out.print(temp.element + " ");
32+
temp = temp.next;
33+
}
34+
System.out.println();
35+
}
36+
public E first(){
37+
if (isEmpty()) {
38+
return null;
39+
}
40+
return head.getElement();
41+
}
42+
public E last(){
43+
if (isEmpty()) {
44+
return null;
45+
}
46+
return tail.getElement();
47+
}
48+
//upadate methods
49+
public void addFirst(E e){
50+
head = new Node<>(e, head);
51+
if (size==0) {
52+
tail=head;
53+
}
54+
size++;
55+
}
56+
public void addLast(E e){
57+
Node<E> newest = new Node<>(e,null);
58+
if (isEmpty()) {
59+
head = newest;
60+
}
61+
else{
62+
tail.setNext(newest);
63+
}
64+
tail = newest;
65+
size++;
66+
}
67+
public E removeFirst(){
68+
if (isEmpty()) {
69+
return null;
70+
}
71+
E answer = head.getElement();
72+
head = head.getNext();
73+
size--;
74+
if (size==0) {
75+
tail=null;
76+
}
77+
return answer;
78+
}
79+
}
80+
public class SignlyLinkedList{
381
public static void main(String[] args){
4-
System.out.print("print");
82+
// SignlyLL<Integer> sll = new SignlyLL<>();
83+
// sll.addFirst(5);
84+
// sll.addFirst(6);
85+
// sll.addFirst(7);
86+
// sll.addFirst(8);
87+
// sll.addFirst(9);
88+
// sll.addFirst(10);
89+
// sll.addFirst(4);
90+
// System.out.println("Head"+sll.first());
91+
// System.out.println("Tail: "+sll.last());
92+
// System.out.println("Total Element: "+sll.size());
93+
// System.out.println("All element in this SLL list");
94+
// sll.display();
95+
SignlyLL<String> sll = new SignlyLL<>();
96+
sll.addFirst("Hello");
97+
sll.addFirst("Rajendra");
98+
sll.addFirst("Pancholi");
99+
sll.addFirst("!");
100+
sll.addLast("last element.");
101+
System.out.println("Head Element: "+sll.first());
102+
System.out.println("Tail Element: "+sll.last());
103+
System.out.println("Total Element: "+sll.size());
104+
System.out.println("All element in this SLL list");
105+
sll.display();
5106
}
6107
}

0 commit comments

Comments
 (0)