Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
58 views
Data Structures Using Java - Chapter 2
Data Structures Using Java - Chapter 2
Uploaded by
lex7396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Data Structures Using Java - Chapter 2 For Later
Download
Save
Save Data Structures Using Java - Chapter 2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
58 views
Data Structures Using Java - Chapter 2
Data Structures Using Java - Chapter 2
Uploaded by
lex7396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Data Structures Using Java - Chapter 2 For Later
Carousel Previous
Carousel Next
Save
Save Data Structures Using Java - Chapter 2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 51
Search
Fullscreen
24 68 CHAPTER 2 The Stack The stack is one of the most useful concepts in computer science. In this chapter, we shall examine this deceptively simple data structure and see why it plays such a promi- nent role in the areas of programming and programming languages. We shall define the abstract concept of a stack and show how it can be made into a concrete and valuable tool in problem solving. DEFINITION AND EXAMPLES A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the zop of the stack. We can picture astack as in Figure 2.1.1. Unlike an array, the definition of a stack provides for the insertion and deletion of items, and thus a stack is a dynamic, constantly changing object. The question there- fore arises, how does a stack change? The definition specifies that a single end of the stack is designated as the stack top. New items may be put on top of the stack (in which case the top of the stack moves upwards to correspond to the new highest element) or items which are at the top of the stack may be removed (in which case the top of the stack moves downwards to correspond to the new highest element). To answer the question “which way is up?” we must decide which end of the stack is designated as its afelalola|s Stack containing2.1. Definition and Examples 69 top—that is, at which end items are added or deleted. By drawing Figure 2.1.1 so that F is physically higher on the page than all the other items in the stack, we imply that Fis the current top element of the stack. If any new items are added to the stack, they are placed on top of F, and if any items are deleted, F is the first to be deleted. This is also indicated by the vertical lines that extend past the items on the stack in the direction of the stack top. Figure 2.1.2 is a motion picture of a stack as it expands and shrinks with the pas- sage of time. Figure 2.1.2(a) shows the stack as it exists at the time of the snapshot in Figure 2.1.1. In Figure 2.1.2(b), item G is added to the stack. According to the defini- tion, there is only one place on the stack where it can be placed—the top. The top ele~ ment on the stack is now G, As the motion picture progresses through frames c, d, and e,items H, I, and J are successively added onto the stack. Note that the last item insert- ed (in this case J) is at the top of the stack. Beginning with frame f, however, the stack begins to shrink, as first J, then I, H, G, and F are successively removed, At each point, the top element is removed, since a deletion can be made only from the top. Item G could not be removed from the stack before items /, J, and H! were gone. This illustrates the most important attribute of a stack, that the last element inserted is the first ele- ment deleted, Thus J is deleted before / because J was inserted after I. For this reason a stack is sometimes called a last-in, first-out (or LIFO) list. ‘Between frames j and k, the stack has stopped shrinking and begins to expand again as item K is added. However, this expansion is short-lived, as the stack then shrinks to only three items in frame n. Note that there is no way to distinguish between frame a and frame i by looking at the stack’s state at the two instances. In both cases, the stack contains the identical items in the same order and has the same stack top. No record is kept on the stack of the fact that four items had been pushed and popped in the meantime. Similarly, there is no way to distinguish between frames d and f or j and |. If a record is needed of the intermediate items having been on the stack, it must be kept elsewhere; it does not exist within the stack itself. In fact, we have actually taken an extended view of what is really observed in a stack. The true picture of a stack is given by a view from the top looking down, rather than from a side looking in. Thus, in Figure 2.1.2, there is no perceptible difference be- tween frames h and o. In each case the element at the top is G. While the stack at h and the stack at o are not equal, the only way to determine this is to remove all the ele~ ments on both stacks and compare them individually. While we have been looking at cross-sections of stacks to make our understanding clearer, it should be noted that this is an added liberty and there is no real provision for taking such a picture. Primitive Operations ‘The two changes which can be made to a stack are given special names. When an item is added to a stack, it is pushed onto the stack, and when an item is removed, it is popped from the stack. Given a stack s, and an item i, performing the operation sipush(i) adds the item / to the top of stack s. Similarly, the operation spop() removes the top element and returns it as a method value. Thus the assignment operation i= s.pop0: removes the element at the top of s and assigns its value to i.pes e jo einpid uonoW 717 FNL © @ ® 0 Oo oO ® 8 © © © @ ¥ v ¥ ¥ v v vo v ¥ v Vv v v v a g a a @ a q a a a a © a a 2 2 2 2 a 2 2 2 2 2 2 2 3 2 D he] a a a Cc @ a a @ @ @ a a a z z a a z a a a a Y a a af a a Z a a ad |e dor a 2. 3 a a a D || H H H H H te! 7 I T he t «| 70
You might also like
What Is A Stack?: Some Key Points Related To Stack
PDF
No ratings yet
What Is A Stack?: Some Key Points Related To Stack
23 pages
Final Practice I UCSB Pstat 160A
PDF
No ratings yet
Final Practice I UCSB Pstat 160A
1 page
Pstat 160A Final Practice II Solutions
PDF
No ratings yet
Pstat 160A Final Practice II Solutions
6 pages
Instructional Module: Republic of The Philippines Nueva Vizcaya State University Bayombong, Nueva Vizcaya
PDF
No ratings yet
Instructional Module: Republic of The Philippines Nueva Vizcaya State University Bayombong, Nueva Vizcaya
19 pages
STACK
PDF
No ratings yet
STACK
1 page
DS - Unit I
PDF
No ratings yet
DS - Unit I
32 pages
Ds 2nd Unit
PDF
No ratings yet
Ds 2nd Unit
54 pages
Lecture 5- Stack ADT
PDF
No ratings yet
Lecture 5- Stack ADT
5 pages
UNITIIOFDSpdf__2024_11_23_23_51_47
PDF
No ratings yet
UNITIIOFDSpdf__2024_11_23_23_51_47
45 pages
Introduction To Algorithms
PDF
No ratings yet
Introduction To Algorithms
18 pages
Stacks and Queue by
PDF
No ratings yet
Stacks and Queue by
12 pages
Stack
PDF
No ratings yet
Stack
5 pages
Design and Analysis of Algorithms
PDF
No ratings yet
Design and Analysis of Algorithms
34 pages
Data Structures C++ II-Unit
PDF
No ratings yet
Data Structures C++ II-Unit
24 pages
21cs32-Dsa-Module 2 - 2022
PDF
No ratings yet
21cs32-Dsa-Module 2 - 2022
35 pages
DSA-Lect-5-Stack
PDF
No ratings yet
DSA-Lect-5-Stack
31 pages
Stack
PDF
No ratings yet
Stack
91 pages
Stack
PDF
No ratings yet
Stack
25 pages
Chapter 5 Stack and Queue
PDF
No ratings yet
Chapter 5 Stack and Queue
135 pages
Unit 4
PDF
No ratings yet
Unit 4
48 pages
MC 0068 - 01 Data Structures Using C
PDF
No ratings yet
MC 0068 - 01 Data Structures Using C
15 pages
Stack
PDF
No ratings yet
Stack
19 pages
Data Structure: Stacks'.: PPO Presentation
PDF
No ratings yet
Data Structure: Stacks'.: PPO Presentation
18 pages
Elementary Data Structure
PDF
No ratings yet
Elementary Data Structure
15 pages
Week 4
PDF
No ratings yet
Week 4
23 pages
MCS Data 2
PDF
No ratings yet
MCS Data 2
54 pages
Data Structure Unit-2
PDF
No ratings yet
Data Structure Unit-2
9 pages
Data Structures and Algorithms - Unit 2
PDF
No ratings yet
Data Structures and Algorithms - Unit 2
19 pages
Unit 2 Stack_dce8f1f7-3d0a-46f9-9061-3b000fb70119
PDF
No ratings yet
Unit 2 Stack_dce8f1f7-3d0a-46f9-9061-3b000fb70119
48 pages
Data Structures Ass
PDF
No ratings yet
Data Structures Ass
11 pages
DS Unit - Ii
PDF
No ratings yet
DS Unit - Ii
78 pages
DSA - M03 - C01 - SLM - Stacks
PDF
No ratings yet
DSA - M03 - C01 - SLM - Stacks
40 pages
Data Structure Lecture 3 (1)
PDF
No ratings yet
Data Structure Lecture 3 (1)
26 pages
Chapter 6 - Stack and Queue PDF
PDF
No ratings yet
Chapter 6 - Stack and Queue PDF
92 pages
Unit - I
PDF
No ratings yet
Unit - I
36 pages
STACK
PDF
No ratings yet
STACK
39 pages
Unit Ii_stack & Queues
PDF
No ratings yet
Unit Ii_stack & Queues
28 pages
DSA Lab Report 9 Anas Zohrab
PDF
No ratings yet
DSA Lab Report 9 Anas Zohrab
6 pages
Stack and QUEUE
PDF
No ratings yet
Stack and QUEUE
31 pages
Stacks&Queues Lecture 4
PDF
No ratings yet
Stacks&Queues Lecture 4
60 pages
communication skills
PDF
No ratings yet
communication skills
133 pages
CH # 2 (The Stack)
PDF
No ratings yet
CH # 2 (The Stack)
19 pages
DS Unit 2
PDF
No ratings yet
DS Unit 2
34 pages
Unit 2
PDF
No ratings yet
Unit 2
29 pages
Stack
PDF
No ratings yet
Stack
48 pages
Data Structure Using C Bsc-IV
PDF
No ratings yet
Data Structure Using C Bsc-IV
26 pages
Unit-III: Introduction To Stacks Applications of Stacks
PDF
No ratings yet
Unit-III: Introduction To Stacks Applications of Stacks
37 pages
Unit Two
PDF
No ratings yet
Unit Two
33 pages
Module2_Stack
PDF
No ratings yet
Module2_Stack
42 pages
Unit-4 OOP_DS
PDF
No ratings yet
Unit-4 OOP_DS
15 pages
Module 2 Stack
PDF
No ratings yet
Module 2 Stack
19 pages
HO 05 Stack
PDF
No ratings yet
HO 05 Stack
78 pages
UNIT-2
PDF
No ratings yet
UNIT-2
64 pages
Topic 3 Stacks
PDF
No ratings yet
Topic 3 Stacks
39 pages
Abstract Data Types (ADT's) (Stack) - Anqa#11Green
PDF
No ratings yet
Abstract Data Types (ADT's) (Stack) - Anqa#11Green
9 pages
Analogy For Abstract Data Types
PDF
No ratings yet
Analogy For Abstract Data Types
5 pages
Stack and Queue
PDF
No ratings yet
Stack and Queue
26 pages
Long Report Ds
PDF
No ratings yet
Long Report Ds
21 pages
L5_Stack_Queue
PDF
No ratings yet
L5_Stack_Queue
127 pages
Ds Chapter 3 Stacks, Queues and Recursion-Part I
PDF
No ratings yet
Ds Chapter 3 Stacks, Queues and Recursion-Part I
30 pages
DS-Lect2
PDF
No ratings yet
DS-Lect2
36 pages
A1 Fundamentals Examples
PDF
No ratings yet
A1 Fundamentals Examples
7 pages
Pstat 171
PDF
No ratings yet
Pstat 171
2 pages
A.1. Practice Problems On Fundamentals of Probability
PDF
No ratings yet
A.1. Practice Problems On Fundamentals of Probability
6 pages
UCSB Math34B Homework
PDF
No ratings yet
UCSB Math34B Homework
5 pages
Pstat 160 A Homework 6
PDF
No ratings yet
Pstat 160 A Homework 6
2 pages
PStat 160 A Homework Solutions
PDF
No ratings yet
PStat 160 A Homework Solutions
6 pages
Econ 171 Extra Credit PDF
PDF
No ratings yet
Econ 171 Extra Credit PDF
1 page
Pstat 120c 2.17.15
PDF
No ratings yet
Pstat 120c 2.17.15
3 pages
Math104B Midterm Key 2010
PDF
No ratings yet
Math104B Midterm Key 2010
8 pages
Math104B Midterm Key 2010
PDF
No ratings yet
Math104B Midterm Key 2010
8 pages