Hack Forums
[Java] Creating linked list without using the imported class - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Creating linked list without using the imported class (/showthread.php?tid=6056024)



Creating linked list without using the imported class - Juice. - Mar 10, 2020

I am trying to do an assignment in my Java class. I currently have it working, but I am using a linked list and it's methods in Java. To learn better, our professor wants us to make our own linked list and methods with it. It is for a calculator that uses a stack. I will post part of the code that shows it working using the predefined linked list, but I don't know how to make it myself and was going to see if I could get some help.



Code
LinkedList<Constant> constantlist=new LinkedList<Constant>();    //can't use this

                else if ((s.substring(0,2).equals(":=")) && (s.length() > 2)) {
                    char c = s.charAt(2);
                    if ((c>='a') && (c<= 'z') || (c>='A') && (c<= 'Z'))
                    {
                        s=s.substring(2);
                        for (int i=0; i< s.length(); i++)
                        {
                            if(!Character.isJavaIdentifierPart(s.charAt(i))) {
                                System.out.println("Error: invalid identifier.");
                                System.exit(1);
                            }
                        }
                        if (constantlist != null)
                        {
                            for (int i = 0; i < constantlist.size(); i++) {
                            if (s.equals(constantlist.get(i).name)==true)
                            {
                            System.out.println("ERROR: name already defined.");
                            System.exit(1);
                            }
                            }
                            constantlist.add(new Constant(s, stack.peek() , true));
                            System.out.println("Added constant " + s + " with value " + stack.peek());
                        }
                    }
                }
                    else {
                        for (int i = 0; i < constantlist.size(); i++) {
                            if (s.equals(constantlist.get(i).name)==true) // check if exists as a constant
                            {
                                foundconstant=true;
                                stack.push(constantlist.get(i).data); // if it does, use that value
                            }
                        }
                        if (foundconstant==false) {
                                    System.out.println("Didn't recognize input. Exiting.");
                                    System.exit(1);    
                                }



class Constant{
public String name;
public double data;
public boolean isconstant;



Constant(String n, double d, boolean c) {
    name = n;
    data = d;
    isconstant = c;
  }

}