Data Structures: Stacks - Self Review

Ace your homework & exams now with Quizwiz!

What is abstraction and what advantage does it provide?

Abstraction is the concept of hiding the underlying implementation of operations and data storage in order to simplify the use of a collection.

push

Adds an element to the top of the stack. public void push (T element){ if (count == stack.length) expandCapacity(); stack[count] = element; count++; } ■ Make sure that the array is not full. ■ Set the reference in position top of the array to the object being added to the stack. ■ Increment the values of top and count

What is an abstract data type?

An abstract data type is a data type that is not defined within the programming language and must be defined by the programmer.

A collection is an abstraction where the details of the implementation are hidden.

true

A linked implementation of a stack adds and removes elements from one end of the linked list.

true

A linked list dynamically grows as needed and essentially has no capacity limitations.

true

A linked list is composed of objects that each point to the next object in the list.

true

A stack is the ideal data structure to use when evaluating a postfix expression.

true

Any implementation of a collection can be used to solve a problem as long as it validly implements the appropriate operations.

true

By using the interface name as a return type, the interface doesn't commit the method to the use of any particular class that implements a stack.

true

Dealing with the first node in a linked list often requires special handling.

true

Object reference variables can be used to create linked structures

true

Objects that are stored in a collection should not contain any implementation details of the underlying data structure.

true

an Object reference can be used to refer to any object

true because ultimately all classes are descendants of the Object class

instantiate an array

stack = (T[])(new Object[DEFAULT_CAPACITY]);

data structure

the collection of programming constructs used to implement a collection. For example, a collection might be implemented using a fixed-size structure such as an array

Type compatibility

the concept of whether a particular assignment of an object to a reference is legal

linear collection

the elements of the collection are organized in a straight line

an ArrayList can be used to store any kind of object

true

isEmpty

Determines if the stack is empty.

data type

a group of values & the operations defined on those values

collection

is an object that gathers and organizes other objects

stack

(LIFO) a linear collection whose elements are added & removed from the same end. We say that a stack is processed in a last in, first out manner. The last element to be put on a stack will be the first one that gets removed.

ArrayStack will need what variables

//a constant to store the default capacity, private final int DEFAULT_CAPACITY = 10; //a variable to keep track of the top of the stack, private int count; //& a variable for the array to store the stack private T[] stack;

solve this postfix expression 3 4 * 2 5 4 * 2

12 2 5 4 * 2 then 12 7 4 * 2 then 5 4 * 2 then 20 2 then 10

What is a collection?

A collection is an object that gathers and organizes other objects

What is a data structure?

A data structure is the set of objects necessary to implement an abstract data type.

What is a data type?

A data type is a set of values and operations on those values defined within a programming language.

What are the advantages to using a linked implementation as opposed to an array implementation?

A linked implementation allocates space only as it is needed & has a theoretical limit of the size of the hardware.

Compare and contrast a linked list and an array.

A linked list has no capacity limitations, while an array does. However, arrays provide direct access to elements using indexes, whereas a linked list must be traversed one element at a time to reach a particular point in the list.

What is the difference between a doubly linked list and a singly linked list?

A singly linked list maintains a reference to the first element in the list and then a next reference from each node to the following node in the list. A doubly linked list maintains two references: front and rear. Each node in the doubly linked list stores both a next and a previous reference

What is the characteristic behavior of a stack?

A stack is a last in, first out (LIFO) structure.

What are the advantages to using an array implementation as opposed to a linked implementation?

An array implementation uses less space per object since it only has to store the object and not an extra pointer. However, the array implementation will allocate much more space than it needs initially.

How do object references help us define data structures?

An object reference can be used as a link from one object to another. A group of linked objects can form a data structure, such as a linked list, on which a collection can be based.

What is the purpose of Generics in the Java language?

Beginning with Java 5.0, Java enables us to define a class based on a generic type. That is, we can define a class so that it stores, operates on, and manages objects whose type is not specified until the class is instantiated. This allows for the creation of structures that can manipulate "generic" elements and still provide type checking

What do the LinkedStack<T> and ArrayStack<T> classes have in common?

Both the LinkedStack<T> and ArrayStack<T> classes implement the StackADT<T> interface. This means that they both represent a stack collection, providing the necessary operations needed to use a stack. Though they both have distinct approaches to managing the collection, they are functionally interchangeable from the user's point of view

Why is a class an excellent representation of an abstract data type?

Classes naturally provide abstraction since only those methods that provide services to other classes have public visibility.

size

Determines the number of elements on the stack.

peek

Examines the element at the top of the stack.

Define inheritance?

Inheritance is the process in which a new class is derived from an existing one. The new class automatically contains some or all of the variables and methods in the original class. Then, to tailor the class as needed, the programmer can add new variables and methods to the derived class, or modify the inherited ones.

Why should a linked list node be separate from the element stored on the list?

It is unreasonable to assume that every object that we may want to put in a collection can be designed to cooperate with the collection implementation. Furthermore, the implementation details are supposed to be kept distinct from the user of the collection, including the elements the user chooses to add to the collection.

What impact would the use of sentinel nodes or dummy nodes have upon a doubly linked list implementation?

It would take two dummy records in a doubly linked list, one at the front and one at the rear, to eliminate the special cases when dealing with the first and last node.

Given the example in figure 14.4, will the following code compile? Horse creature = new Mammal();

No, a reference variable for a child or subclass may not hold a reference to a parent or superclass. To make this assignment, you would have to explicitly cast the parent class into the child class (Horse creature = (Horse)(new Mammal());

What is the advantage of postfix notation?

Postfix notation avoids the need for precedence rules that are required to evaluate infix expressions

What is the advantage of postfix notation?

Postfix notation avoids the need for precedence rules that are required to evaluate infix expressions

pop

Removes an element from the top of the stack. ■ Make sure the stack is not empty. ■ Decrement the top counter. ■ Set a temporary reference equal to the element in stack[top]. ■ Set stack[top] equal to null. ■ Return the temporary reference.

What are the advantages of the java.util.Stack implementation of a stack?

Since the java.util.Stack implementation is an extension of the Vector class, it can keep track of the positions of elements in the stack using an index and thus does not require each node to store an additional pointer. This implementation also allocates space only as it is needed, like the linked implementation.

What is the potential problem with the java.util.Stack implementation?

The java.util.Stack implementation is an extension of the Vector class and thus inherits a large number of operations that violate the basic assumptions of a stack.

What special case exists when managing linked lists?

The primary special case in linked list processing occurs when dealing with the first element in the list. A special reference variable is maintained that specifies the first element in the list. If that element is deleted, or a new element is added in front of it, the front reference must be carefully maintained

Given the example in Figure 14.4, list the subclasses of Mammal?

The subclasses of Mammal are Horse and Bat.

Define polymorphism?

The term polymorphism can be defined as "having many forms." A polymorphic reference is a reference variable that can refer to different types of objects at different points in time. The specific method invoked through a polymorphic reference can change from one invocation to the next

What would be the time complexity of the push operation if we chose to push at the end of the list instead of the front?

To push at the end of the list, we would have to traverse the list to reach the last element. This traversal would cause the time complexity to be O(n). An alternative would be to modify the solution to add a rear reference that always pointed to the last element in the list. This would help the time complexity for add but would have consequences if we try to remove the last element.

capacity

When an array object is created, it is allocated a specific number of cells into which elements can be stored. For example, Object[] collection = Object[500];

Given the example in figure 14.4, will the following code compile? Animal creature = new Parrot();

Yes, a reference variable of a parent class or any superclass may hold a reference to one of its descendants.

linked structure

a data structure that uses object reference variables to create links between objects. Linked structures are the primary alternative to an array-based implementation of a collection.

pointer

an object reference variable holds the address of an object, indicating where the object is stored in memory. The following declaration creates a variable called obj that is only large enough to hold the numeric address of an object: Object obj;

APIs

application programming interfaces

primitive data types

byte short int long float double char

What if the stack is empty on a pop?

catch (EmptyCollectionException exception)

abstract data type (ADT)

data type whose values and operations are not inherently defined w/in a programming language. It is abstract only in that the details of its implementation must be defined and should be hidden from the user. A collection, therefore, is an abstract data type.

stack interface

defined as Stack<T>, operating on a generic type T

abstraction

hides certain details at certain times. Dealing with an abstraction is easier than dealing with too many details at one time. Uses the interface and hides the details

Reuse

in its purest form, should mean that we create a collection that is written once, compiled into byte code once, and will then handle any objects we choose to store in it safely, efficiently, and effectively

type check this code- String x = new Integer(10);

it is not legal since you cannot assign a reference declared to be of type String to point to an object of type Integer

What are some of the other operations that might be implemented for a stack?

makeEmpty(), destroy(), full()

stack expandCapacity()

private void expandCapacity() { T[] larger = (T[])(new Object[stack.length*2]); for (int index=0; index < stack.length; index++) larger[index] = stack[index]; stack = larger; } The expandCapacity method is implemented to double the size of the array as needed. Of course since an array cannot be resized once it is instantiated, this method simply creates a new larger array, and then copies the contents of the old array into the new one. It serves as a support method of the class and can therefore be implemented with private visibility.

stack toString

public String toString(){ String result = "<top of stack>\n"; for (int index=count-1; index >= 0; index—) result += stack[index] + "\n"; return result + "<bottom of stack>"; }

self-referential

public class Person { private String name; private String address; private Person next; // a link to another Person object } Using only this one class, a linked structure can be created. One Person object contains a link to a second Person object. This second object also contains a reference to a Person, which contains another, and so on. This kind of relationship forms the basis of a linked list, which is a linked structure in which one object refers to the next, creating a linear ordering of the objects in the list.

What are the five basic operations on a stack?

push—adds an element to the end of the stack pop—removes an element from the front of the stack peek—returns a reference to the element at the front of the stack isEmpty—returns true if the stack is empty, returns false otherwise size—returns the number of elements in the stack

nonlinear collection

the elements are organized in something other than a straight line, such as a hierarchy or a network. For that matter, a nonlinear collection may not have any organization at all.

top

the end to which elements are added and from which they are removed

nodes

the objects stored in a linked list are referred to generically as the nodes of the list

A Java interface defines a set of abstract methods and is useful in separating the concept of an abstract data type from its implementation

true

A data structure is the underlying programming constructs used to implement a collection.

true

A polymorphic reference uses the type of the object, not the type of the reference, to determine which version of a method to invoke.

true

A programmer should choose the structure that is appropriate for the type of data management needed.

true

Elements in a collection are typically organized by the order of their addition to the collection or by some inherent relationship among the elements.

true

Errors and exceptions represent unusual or invalid processing.

true

For efficiency, an array-based stack implementation keeps the bottom of the stack at index 0.

true

For efficiency, an array-based stack implementation keeps the bottom of the stack at index 0.

true

How we handle exceptional conditions determines whether the collection or the user of the collection controls the particular behavior.

true

Implementing a list with a sentinel node or dummy node as the first node eliminates the special cases dealing with the first node.

true

Stack elements are processed in a LIFO manner—the last element in is the first element out.

true

The implementation of the collection operations should not affect the way users interact with the collection.

true

The java.util.Stack class is derived from Vector, which gives a stack inappropriate operations

true

The order in which references are changed is crucial to maintaining a linked list.

true

a linked list has no upper bound on its capacity other than the limitations of memory in the computer.

true

generic type

we can define a class so that it stores, operates on, and manages objects whose type is not specified until the class is instantiated. For example: class Box<T>{ // declarations & code that manage objects of type T } Then, when a Box is needed, it is instantiated w a specific class used in place of T. For example, if we wanted a Box of Widget objects, we could use the following declaration: Box<Widget> box1 = new Box<Widget>; Now suppose we wanted a Box in which to store Gadget objects; we could make the following declaration: Box<Gadget> box2 = new Box<Gadget>;


Related study sets

Biology II - Chapter 21 The Evidence of Evolution - Key Terms/Questions

View Set

2024 Recertifying Grassroots Referee quiz

View Set

AP Gov Semster 1 Final Question Bank

View Set

Federal Tax Considerations for Life Insurance and Annuities

View Set

Gazdaság és jogi alapismeret: Vállalati termelői magatartás

View Set