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 2 of 2

Thread: Having problems understanding what is going on in this linked stack.

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Having problems understanding what is going on in this linked stack.

    Okay, so we are working with linked lists, nodes and the stack ADT. I do believe that I have somewhat of a firm grasp on each of these concepts respectively. However, it is the implementation of all these ideas into code that I am metaphorically tearing my hair out over. When I look at it I am not seeing the big picture. The problem is my teacher just handed us the code and I guess expects us to learn it. What really would have worked is if he could of walked us through the building of the entire piece, step-by-step then I know I would understand these things. I guess my question too all you programming guru's is this: can you help me understand what each of these individual lines of code is doing? I really do not know if that is even possible with the constraints of not actually being face to face with each other but maybe you could take the time *and I am not saying you should* but it might help me if someone went through and gave assertions or comments for each line or something.

    The problem is, as I have stated in previous posts, I am in a class that I do not seem prepared for. However, I have been working my butt off to try and catch up to the level that these other students must be on. We have a quiz on Friday over stacks, queues and linked data structures and I will probably fail it... sorry I am venting: in any case yes please help if you can!

    package stack1_dog_Linked;
     
    import java.util.Scanner;
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		int choice;
    		String breed;
    		Dog dogReference;
    		boolean done = false;
    		DogStack dogStack = new DogStack();
    		Scanner keyboard = new Scanner(System.in);
     
    		while (!done) {
    			System.out
    					.print("Enter 1 to push, 2 to pop, 3 to check empty, and 4 to quit: ");
    			choice = keyboard.nextInt();
    			System.out.println();
     
    			switch (choice) {
    			case 1:
    				System.out.print("Enter a dog breed to push: ");
    				breed = keyboard.next();
    				dogReference = new Dog(breed);
    				dogStack.push(dogReference);
    				System.out.println();
    				break;
    			case 2:
    				if (dogStack.isEmpty()) {
    					System.out.println("Can't pop, stack is empty!");
    				} else {
    					System.out.println("The top dog was a : "
    							+ dogStack.pop().getBreed());
    					System.out.println();
    				}
    				break;
    			case 3:
    				if (dogStack.isEmpty()) {
    					System.out.println("The stack is empty.");
    				} else {
    					System.out.println("The stack is not empty.");
     
    				}
    				break;
    			case 4:
    				done = true;
    				break;
    			default:
    				System.out.println("The number you entered, " + choice
    						+ ", is not 1, 2, 3, 4. Try again!");
    				System.out.println();
    				break;
     
    			}
    		}
    		System.out.println("...quitting");
    	}
     
    }

    package stack1_dog_Linked;
     
    public class Dog {
    	private String breed = new String();
    	private String name = new String();
     
    	public Dog(String breedName) {
    		breed = breedName;
    	}
     
    	public void setBreed(String breedName) {
    		breed = breedName;
    	}
     
    	public String getBreed() {
    		return breed;
    	}
     
    }


    package stack1_dog_Linked;
     
    public class DogStack {
     
    	private Node top = null;
     
    	private class Node {
    		private Dog doggy;
    		private Node nextNode;
     
    		Node(Dog newDog) {
    			doggy = newDog;
    		}
    	}
     
    	public void push(Dog aDog) {
    		Node dogNode = new Node(aDog);
    		dogNode = new Node(aDog);
    		dogNode.nextNode = top;
    		top = dogNode;
    	}
     
    	public Dog pop() {
    		Dog topDog = top.doggy;
    		top = top.nextNode;
    		return topDog;
    	}
     
    	public boolean isEmpty() {
    		return (top == null);
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Having problems understanding what is going on in this linked stack.

    I'm a little confused as to what your question is. Do you understand the theory of how linked lists and stacks work? Or are you having a hard time with the implementation? What parts of the code don't you understand?

    A stack data structure is what it sounds like: a stack of stuff. You can put stuff on top of the stack (known as "pushing"), or you can remove stuff from the top of the stack (known as "popping"). This is known as a "FILO" (first in, last out) data structure because what you put in first is what you get out last.

    A linked list is similar to a train. You have a head and a tail, and between these you have various train cars. Each train car is connected directly to only it's two neighbors: the train car in front of it, or the train car after it.

    A good way to read through code is to break it up into chunks.

    The largest chunk are top-level class. So you have 3 classes: a Main class, a Dog class, and a DogStack class.

    Inside of each class you can have various fields, methods, or internal classes. That would be the next division you can make.

    Let's start with the Dog class since it's the simplest.

    it has 2 fields: breed, and name. Both hold String objects so we would say these are String type variables.

    There are 3 methods inside of Dog: A constructor, setBreed, and getBreed. What is each method suppose to do? Well, normally you would read the documentation (a.k.a. Javadoc), but there aren't any. However, the method names or context indicate what they are suppose to do. The constructor creates a new Dog object. It sets the breed of the dog to the given parameter. The setBreed method alters the breed of the given Dog object to the specified breed, and the getBreed method retrieves the current breed. Incidentally, the Dog class initializes the name and breed fields to new empty strings. This really isn't the best way to do this, and for future reference I would do it this way:

    	private String breed = "";
    	private String name = "";

    There are complicated reasons for this, if you're interested look up "String interning". The class also isn't really complete, it doesn't use the name field at all. I don't know if this was intentional or not, just thought you should be aware of that.

    Now it's your turn to do the same level-by-level breakdown of the other two classes. Don't be afraid to add documenting comments! It will help you when you come back to your code and try to understand what it's suppose to do.

    There are 3 main ways to document code:

    /**
     * this is a JavaDoc comment block
     */
     
     /*
      this is a comment block
     */
     
    // this is a line comment

    JavaDoc blocks are basically glorified comment blocks. However, in Java I exclusively use these for documenting classes, methods, and fields and don't really use regular comment blocks at all for documentation. A JavaDoc block begins with a forward slash and two asterisks. The end of the JavaDoc block ends with an asterisk and a forward slash. Everything in-between the beginning and ending token are part of the JavaDoc block. There are also special annotations you can add to JavaDoc blocks for tools which pull the JavaDoc and create documentation for your class, but these aren't really required.

    It's also common to add an asterisk at the beginning of each new line, though I can't remember if this is required or not.

    A JavaDocs primary purpose is to identify the "contract" which the given item is expected to follow. Basically, answer "what can it do".

    Line comments are useful for documenting the code itself. A good line-comment will explain in natural language (e.g. English) what a particular section of code is suppose to do. There is such a thing as over-commenting or having poor comments, and sadly commenting isn't something that's taught that well if at all in classes. A comment is meant to be useful. If you think a comment provides useful information, add it.

    Here's a documented version of the Dog class:

    package stack1_dog_Linked;
     
    /**
     * Represents a Dog.
     * A Dog can have a name and a breed.
     */
    public class Dog {
    	private String breed = new String();
    	private String name = new String();
     
            /**
             * Creates a dog.
             * parameters:
             *    breedName: The breed of the dog
             *
             */
    	public Dog(String breedName) {
    		breed = breedName;
    	}
     
            /**
             * Sets the breed of the dog to breedName
             */ 
    	public void setBreed(String breedName) {
    		breed = breedName;
    	}
     
            /**
             * Gets the current breed of the dog
             */ 
    	public String getBreed() {
    		return breed;
    	}
     
    }

Similar Threads

  1. STACK USING LINKED LIST------------HELP !
    By jayeshsolanki93 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 26th, 2012, 10:27 AM
  2. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 PM
  3. Need help to understanding with Doubly-linked circular Lists
    By mrdeath9x in forum Java Theory & Questions
    Replies: 3
    Last Post: May 5th, 2011, 03:51 AM
  4. Problems in linked lists
    By Hotzero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2010, 09:25 AM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM