0

I don't know how to write function search and delete by name in linked list. I have problems when I write the function to delete the employee by input of the employee's name. I still have the same problem with the search function. And as required i cannot use Array list. Can anybody help me please? Thank you so much.

Here is my source code :

import java.util.Scanner;
import java.io.Serializable;

/*  Class Node  */

class Employee implements Serializable{
    int ID;
    String name;
    String address;

    Employee(int emp_ID, String emp_name, String emp_address){
        ID = emp_ID;
        name = emp_name;
        address = emp_address;
    }
    public void print(){
        System.out.println(ID);
        System.out.println(name);
        System.out.println(address);

    }
    @Override
    public String toString() {
        return ID + "-" + name + "-" + address;
     }

}

class Node 
{
    protected Employee emp;
    protected Node link;
    public Object name;
    public Node in;

    /*  Constructor  */
    public Node()
    {
        link = null;
        emp = null;
    }    
    /*  Constructor  */
    public Node(Employee e,Node n)
    {
        emp = e;
        link = n;
    }    
    /*  Function to set link to next Node  */
    public void setLink(Node n)
    {
        link = n;
    }    
    /*  Function to set data to current Node  */
    public void setData(Employee e)
    {
        emp = e;
    }    
    /*  Function to get link to next node  */
    public Node getLink()
    {
        return link;
    }    
    /*  Function to get data from current Node  */
    public Employee getData()
    {
        return emp;
    }
}



/* Class linkedList */
class linkedList
{
    protected Node start ;
    protected Node end ;
    public int size ;

    /* Constructor */
    public linkedList()
    {
        start = null;
        end = null;
        size = 0;
    }
    /* Function to check if list is empty */


    /* Function to get size of the list */

    /* Function to insert element at the begining */
    public void insertAtStart(Employee e)
    {
        Node nptr = new Node(e,null);    
        nptr.setLink(start);
        if(start == null)
        {            
            start = nptr;
            nptr.setLink(start);
            end = start;            
        }
        else
        {
            end.setLink(nptr);
            start = nptr;        
        }
        size++ ;
    }

    public void searchByName(){

    }

    public void deleteByName(){

    }

}



/* Class CircularSinglyLinkedList */
public class CurrilarLinkedList
{    
    public static void main(String[] args)
    {    


        int ID = 0;
        String name = null;
        String address = null;
        Employee emp = null;
        Scanner scan = new Scanner(System.in);
        /* Creating object of linkedList */
        linkedList list = new linkedList(); 
        System.out.println("Circular Singly Linked List Test\n");          
        char ch;
        /*  Perform list operations  */
        do
        {
            System.out.println("\nCircular Singly Linked List Operations\n");
            System.out.println("1. insert at begining");
            System.out.println("2. delete by name");
            System.out.println("3. search by name");
            int choice = scan.nextInt(); 

            switch (choice)
            {
            case 1 : 
                System.out.print("Please input an Employee \n");
                Scanner myScanner = new Scanner(System.in);
                System.out.println("Please input an Employee ID");
                ID = myScanner.nextInt();
                myScanner.nextLine();
                System.out.println("Please input an Employee Name");
                name = myScanner.nextLine();
                System.out.println("Please input an Employee Address");
                address = myScanner.nextLine();
                emp = new Employee(ID,name,address);
                list.insertAtStart(emp);                     
                break;                          
            case 2 :                 
                break;                         
            case 3 : 
                break;                                          
            default : 
                System.out.println("Wrong Entry \n ");
                break;   
            }

            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);            
        } while (ch == 'Y'|| ch == 'y');                    
    }

}
ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
  • Possible duplicate of https://stackoverflow.com/questions/18852059/java-list-containsobject-with-field-value-equal-to-x – Feras Al Sous Aug 13 '17 at 11:24

1 Answers1

-1

You can find a proper implementation in the following book. I put here as is in the book.

Data Structures and Algorithms in Java™ Sixth Edition

Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser

/∗∗ A basic doubly linked list implementation. ∗/
public class DoublyLinkedList < E > {
        //---------------- nested Node class ----------------
        private static class Node < E > {
            private E element;
            private Node < E > prev;
            private Node < E > next;
            public Node(E e, Node < E > p, Node < E > n) {
                element = e;
                prev = p;
                next = n;
            }
            public E getElement() {
                return element;
            }
            public Node < E > getPrev() {
                return prev;
            }
            public Node < E > getNext() {
                return next;
            }
            public void setPrev(Node < E > p) {
                prev = p;
            }
            public void setNext(Node < E > n) {
                next = n;
            }
        } //----------- end of nested Node class -----------
        // instance variables of the DoublyLinkedList
        private Node < E > header;
        private Node < E > trailer;
        private int size = 0;
        /∗∗ Constructs a new empty list. ∗/
        public DoublyLinkedList() {
            header = new Node < > (null, null, null);
            trailer = new Node < > (null, header, null);
            header.setNext(trailer);
        }
        // reference to the element stored at this node // reference to the previous node in the list
        // reference to the subsequent node in the list
        /∗∗ Returns the number of elements in the linked list. ∗/
        public int size() {
            return size;
        }
        /∗∗ Tests whether the linked list is empty. ∗/
        public boolean isEmpty() {
            return size == 0;
        }
        /∗∗ Returns (but does not remove) the first element of the list. ∗/
        public E first() {
            if (isEmpty()) return null;
            return header.getNext().getElement(); // first element is beyond header }
            /∗∗ Returns (but does not remove) the last element of the list. ∗/
            public E last() {
                if (isEmpty()) return null;
                return trailer.getPrev().getElement(); // last element is before trailer 
            }

            // public update methods
            /∗∗ Adds element e to the front of the list. ∗/
            public void addFirst(E e) {
                addBetween(e, header, header.getNext());
            }
            /∗∗ Adds element e to the end of the list. ∗/
            public void addLast(E e) {
                addBetween(e, trailer.getPrev(), trailer);
            }
            /∗∗ Removes and returns the first element of the list. ∗/
            public E removeFirst() {
                if (isEmpty()) return null; // nothing to remove
                return remove(header.getNext()); // first element is beyond header
            }
            /∗∗ Removes and returns the last element of the list. ∗/
            public E removeLast() {
                    if (isEmpty()) return null;
                    return remove(trailer.getPrev());
                }
                // private update methods
            /∗∗ Adds element e to the linked list in between the given nodes. ∗/
            private void addBetween(E e, Node < E > predecessor, Node < E > successor) {
                // create and link a new node
                Node < E > newest = new Node < > (e, predecessor, successor);
                predecessor.setNext(newest);
                successor.setPrev(newest);
                size++;
            }
            /∗∗ Removes the given node from the list and returns its element. ∗/
            private E remove(Node < E > node) {
                Node < E > predecessor = node.getPrev();
                Node < E > successor = node.getNext();
                predecessor.setNext(successor);
                successor.setPrev(predecessor);
                size−−;
                return node.getElement();
            }
        } //----------- end of DoublyLinkedList class -----------
ugur
  • 3,604
  • 3
  • 26
  • 57