/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.linkedlistdemo;
import javax.swing.*;
/**
 *
 * @author zulfi
 */
public class LinkedList extends javax.swing.JFrame {
   
    Node head;
    
    public void add(int data) {
        JOptionPane.showMessageDialog(null, "Inside ADD data = " + data+ " ");
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }
    
    public String printList() {
        Node temp = head;
        String res ="";
        while (temp != null) {
            //JOptionPane.showMessageDialog(null, "temp.data="+temp.data + " ");
            res = res + temp.data + " ";
            temp = temp.next;
        }
        //JOptionPane.showMessageDialog(null, "Res= " + res + " ");
        return res;
    }
    
    public boolean delete(int key) {
        Node temp = head, prev = null;
        if(temp == null)
            JOptionPane.showMessageDialog(null,"List is empty");
        
        if (temp != null && temp.data == key) {
            head = temp.next;
            return true;
        }
        
        while (temp != null && temp.data != key) {
            prev = temp;
            temp = temp.next;
        }
        
        if (temp == null) return false;
        
        prev.next = temp.next;
        return true;
    }
}
    

