Jump to content

How to delete a string in my list??

- - - - -

  • Please log in to reply
1 reply to this topic

#1
sxlend

sxlend

    Newbie

  • Members
  • Pip
  • 1 posts
my question is here

You are required to implement a linked list ADT that contains only one reference to the front of the list. Each node should be able to keep a generic Object type. Your implementation should at least include the following operations:

 Add a new item to the list
 Remove a specified item from the list
 Retrieve a specified item from the list
 Make the list empty
 Check whether the list is empty
 Other operations deemed necessary

You are also required to implement an Iterator for your linked list.

Part 2: The Application Code

Design an application code that keeps a list of inventory items using the linked list ADT that you have implemented above. Implement a class of your choice to represent your inventory items such as Book, Stationary, Grocery, Hardware Item, etc. Provide a menu to allow the user to repeatedly perform operations such as

 Add Item
 Delete Item
 View an Item’s Information
 Display all Item’s Information
 etc

code for function
import javax.swing.JOptionPane;

import java.util.*;


public class List

{

    Node head;

    

    List()

    {

        head = null;

    }

    

    void addItem(Object item)

    {

        Node newnode = new Node(item);

        newnode.setLink(head);

        head = newnode;

        JOptionPane.showMessageDialog(null,"Item sucessful added!");

    }

    

    void deleteItem(String target)

    {

        Node current = head;

        Node lastCurrent = current;

        Data info = (Data)current.item; 

        if (current == null)

        {

            JOptionPane.showMessageDialog(null, " No item in the list");

        }

        else { 

            while(info.getItem() != target&& current.getLink()!=null)

            {

                lastCurrent= current;

                current = current.getLink();

            }

            if (info.getItem() == target)

            {

                lastCurrent.setLink(current.getLink()) ;                

                JOptionPane.showMessageDialog(null,"Item sucessful deleted!"); 

                

            }

            else

            {

                JOptionPane.showMessageDialog(null, " Item not found in the list"); 

            }

        }

    }

       

    

    public void count()

    {

        int count =1;

        Node current = head;

        if (head==null)

        {

            JOptionPane.showMessageDialog(null, " The list is empty"); 

        }

        else

        {

            while( current.getLink()!= null)

            {

                count++;

                current = current.getLink();

            }

            JOptionPane.showMessageDialog(null, " The list have " + count +" element");

            

            } 

        }

    

    void searchItem(String target)

    {

        Node current = head;

        Node lastCurrent;

        Data info = (Data)current.item; 

        if (current == null)

        {

            JOptionPane.showMessageDialog(null, " No item in the list");

        }

        else { 

            while(info.getItem() != target && current.getLink() !=null)

            {

                lastCurrent= current;

                current = current.getLink();

                info = (Data)current.item;

            }

            if ( info.getItem() == target)

            {

                JOptionPane.showMessageDialog(null,"\n\nItem : " +info.getItem()+" \nOwner : "+info.getOwner()+"\nPrice : RM" +info.getPrice());

                

            }

            else

            {

                JOptionPane.showMessageDialog(null, " Item not found in the list"); 

            }

        }

    }

        

    public boolean isEmpty()

    {

        return head == null;

    }

    

    

    public Iterator getListIterator()

    {

        return new IteratorForList();

    }

    

    class IteratorForList implements Iterator

    {

        private Node current;

        

        public IteratorForList()

        {

            current = head;

        }

        

        public boolean hasNext()

        {

            return current != null;

        }

        

        public Object next() throws NoSuchElementException // display()  //represent by next()

        {

            if(hasNext())

            {

                Node returnNode = current;

                current = current.getLink();

                return returnNode.getItem();

            }

            else

            {

                return null;

            }

        }

        

        public void remove() 

        {

            head=null;

        }

    }        

    

    void display()

    {

        Node current = head;

        

        while(current != null)

        {

            System.out.println(current.getItem()); //Displaying by calling the toString()

            Data info = (Data)current.item; //Casting to an object

            current = current.getLink();

        }

    }

}
code for data
public class Data

{

    String item;

    String owner;

    int price;

    

    Data(String item,String owner, int price)

    {

        this.owner = owner;

        this.item = item;

        this.price = price;

    }

    

    Data(Data e)

    {

        this.item = e.item;

        this.owner = e.owner;

        this.price = e.price;

    }

    

    String getItem()

    {

        return item;

    }

    

    String getOwner()

    {

        return owner;

    }

    

    int getPrice()

    {

        return price;

    }

    

    public String toString()

    {

        return "\n\nItem : " +item+" \nOwner : "+owner+"\nPrice : RM" +price;

    }

}
code for client
import java.util.*;

import javax.swing.JOptionPane;


public class Client

{

    public static void main(String args[])

    {

        List list = new List();

        String item = " ";

        String owner = " ";

        int price;

        int options = 0;

        Iterator iterate = list.getListIterator();

        

        while (options !=7){

            options = Integer.parseInt(JOptionPane.showInputDialog("Welcome to my item database! \n Please select your options \n1.Add an item\n2.Delete an item\n3.Search an item\n4.Display all item\n5.Delete all item\n6.Check the ammount of the element\n7.Quit"));

            switch(options)

            {

                case 1 : {

                    item = JOptionPane.showInputDialog("Please enter the Item name : ");

                    owner = JOptionPane.showInputDialog("Please enter the owner name : ");

                    price = Integer.parseInt(JOptionPane.showInputDialog("Please enter the price of item : "));

                    Data itemData = new Data(item, owner, price);

                    list.addItem(itemData);

                    break;

                }

                case 2 :{

                    if (list.isEmpty() == true)

                    {

                        JOptionPane.showMessageDialog(null,"There is no item in the list");

                    } else

                    {

                        item = JOptionPane.showInputDialog("Please enter the Item name : ");

                        list.deleteItem(item);

                    }

                        break;

                }

                        

                

                case 3: {

                    item = JOptionPane.showInputDialog("Please enter the Item name : ");

                    list.searchItem(item);

                    break;

                }

                

                case 4 : {

                    list.display();

                    break;

                }

                

                case 5 : {

                    iterate.remove();

                    break;

                }

                

                case 6 : {

                    list.count();

                    break;

                }

                

                case 7 : {

                    break;

                }

                

                default : {

                    JOptionPane.showMessageDialog(null,"Invalid Input! Please try again");

                    break;

                }

            }

        }

            JOptionPane.showMessageDialog(null,"Thank you for using my system");

    }

}
in the result,i cannot delete either search the item

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Do you have your own Node class?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users