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:
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;
}
}