i have no insert method and still its adding node in linkedlist. code below have no error so you can run to see the problem better.

if u run this it will ask u to enter a command. i only have two commands exit and display. display command will print linklist note it will be empty bc no insert method. but it is adding empty string and int zero. which i dont want.

i think this is bc of empty constructor. but i dont know how to do it so it wont add any thing. any ideas?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class aaa
{
    /*** CREATE NODE ***/
    private class node
    {
        private String name;
        private int age;
        private node next;

        /*** Constructor ***/
        public node()
        {
            name = "";
            age = 0;
            next = null;
        }
        public node(String n, int a)
        {
            name = n;
            age = a;
            next = null;
        }/*** End of constructor ***/
    }/*** End of Node class ***/





    /*** Linked List Constructor ***/
    private node head_node;
    private node current_node;

    public aaa()
    {
        head_node = new node();
        current_node = new node();
    }


    /*** Insert Method ***/
    public void insert(String name, int age)
    {

    }/*** End of Insert Method ***/



    /*** Delete Method ***/

    /*** Display Method ***/
    public void display()
    {
        current_node = head_node;
        System.out.println("name\tage");
        while(current_node != null)
        {
            System.out.println(current_node.name+"\t"+current_node.age);
            current_node = current_node.next;

        }
    }/*** End of Display Method/



    /*** Main Method ***/
    public static void main(String[] args) throws IOException
    {
        aaa m = new aaa();

        String cmd = "";
        int age = 0;
        String name = "";

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a Command: ");
        cmd = br.readLine();
        while(!(cmd.equals("exit")))
        {
            if(cmd.equals("insert"))
            {

            }
            else if(cmd.equals("display"))
            {
                m.display();
            }
            System.out.print("Enter a Command: ");
            cmd = br.readLine();
        }
    }
}

instead of

        while(current_node != null)
        {
            System.out.println(current_node.name+"\t"+current_node.age);
            current_node = current_node.next;

        }

do this :

        while(current_node.next != null) // as the head (which the current node has been assinged ) is a sentinel , and actual data starts from the next node
        {
            System.out.println(current_node.name+"\t"+current_node.age);
            current_node = current_node.next;

        }

also , in your constructor :

    public aaa()
    {
        head_node = new node();
        current_node = new node(); // shouldnt this be current_node = head_node ?
    }

im not sure of the advantage that creating a separate node for the "current_node" gives... especially since your assinging the head to the current node in your display method later on.

i want to make sure some thing:

les say i want to del(3):

i want to make sure some thing:

les say i want to del(3):

head/cur
O---------->|1|---->|2|---->|3|------>|4|------->null

move the curNode before 3.

head
O------->|1|----->|2|----->|3|----->|4|------->null
                   ^
                   |
               Cur O

del = curNode.next;

head
    O------->|1|----->|2|----->|3|----->|4|------->null
                       ^        ^
                       |        |
                   Cur O    del O

curNode.next = CurNode.next.next;

                        -----------------|
  head                 |                 V
    O------->|1|----->|2|      |3|----->|4|------->null
                       ^        ^
                       |        |
                   Cur O    del O

delNode = null;

does this looks right?

u had "del" and "delNode" , that was a bit confusing... basically , what you have to do is :

temp = curr.next.next; // stores address of node pointed to by the node you want to delete
curr.next.next = null; // node_to_delete now points to null
curr.next = temp; // node prior to the one to delete now points to the node originally pointed to by the deleted node.

thanks got it to working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.