Untitled

 avatar
unknown
plain_text
4 years ago
3.1 kB
10
Indexable
import java.util.Scanner;
class Node{
    private int data;
    private Node next;
    public Node(int data) {
        this.data=data;
        this.next=null;
    }
    public int getdata() {
        return this.data;
    }
    public void setdata(int data) {
        this.data=data;
    }
    public Node getnext() {
        return this.next;
    }
    public void setnext(Node temp) {
        this.next=temp;
    }
   
}
public class linklist {
    private Node head,tail;
     int a;
     String b;
    public linklist() {
        this.head=this.tail=null;
    }
    public void insert(int data) {
         if(this.head==null){
             this.head=this.tail=new Node(data);
         }
         else{
             Node n=new Node(data);
             n.setnext(this.head);
             this.head=n;
         }
    }
   public void insertatEnd(int data) {
         if(this.head==null){
             this.head=this.tail=new Node(data);
         }
         else{
             this.tail.setnext(new Node(data));
             this.tail=this.tail.getnext();
         }
    }
    public boolean search(int data){
         Node temp=head;
         while(temp!=null){
            if(temp.getdata()==data){
                return true;
            }
            temp=temp.getnext();
         }
         return false;
    }
    public boolean delete(int data){
           if(this.head.getdata()==data){
                 this.head=this.head.getnext();
                 return true;
           }
           else{
             Node temp=this.head;
             while(temp.getnext()!=this.tail){
                 if(temp.getnext().getdata()==data){
                     temp.setnext(temp.getnext().getnext());
                     return true;
                 }
                 temp=temp.getnext();
             }
             if(this.tail.getdata()==data){
                 temp.setnext(null);
                 this.tail=temp;
                 return true;
             }
             
            
           }
            return false;
    }
    public void display(){
         Node temp=head;
         while(temp!=null){
            System.out.println(temp.getdata());
            temp=temp.getnext();
         }
    }
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter usernamae");
        String user=sc.nextLine();
        System.out.println("usernamae is="+user);
        
        linklist l=new linklist();
        l.insertatEnd(40);
        l.insertatEnd(30);
        l.insertatEnd(20);
        l.insertatEnd(10);
        System.out.println(l.delete(5));
        System.out.println(l.delete(10));
        System.out.println(l.delete(20));
        System.out.println(l.delete(40));
        l.display();
       
        System.out.println(l.a+" "+l.b);
        //System.out.println(l.delete(10));
        //System.out.println(l.delete(20));
        //l.display();
    }
}
Editor is loading...