















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
stack and queues ,chapter 3 , data Structures
Typology: Slides
1 / 55
This page cannot be seen from the preview
Don't miss anything!
















































2 8 1 7 2 7 2
1 7 2 1 7 2
8
1
7
2
8
1
7
2
top
top
top
top
top
top
Push(8)
Push(2)
pop()
pop()
pop()
STACK ERRORS
THE STACK ABSTRACT DATA TYPE
Stacks are the simplest of all data structures
Formally, a stack is an abstract data type (ADT)
: that supports the following two methods
pop(): Remove from the stack and return the top element
; on the stack
?an error occurs if the stack is empty – what error
Additionally, let us also define the following
: methods
size(): Return the number of elements in the stack
isEmpty(): Return a Boolean indicating if the stack is empty
top(): Return the top element in the stack, without removing
it
THE STACK ABSTRACT DATA TYPE
Operation
Operation Output
Output Stack Contents
Stack Contents
push(5)
push(5)
push(3)
push(3)
pop() pop()
push(7) push(7)
pop()
pop()
top()
top()
pop()
pop()
pop()
pop()
isEmpty()
isEmpty()
push(9)
push(9)
push(7)
push(7)
push(3)
push(3)
push(5) push(5)
size() size()
pop()
pop()
push(8)
push(8)
pop()
pop()
pop()
pop()
"error“ "error“
true true
THE STACK ABSTRACT DATA TYPE
Implementing an abstract data type in Java involves two
steps. The first step is the definition of a Java Application
Programming Interface (API), or simply interface , which
describes the names of the methods that the ADT
. supports and how they are to be declared and used
In addition, we must define exceptions for any error
conditions that can arise. For instance, the error condition
that occurs when calling method pop() or top() on an
empty stack is signaled by throwing an exception of type
, EmptyStackException
THE STACK ABSTRACT DATA TYPE
Code Fragment 5.2: Interface Stack documented with comments in Javadoc style (Section 1.9.3). Note also the use of
. the generic parameterized type, E, which implies that a stack can contain elements of any specified class
IMPLEMENTING A STACK USING AN
IMPLEMENTING A STACK USING AN
ARRAY OF A GIVEN SIZE,
ARRAY OF A GIVEN SIZE, N
N .
.
ANALYZING THE ARRAY-
BASED STACK
IMPLEMENTATION
The correctness of the methods in the array-based implementation follows
. immediately from the definition of the methods themselves
Note that we could have avoided resetting the old S [ t ] to null and we would
. still have a correct method
There is a trade-off in being able to avoid this assignment should we be
. thinking about implementing these algorithms in Java
The trade-off involves the Java garbage collection mechanism that searches
memory for objects that are no longer referenced by active objects, and
reclaims their space for future use. Let e = S [ t ] be the top element before the
. pop method is called
By making S [ t ] a null reference, we indicate that the stack no longer needs to
hold a reference to object e. Indeed, if there are no other active references to e ,
. then the memory space taken by e will be reclaimed by the garbage collector
Table 5.1 shows the running times for methods in a realization of a stack by an
array. Each of the stack methods in the array realization executes a constant
number of statements involving arithmetic operations, comparisons, and
. assignments
In addition, pop also calls isEmpty, which itself runs in constant time. Thus, in
this implementation of the Stack ADT, each method runs in constant time, that
. is, they each run in O (1) time
A DRAWBACK WITH THE
ARRAY-BASED STACK
IMPLEMENTATION
. The array implementation of a stack is simple and efficient
This implementation has one negative aspect
.it must assume a fixed upper bound, CAPACITY, on the ultimate size of the stack . In Code Fragment 5.4, we chose the capacity value 1,000 more or less arbitrarily
, An application may actually need much less space than this
.which would waste memory
, An application may need more space than this
which would cause our stack implementation to generate an exception as soon as a client . program tries to push its 1,001st object on the stack
Thus, even with its simplicity and efficiency, the array-based stack
. implementation is not necessarily ideal
, Fortunately, there is another implementation, which we discuss next
.and use space proportional to the actual number of elements stored in the stack
Still, in cases where we have a good estimate on the number of items needing to
, go in the stack
Stacks serve a vital role in a number of computing applications, so it is helpful to have a fast
. stack ADT implementation such as the simple array-based implementation
IMPLEMENTING A STACK
WITH A GENERIC LINKED
LIST
the top of the stack is at the head
.or at the tail of the list
Thus, it is more efficient to have the top of the stack at the head of
. our list
, in order to perform operation size in constant time
we keep track of the current number of elements in an instance
. variable
A Java implementation of a stack, by means of a generic singly
. linked list, is given in Code Fragment 5. . All the methods of the Stack interface are executed in constant time
In addition to being time efficient, this linked list implementation
has a space requirement that is O ( n ), where n is the current number
. of elements in the stack
Thus, this implementation does not require that a new exception be
created to handle size overflow problems. We use an instance
variable top to refer to the head of the list (which points to the null
. object if the list is empty)
When we push a new element e on the stack, we simply create a
new node v for e , reference e from v , and insert v at the head of the
. list
Likewise, when we pop an element from the stack, we simply
remove the node at the head of the list and return its element. Thus,
we perform all insertions and removals of elements at the head of
. the list
Code Fragment 5.7: Class NodeStack, which implements the Stack
interface using a singly linked list, whose nodes are objects of class Node from
Code Fragment 5.6.