Question

I have the following code for a singly linked list, but the value I insert is made only once, how to make the user give say for example 5 values at different position? Is what I did right, and can anyone make it clear?

class Node1{
    private String data;
    private Node1 nextNode;
    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
        insertFirst(data);
    }

    public Node1 getNextNode() {
        return nextNode;

    }
    public void setNextNode(Node1 nextNode) {
        this.nextNode = nextNode;
        insert("deena",1);
    }

    public void insertFirst(String data){

        insert(data, 0);

    }
    public void insertLast(String data){
        // Logic for insert last
    }
    public void insert(String data, int position){
        System.out.println("data   "+data);
        System.out.println("position   "+position);

        }

    public static void main(String[] arg){
        String j;
        Node1 n = new Node1();
        System.out.println("Enter the name to be inserted");
        Scanner d=new Scanner(System.in);
        j=d.next();
        n.insertFirst(j);
        }
    }
Was it helpful?

Solution

how about something like this

Link.java

public class Link
   {
   public long dData;                 // data item
   public Link next;                  // next link in list
// -------------------------------------------------------------
   public Link(long d)                // constructor
      { dData = d; }
// -------------------------------------------------------------
   public void displayLink()          // display this link
      { System.out.print(dData + " "); }
// -------------------------------------------------------------
   }  // end class Link

FirstLastList.java

public class FirstLastList {
    private Link first; // ref to first link
    private Link last; // ref to last link
    // -------------------------------------------------------------

    public FirstLastList() // constructor
    {
        first = null; // no links on list yet
        last = null;
    }

    // -------------------------------------------------------------
    public boolean isEmpty() // true if no links
    {
        return first == null;
    }

    // -------------------------------------------------------------
    public void insertFirst(long dd) // insert at front of list
    {
        Link newLink = new Link(dd); // make new link

        if (isEmpty()) // if empty list,
            last = newLink; // newLink <-- last
        newLink.next = first; // newLink --> old first
        first = newLink; // first --> newLink
    }

    // -------------------------------------------------------------
    public void insertLast(long dd) // insert at end of list
    {
        Link newLink = new Link(dd); // make new link
        if (isEmpty()) // if empty list,
            first = newLink; // first --> newLink
        else
            last.next = newLink; // old last --> newLink
        last = newLink; // newLink <-- last
    }

    // -------------------------------------------------------------
    public long deleteFirst() // delete first link
    { // (assumes non-empty list)
        long temp = first.dData;
        if (first.next == null) // if only one item
            last = null; // null <-- last
        first = first.next; // first --> old next
        return temp;
    }

    // -------------------------------------------------------------
    public void displayList() {
        System.out.print("List (first-->last): ");
        Link current = first; // start at beginning
        while (current != null) // until end of list,
        {
            current.displayLink(); // print data
            current = current.next; // move to next link
        }
        System.out.println("");
    }
    // -------------------------------------------------------------
}

FirstLastApp.java

public class FirstLastApp
{
public static void main(String[] args)
   {                              // make a new list
   FirstLastList theList = new FirstLastList();

   theList.insertFirst(22);       // insert at front
   theList.insertFirst(44);
   theList.insertFirst(66);

   theList.insertLast(11);        // insert at rear
   theList.insertLast(33);
   theList.insertLast(55);

   theList.displayList();         // display the list

   theList.deleteFirst();         // delete first two items
   theList.deleteFirst();

   theList.displayList();         // display again
   }  // end main()
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top