stack and queues ch3, Slides of Data Structures and Algorithms

stack and queues ,chapter 3 , data Structures

Typology: Slides

2016/2017

Uploaded on 12/12/2017

11_sundus
11_sundus 🇸🇦

1 document

1 / 55

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
STACKS AND QUEUES
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

Partial preview of the text

Download stack and queues ch3 and more Slides Data Structures and Algorithms in PDF only on Docsity!

STACKS AND QUEUES

?WHAT IS A STACK

Stack is a data structure in which data is added and removed at only

one position called the top

:Examples of stacks are

Stack of books

Stack of trays in a cafeteria

BUILDING STACK STEP-BY-STEP

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

Stack Overflow

An attempt to add a new element in an already full stack is an error

A common mistake often made in stack implementation

Stack Underflow

An attempt to remove an element from the empty stack is also an error

Again, a common mistake often made in stack implementation

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

push( e ): Insert element e to the top of the stack

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

an error occurs if the stack is empty

THE STACK ABSTRACT DATA TYPE

Example 5.3: The following table shows a series of stack operations and

. their effects on an initially empty stack S of integers

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

that does not have a size limitation

.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

. the array-based implementation is hard to beat

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

. Using a singly linked list to implement the stack ADT

In designing such an implementation, we need to decide if

the top of the stack is at the head

.or at the tail of the list

Since we can insert and delete elements in constant time

. only at the head

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

Rather than use a linked list that can only store objects of a

certain type, we would like, in this case, to implement a

. generic stack using a generic linked list

Thus, we need to use a generic kind of node to implement

this linked list. We show such a Node class in Code

. Fragment 5.

A GENERIC NODESTACK

CLASS

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.