Is my code a Linked Lists or not?

Hello everyone,
[I hope I am in the right forum]
I am trying to learn how to implement Linked Lists with Java. I have written a small code and I would appreceate it a lot if any one would be kind enough to check if this code is really a linked list.
I tried to make a list with 3 nodes, with no helping methods for adding and removing nodes. Just a simple example. This is my code:
public class myList{
     public static final long serialVersionUID = 24362462L;
     //node pointer important to define private, so not share same value
     private myList pointer;
     //node data
     private String nodeData;
public static void main(String args[]){
//Give memory to nodes
myList Node1 = new myList();
myList Node2 = new myList();
myList Node3 = new myList();
//Make Node1
Node1.pointer = Node2;//give value to pointer
Node1.nodeData = "Hi i am data contained in Node 1.";
//Make Node2
Node2.pointer = Node3;
Node2.nodeData = "Hi i am data contained in Node 2.";
//Make Node3
Node3.pointer = null;
Node3.nodeData = "Hi i am data contained in Node 3.";
//Display Data
System.out.println(Node1.nodeData);
System.out.println(Node2.nodeData);
System.out.println(Node3.nodeData);
//Display pointers
System.out.println("Hi this is Node2 ==============>:"+Node2);
System.out.println("This is the value of pointer of Node1:"+Node1.pointer);
System.out.println("Hi this is Node3===============>:"+Node3);
System.out.println("This is the value of pointer of Node2:"+Node2.pointer);
}//main
}//class
/***** OUTPUT ***** OUTPUT ***** OUTPUT ***** OUTPUT *****
Hi i am data contained in Node 1.
Hi i am data contained in Node 2.
Hi i am data contained in Node 3.
Hi this is Node2 ========================>:myList@16f0472
This is the value of pointer of Node 1 ==>:myList@16f0472
Hi this is Node3 ========================>:myList@18d107f
This is the value of pointer of Node 2 ==>:myList@18d107f
Press any key to continue...
Thank you very much,
JMelsi

Happy to advise. Here we go. :)
Firstly, you'll want to post your code in code tags. They make your code more legible, thus making it easier and therefore more likely that someone responds. ;)
Second, it would be more appropriate for class names (such as "myList") to start with an upper-case letter (such as "MyList"). Additionally, the class in question does not represent a whole list but instead a node, so you might want to call it "MyListNode" or something similar.
Strictly speaking, your code is definitely that of a linked list of nodes. For example, one could print all of the contents in your list with code like this:
myList node = ...; // assign to the first node
while (node!=null) // as long as there is a node
    System.out.println(node.nodeData); // print the data in this node
    node = node.pointer; // move on to the next node
}Note that, in the above code, I didn't have to know how many nodes were in your list or have any references to them beforehand. :) That's the power in the data structure you're creating.
Enjoy!

Similar Messages

  • Link List Explorer NOT Opening In A New Window

    Hi All,
    I am trying to force my KM linklist links to open in a new window and this is proving to be a hard feat!
    I have navigated to System Admin --> System Config --> Knowledge Management --> Content Management --> Configuration --> Content Management --> User Interface --> Settings --> Resource Renderer Settings --> Link List Resource Renderer.
    Within this area, I have changed the Target Window Type to "blank" and "fixed" amongst other options but it is not opening the new links in a blank window.
    Instead, it opens the KM folder in a new window and when I click on a link within that folder, it opens in a new portal window complete with Portal framework.
    All I want is for my links to open in a new blank window with no Portal framework.
    Any ideas?
    Thanks,
    Bim.

    Hi,
    It's known bug. this issue fixed in 11.1.1.7.0 (coming soon).
    bug 13852263 - ACTION LINKS - NAVIGATION TO BI REPORTS DOESN'T WORK ( OPEN IN NEW WINDOW)
    for now : to uncheck the "Open in New Window" checkbox and go to the detail report in the same Window.
    Note: this bug has been fixed by applying the patch 14003822 (u need to get the latest patch)
    Thanks
    Deva
    Edited by: Devarasu R on Feb 14, 2013 4:05 PM

  • Why LinkedList uses doubly linked list and not single link list

    Hi,
    Please help me to understand the concept behind java using doubly linked list for LinkedList and not single link list?
    Edited by: user4933866 on Jun 26, 2012 11:22 AM
    Edited by: user4933866 on Jun 26, 2012 11:25 AM
    Edited by: EJP on 27/06/2012 08:50: corrected title to agree with question

    EJP wrote:
    Could you help me with one real world use case where we need to traverse linklist and arraylist in backward direction other than printing the linklist or arraylist in reverse order. One example is that you might want to use it as a stack. For a very large (+very+ large) stack it would out-perform the Vector-based Stack in the API.The first thing that pops into my head is an undo/redo function for a text editor. Each edit action is dumped onto such a stack. Each time you hit CTRL+Z to undo an action you go back in the stack. Each time you hit CTRL+Y to redo the action you go forward again.

  • Organizing entries in link list shall not be possible for standard users

    Hello,
    how can I avoid that a standard user can organize the entries in the iView <i>Links List</i>?
    That means the link 'Organize Entries' shall not be shown for them.
    Only the content manager shall be able to organize.
    Regards,
    Susanne

    I got the solution by myself:
    In the KM repositories click on the context menue of /documents/links and choose 'Details'.
    In the opened window choose Settings > Permissions and change the permission you like.

  • Links list to open in new tab

    I have created a links list and added that webpart to a page in office 365 for sharepoint. When the user clicks on a links it needs to be opened in new tab how to impletement.
    I tried with javascript content editor webpart but it is removing  the  scroll bar of the page.
    Please let me know. many thanks in advance
    Regards,
    Chaitanya

    Hi Chaitanya,
    May check this article to find the similar code in Links list web part with SharePoint Designer, add the attribute target="_blank" within the code, then check results.
    http://365.webbrewers.com/blog/Lists/Posts/Post.aspx?ID=9
    Thanks
    We
    are trying to better understand customer views on social support experience, so your participation in this
    interview project would be greatly appreciated if you have time.
    Thanks for helping make community forums a great place.

  • New to java  -especially linked lists

    I�m trying to work with linked lists but not having much luck�
    Overall, the program must create a datastructure that will hold titles of articles. Up to 10 keywords can be added to each article and it must then be possible to search all of the titles for matches to the keyword.
    Currently I�m stuck in a for-loop � my linked list (articleList) is not accepted. Any help would be much appreciated. The following error occurs when I attempt to compile:
    SearchMachine.java:30: cannot resolve symbol
    symbol : variable articleList
    location: class SearchMachine
              for(ListIterator listiter = articleList.listIterator(); listiter.hasNext();)
    ^
    import java.util.*;
    class Document
         private String title;
         private String keywords [] = new String [10];
         private int last = 0;
         public Document (String _title)
              String title = _title;
         public void addKeyword(String kw)
              if(last < 9)
                   keywords [last]= kw;
                   last ++;
                   System.out.println("The keyword " + kw + " has now been added");
              else if (last == 9)
                   keywords [last]= kw;
                   System.out.println ("The document now has 10 keywords. No more can be added");
    class SearchMachine
         SearchMachine()
              LinkedList articleList = new LinkedList ();
              articleList.add (new Document ("Oracle 8 the new SQL programming"));
              articleList.add (new Document ("Java programming made easy"));
         public void print()
              for(ListIterator listiter = articleList.listIterator(); listiter.hasNext();)
                   System.out.println(listiter.next());
         public static void main (String [] args)
              SearchMachine search;     
              search = new SearchMachine();
              search.print();

    This is a basic scope issue. You declare the variable articleList inside the constructor block and thus it's visible only in the constructor block. The variable doesn't exist anywhere else but in SearchMachine() {...} The solution is to move the line "LinkedList articleList = new LinkedList();" three lines up, outside the constructor but inside the class definition, so that articleList becomes a member of the SearchMachine class.

  • Tax code and price list not appear automatic

    Hi My Dear,
    i work in R12.1.1 (solution beacon)
    -I assigned tax out classification code to an item from the master item,when navigate to sales order and enter the item,the tax code dose not joined with item ,it should be selected from the LOV
    - also I linked the price list to a specific customer and an order type ,when navigate to sales order header and select the customer and order type the price list dose not appear automatic
    i should click on the price list field to appear.
    thanks

    Check the defaulting rules for the order/order line.

  • In firefox 12 in page source code all links 404 not found! base tag ignored!

    in firefox 12 in page source code all links 404 not found! base tag ignored!

    I filed bug 757348 for you:
    https://bugzilla.mozilla.org/show_bug.cgi?id=757348
    If you want to be notified about changes, please add yourself to the CC list of the bug by creating a new bugzilla account: http://bugzilla.mozilla.org/createaccount.cgi
    Then go to the bug page shown above and hit 'Save changes' in order to add yourself to the CC list.
    --Tobbi

  • Help with linked list code.

    Here's a basic linked list code that I need help on.
    What I need assistance with:
    1. How would I rewrite and delete words/phrases in the file IO section of my code?
    2. How would I make the code recognize all the words on each line of the linked list? Only the words on the first line of the file go into the linked list.
    import java.util.LinkedList;
    import java.io.*;
    import javax.swing.JOptionPane;
    public class LinkedLists {
        public static void delete (LinkedList test)
            int a = Integer.parseInt(JOptionPane.showInputDialog(null,
            "Enter a position to delete"));
            test.remove(a - 1);
            System.out.print(test);
        public static void add (LinkedList test)
            String user = JOptionPane.showInputDialog(null, "What to input?");
            int b = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "What position to enter it in?"));
            test.add(b - 1, user);
            System.out.print(test);
        public static void fileRead(LinkedList test) throws IOException
            BufferedReader fileIn;
            String text;
            fileIn = new BufferedReader(new FileReader("z:/data.txt"));
            text = fileIn.readLine();
            fileIn.close();
            int b = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "What position to enter it in?"));
            test.add(b - 1, text);
            System.out.print(test);
        public static void fileWrite(LinkedList test) throws IOException
            PrintWriter fileOut;
            fileOut = new PrintWriter(new FileWriter("z:/data.txt",true));
            String user = JOptionPane.showInputDialog(null, "What to input?");
            fileOut.println(user);
            fileOut.close();
            fileRead(test);
        public static void main(String[] args)  {
            LinkedList test = new LinkedList();
            test.addLast("Fly");
            test.addLast("money");
            test.addLast("awesome");
            test.addLast("woot");
            test.addLast("yay");
            System.out.print(test);
            System.out.println();
            int x = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Enter \n1 to Delete \n2 to Add \n3 to read data from file \n4 to write into the file and add to list"));
            if (x == 1) {
                delete(test);
            if (x == 2) {
                add(test);
            if (x == 3) {
                try {
                    fileRead(test);
                } catch (IOException e) {
                    e.printStackTrace();
            if(x==4)
                try {
                    fileWrite(test);
                } catch (IOException e) {
                    e.printStackTrace();
    }Edited by: Johnston on Sep 16, 2007 1:13 AM

    Hi,
    Johnston wrote:
    1. How would I rewrite and delete words/phrases in the file IO section of my code?You want to replace or remove in/from the file?
    First you have to define a file format. This is not a Java technical term, but a thing what you have to keep in mind. Simplest format is:
    - each ListItem is one line in the file closed with newline.
    Now you can read the file line by line and store the lines in a memory structure (LinkedList for ex ;-)). Then replace or remove the designated elements an write the file new by iterate over the list and write each item as line in the file.
    Johnston wrote:
    2. How would I make the code recognize all the words on each line of the linked list? Only the words on the first line of the file go into the linked list.You have to read the file line by line and then add each line as item of the list.
    Examples for reading/writing textfiles:
    http://www.exampledepot.com/egs/java.io/pkg.html#Reading%20and%20Writing
    Btw: Deal with Generics (see http://java.sun.com/docs/books/tutorial/extra/generics/index.html) an use this knowledge to declate your List better.
    greetings
    Axel

  • Doubly Link List code

    Hi,
    I have got the following code for doubly link list from a book:
    public Link deleteFirst ( ) {
    Link temp=First;
    If(first.next==null)
    last = null;
    else
    first.next.previous=null;
    first=first.next;
    return temp;
    In the above if condition, when we one element in the list, I feel that first should also point to null. If I code it in this way,is it correct?
    if(first.next==null)
    last=null;
    first= null;
    return temp;
    else {
    first.next.previous=null;
    first=first.next;
    return temp;
    Is the modified code correct?
    Zulfi.

    Your modification gives the correct result, but so does the original even though it might not be immediately obvious. Your version is definitely easier to read :)
    Here's the original broken in parts:// temp now points to first
    Link temp=first;
    // if there is only one element
    if (first.next == null) {
        // the last element should be null because it's removed
        last = null;
    } else {
        // else break the link from the second element to the first
        first.next.previous=null;
    // update "first" to the second element in the list.
    // NOTICE: if the list has only one element and it's properly
    // constructed this will set "first" to null!
    first=first.next;
    // return the removed element
    return temp;Actually neither implementations are perfect; they don't check if you are trying to remove the first element of an empty list...

  • How to implement the RemoveLast function in a link list? check my code plz

    Hi guys,
    How to delete the last element in a user defined linklist. I am using the following code to find the last element of the list. It works fine, but i don't know how to remove that element ...
    public Object getRear()
                   rear=front;
                   while (rear !=null)
                        if (rear.getNext()!=null)
                             rear=rear.getNext();
                        else
                             break; //rear Node found
                   if (rear !=null) //list is not empty
                        return rear.getData();
                        return null; //list is empty
         }

    Sorry, I missed out on returning the last element:
    public Object delRear() {
      // I assume front is an instance field pointing to the first element
      if(front != null) {  // list contains something
        Element  rear, pred;  // I assume you have some type like this
        // determine predecessor of last element
        rear = front;
        pred = null;
        while(rear.getNext() != null) {
          pred = rear;
          rear = rear.getNext();
        if(pred == null)  front = null;  // delete only element
        else  pred.setNext(null);  // delete last element
        return  rear; // return deleted element
      else  return  null;

  • HT201210 what is error code (1)?  it is not listed but this is the error i get trying to restore my 3gs.

    what is error code (1)?  it is not listed but this is the error i get trying to restore

    Take a look here for Apple's suggestions on that error:
    http://support.apple.com/kb/TS3694#error1
    Regards.

  • How do you do anchors - the three ways listed did not work.  I am using the jQuery mobile template 11.  I want to link to a spot on the same page.

    How do you do anchors - the three ways listed did not work.  I am using the jQuery mobile template 11.  I want to link to a spot on the same page.

    How do you do anchors - the three ways listed did not work.  I am using the jQuery mobile template 11.
    You have aroused my curiousity, what are three ways listed that do not work? At risk of being labeled as an ignoramus, could you also tell me where to get the other 10 templates?
    I usually give an element an ID and use that in my link as in
    <a href="#mySpot">Go to my spot</a>
    <div id="mySpot">
    </div>

  • Not to hard for you java types.  Simply creating a doubly linked list.

    How do i go about declaring a doubly linked list and add nodes to it?

    see http://java.sun.com/j2se/1.3/docs/api/java/util/LinkedList.html
    which is implemented as a doubly linked list:
    import java.util.*;
    public class ListTest {
      public static void main(String [] argv) {
        // synchronize if multiple threads are using list
        LinkedList list = Collections.synchronizedList(
          new LinkedList());
        list.addFirst("this");
        list.addLast("is");
        list.addLast("a test");
        ListIterator it = list.iterator(0);
        // with iterator you can walk the list
    }You can then use iterators to walk the list:
    http://java.sun.com/j2se/1.3/docs/api/java/util/ListIterator.html
    Good Luck.

  • After Delete in Linked list...unable to display the linked list

    Hi...i know that there is an implementation of the class Linked Link but i am required to show how the linked list works in this case for my project...please help...
    Yes..I tried to delete some objects in a linked list but after that i am not able to display the objects that were already in the linked list properly and instead it throws an NullPointerException.
    Below shows the relevant coding for deleting and listing the linked list...
    public Node remove(Comparator comparer) throws Exception {
         boolean found = false;
         Node prevnode = head;          //the node before the nextnode
         Node deletedNode = null;     //node deleted...
         //get next node and apply removal criteria
         for(Node nextnode = head.getNextNode(); nextnode != null; nextnode = nextnode.getNextNode()) {
              if(comparer.equals(nextnode)) {
                   found = true;
                   //remove the next node from the list and return it
                   prevnode.setNextNode(nextnode.getNextNode());
                   nextnode.setNextNode(null);
                   deletedNode = nextnode;
                   count = count - 1;
                   break;
         if (found) return deletedNode;
         else throw new Exception ("Not found !!!");
    public Object[] list() {
         //code to gather information into object array
         Node node;
         Object[] nodes = new Object[count];
         node = head.getNextNode();
         for (int i=0; i<count; i++) {
              nodes[i] = node.getInformation();  // this is the line that cause runtime error after deleting...but before deleting, it works without problem.
              node = node.getNextNode();
         return nodes;
    }Please help me in figuring out what went wrong with that line...so far i really can't see any problem with it but it still throws a NullPointerException
    Thanks

    OK -- I've had a cup and my systems are coming back on line...
    The problem looks to be the way that you are handling the pointer to the previous node in your deletion code. Essentially, that is not getting incremented along with the nextNode -- it is always pointing to head. So when you find the node to delete then the line
       prevnode.setNextNode(nextnode.getNextNode());will set the nextNode for head to be null in certain situations (like if you are removing the tail, for instance).
    Then when you try to print out the list, the first call you make is
    node = head.getNextNode();Which has been set to null, so you get an NPE when you try to access the information.
    Nothing like my favorite alkaloid to help things along on a Monday morning...
    - N

Maybe you are looking for