Delete an element at the starting of linked list in java.
Code link π -> click ππππ
https://www.programiz.com/online-compiler/7xP5aBU4Orcx8
■》Program->
class Node{
int val;
Node next;
Node(int val){
this.val=val;
}
}
class Sll{
Node head;
Node tail;
int size;
void insertAtEnd(int val){
Node temp = new Node(val);
if(head==null) head=tail=temp;
else
{
tail.next=temp;
tail=temp;
}
size++;
}
void display(){
Node temp=head;
while(temp!=null) {
System.out.print(temp.val+" ");
temp=temp.next;
}
System.out.println();
}
void size(){
System.out.println("size is= "+size);
}
void deleteAtStart()throws Error{
if(head==null){
throw new Error("empty linked list");
}
head=head.next;
size--;
}
}
public class Implement {
public static void main(String[] args) {
Sll list = new Sll();
list.insertAtEnd(5);
list.insertAtEnd(6);
list.insertAtEnd(7);
list.insertAtEnd(8);
list.display();
System.out.println("after delete linked list is=");
list.deleteAtStart();
list.display();
}
}
■》 output->
5 6 7 8
after delete linked list is=
6 7 8
Comments
Post a Comment