singly circular linked list deletion at last position
import java.util.Scanner;
public class cll {
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
Node head=null;
Node tail=null;
public void toadd(int data){
Node newnode=new Node(data);
if (head==null){
head=newnode;
tail=newnode;
tail.next=head;
}
else{
tail.next=newnode;
tail=newnode;
tail.next=head;
}
}
public void dis(){
Node temp=head;
if (head==null){
System.out.println("empty");
}
else{
do {
System.out.println(temp.data);
temp=temp.next;
}while (temp!=head);
}
}
public void dellast(){
Node temp=head;
Node current=temp.next;
while(current.next!=head){
temp=current;
current=current.next;
}
temp.next=head;
tail=temp;
} public static void main(String[] args) {
cll cc=new cll();
cc.toadd(10);
cc.toadd(20);
cc.toadd(30);
cc.toadd(40);
cc.dis();
System.out.println("*******");
cc.dellast();
cc.dis();
}
}
Comments
Post a Comment