Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Adding element to Linked List

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Adding element to Linked List

    I'm trying to understand the behavior of on how the elements of a String[] contentsOfBag are added into a linkedbag which is csc220bag (for this programming case).

    First concern: from my understanding, each elements of String[] contentsOfBag are added such as element "A" goes into a Node with the data "A". Inside this same Node we have another Node where the next element "_" will be the data of this next Node and so on? Please correct me if I'm wrong/not totally correct.

    Second concern is the purpose of constructor "private Node(T dataPortion)" & " private Node(T dataPortion, Node nextNode)" ?

    It'll be very helpful for my understanding since I need to implement removeAllOccurences method. Ty

    Here is the code :

    Driver Code
    public class LinkedBagCSC220JavaDriver {
     
        public static void main(String[] args) {
            System.out.println("=== LINKED BAG 220 JAVA ==========================================================");
            System.out.println("[+] Creating a CSC220 LinkedBag...");
            PrimaryDataStructureBagInterface<String> csc220Bag = new LinkedBag<>();
            testAdd(csc220Bag);
            testRemoveAllOccurrences(csc220Bag);
            System.out.println("=== LINKED BAG 220 JAVA ==========================================================");
        }
     
        private static void displayBag(PrimaryDataStructureBagInterface<String> aBag) {
            System.out.print("[>] The bag now contains " + aBag.getCurrentSize() + " string(s): \t");
            Object[] bagArray = aBag.toArray();
            for (Object bagArray1 : bagArray) {
                System.out.print(bagArray1 + " ");
            }
            System.out.println();
        }
     
        private static void testRemoveAllOccurrences(PrimaryDataStructureBagInterface<String> aBag) {
            // Removing all occurrences of the given entries from a bag
            System.out.println("[+] Creating... a 2D test array with the below contents: \t");
            String[][] testArray = {
                    { "A", "A", "A", "A", "A", "A" },
                    { "B", "A", "Bb", "B", "Bb", "B" },
                    { "C", "B", "_", "A" },
                    { "n", "u", "l", "l" }
            };
     
            for (String[] row : testArray) {
                System.out.print("\t\t\t\t\t");
                for (String col : row) {
                    System.out.print(col + " ");
                }
                System.out.println("");
            }
     
            aBag.removeAllOccurrences(testArray);
            displayBag(aBag);
        }
     
        private static void testAdd(PrimaryDataStructureBagInterface<String> aBag) {
            // Adding strings
            String[] contentsOfBag = {
                    "A", "_", "_", "G", "Bb", "A", "_", "u", "n",
                    "o", "A", "o", "d", "Bb", "A", "A", "l", "l"
            };
            System.out.print("[+] Adding.... these items to the bag: \t");
            for (String entry : contentsOfBag) {
                aBag.add(entry); // trying to understand behind the scene of this line
                System.out.print(entry + " ");
            }
            System.out.println();
     
            displayBag(aBag);
        }
    }

    Linkedbag
    public final class LinkedBag<T> implements PrimaryDataStructureBagInterface<T> {
     
        private Node firstNode;
        private int numberOfEntries;
     
        public LinkedBag() {
            firstNode = null;
            numberOfEntries = 0;
        }
     
        @Override
        public boolean removeAllOccurrences(T[][] entries) {
        }
     
        private class Node {
            private T data;
            private Node next;
     
            private Node(T dataPortion) {
                this(dataPortion, null);
            } // end constructor
     
            private Node(T dataPortion, Node nextNode) {
                data = dataPortion;
                next = nextNode;
            }
        }
    }

    Interface class - PrimaryDataStructureBagInterface
    public interface PrimaryDataStructureBagInterface<T> {
     
        public int getCurrentSize();
     
        public boolean isEmpty();
     
        public boolean add(T newEntry);
     
        public boolean removeAllOccurrences(T[][] entries);
     
        public T[] toArray();
    }
    Last edited by siid14; September 29th, 2022 at 10:11 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding element to Linked List

    need the interface class
    Yes, that class is needed to compile the code for execution testing.

    Have you studied the structure of a linked list? Do a lookup on the internet and read up on what goes into a linked list.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Adding element to Linked List

    Just added the interface class

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding element to Linked List

    After you have looked at the online documentation/description for linked lists, do you have any questions about your assignment?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Adding element to Linked List

    I wanna know if I'm right with my understanding of my first concern

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding element to Linked List

    Yes, that sounds like what a linked list is.
    Did you lookup what a linked list is on the internet? Did you see anything different from what you thought?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Adding element to Linked List

    Okay, I see. Also. quick question, were you able to compile the code. Cause as. it is when I try it says :
    LinkedBagCSC220JavaDriver.java:32: error: cannot find symbol
        private static void displayBag(PrimaryDataStructureBagInterface<String> aBag) {
                                       ^
      symbol:   class PrimaryDataStructureBagInterface
      location: class LinkedBagCSC220JavaDriver
    LinkedBagCSC220JavaDriver.java:41: error: cannot find symbol
        private static void testRemoveAllOccurrences(PrimaryDataStructureBagInterface<String> aBag) {
                                                     ^
      symbol:   class PrimaryDataStructureBagInterface
      location: class LinkedBagCSC220JavaDriver
    LinkedBagCSC220JavaDriver.java:63: error: cannot find symbol
        private static void testAdd(PrimaryDataStructureBagInterface<String> aBag) {
                                    ^
      symbol:   class PrimaryDataStructureBagInterface
      location: class LinkedBagCSC220JavaDriver
    LinkedBagCSC220JavaDriver.java:26: error: cannot find symbol
            PrimaryDataStructureBagInterface<String> csc220Bag = new LinkedBag<>();
            ^
      symbol:   class PrimaryDataStructureBagInterface
      location: class LinkedBagCSC220JavaDriver
    LinkedBagCSC220JavaDriver.java:26: error: cannot find symbol
            PrimaryDataStructureBagInterface<String> csc220Bag = new LinkedBag<>();
                                                                     ^
      symbol:   class LinkedBag
      location: class LinkedBagCSC220JavaDriver

    How am I suppose to make it run? I don't understand the error..
    Last edited by siid14; September 29th, 2022 at 12:38 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding element to Linked List

    LinkedBagCSC220JavaDriver.java:32: error: cannot find symbol
    Where is the PrimaryDataStructureBagInterface.class file that defines PrimaryDataStructureBagInterface?
    The compiler can not find it.

    Since none of your classes are in packages, if all the source files are in the same folder then the compiler will be able to find them.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Adding element to Linked List

    I figure it out, using a package extension in VSC

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding element to Linked List

    Good, I am glad you have it working now.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Deleting an element out of a linked list with boolean method
    By arhzz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 16th, 2020, 09:53 AM
  2. I'm having trouble adding polynomials using a singly linked list
    By onestepahead in forum Object Oriented Programming
    Replies: 6
    Last Post: October 8th, 2014, 12:30 AM
  3. Adding Student Objects into singly linked list alphabetically [NullPointerException]
    By Restivethought in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2013, 07:37 AM
  4. [SOLVED] Linked List (adding alphabetically)
    By Usoda in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:17 PM

Tags for this Thread