Lecture 4 Stack
Lecture 4 Stack
Stacks
9/26/2024 https://www.pnu.edu.sa
Updated By L.Fatimah Alanizi 1
Outline
We don't know exactly how a stack or queue is implemented, and we don't need to. We
just need to understand the idea of the collection and what operations it can perform.
• ADTs implementation:
• Answers the ‘how’ questions.
• Done after specification
• Programmer (Implementer) who implements ADT is concerned with
specification, representation, implementation
• An ADT specifies:
• Data stored
• Operations on the data
• Error conditions associated with operations -> OK
While designing ADTs, designer have to specify ADT’s data and operations
(before you implement them) , a designer has to deal with two types of questions :
1. Ask questions about the data in the data collection of the ADT ,what data type to be used?
(To decide the data structures )
2. What operations can be performed on the values of that particular data type?
9/26/2024 Updated By L.Fatimah Alanizi 5
Abstract Data Types (ADTs)
• Example: Design an ADT modeling a simple stock trading system
• Error conditions:
• Buy/sell a nonexistent stock
• cancel a nonexistent order
9/26/2024 Updated By L.Fatimah Alanizi 6
Stacks
9/26/2024
• Top(peek): Examine the top element.
Updated By L.Fatimah Alanizi
stack
8
The Stack ADT
• The Stack ADT stores arbitrary objects (elements are of a generic type <E>)
• Insertions and deletions follow the last-in first-out scheme
• Think of a spring-loaded plate dispenser
• Structure: the elements are linearly arranged, and ordered according to the
order of arrival, most recently arrived element is called top
• Main (Core) stack operations:
• push(object): inserts an element
• object pop(): removes and returns the last inserted element
11
9/26/2024 Updated By L.Fatimah Alanizi
Exceptions
• Attempting the execution of an operation of ADT may sometimes cause an
error condition, called an exception
System.out.println(s.pop()); // "c"
9/26/2024 Updated By L.Fatimah Alanizi 14
Stack Operation: Example
…
S
0 1 2 t
16
9/26/2024 Updated By L.Fatimah Alanizi
Array-based Implementation
…
S
0 1 2 t isFull
public E pop () { 2
E e= data[ t-- ]; 0
return e }
T
public E pop () { 2
E e= data[ t-- ]; 0 T
return e }
public E pop () { 2
E e= data[ t-- ]; 0 T
return e }
e
public E pop () { 2
E e= data[ t-- ]; 0 T
return e }
public E pop () { 2
public E pop () { 2
public E pop () { 2
public E pop () { 2
public E pop () { 2
E e= data[ t-- ]; 0 T
e
return e; }
public E pop () { 2
E e= data[ t-- ]; 0 T
return e; }
public E pop () { 2
E e= data[ t-- ]; 0 T
return e; }
e
public E pop () { 2
E e= data[ t-- ]; 0
return e; }
T
public E pop () { 2
E e= data[ t-- ]; 0
return e; }
T
36
9/26/2024 Updated By L.Fatimah Alanizi
Linked List-based Stack implementation
(1/2)
• The top of the stack is the head of the linked list.
• push: create a new node and add it at the top (head) of the
stack.
Top Bottom
37
a 2 2 a
0 1 2 3 0 1 2 3
3 5 2 9 5 9 2 5 3
1
0 3
return var
44
9/26/2024 Updated By L.Fatimah Alanizi
Parentheses Matching
❑Each “(”, “{”, or “[” must be paired with a matching
“)”, “}”, or “[”
• correct: ( )(( )){([( )])}
• correct: (( )(( )){([( )])})
• incorrect: )(( )){([( )])}
• incorrect: ({[ ])}
• incorrect: (
47
9/26/2024 Updated By L.Fatimah Alanizi
Parentheses Matching Algorithm
48
9/26/2024 Updated By L.Fatimah Alanizi
HTML Tag Matching
◼ For fully-correct HTML, each <name> should pair with a matching </name>
<body>
<center>
<h1> The Little Boat </h1> The Little Boat
</center>
The storm tossed the little boat
<p> The storm tossed the little
like a cheap sneaker in an old
boat like a cheap sneaker in an washing machine. The three
old washing machine. The three drunken fishermen were used to
drunken fishermen were used to such treatment, of course, but not
such treatment, of course, but the tree salesman, who even as
not the tree salesman, who even as a stowaway now felt that he had
a stowaway now felt that he overpaid for the voyage.
had overpaid for the voyage. </p>
<ol> 1. Will the salesman die?
<li> Will the salesman die? </li> 2. What color is the boat?
3. And what about Naomi?
<li> What color is the boat? </li>
<li> And what about Naomi? </li>
49
</ol>
9/26/2024 </body> Updated By L.Fatimah Alanizi
Exercises
• What are the differences between Array-based stack and Linked list-based stack?
• Array Based Stack has a fixed size, needed an additional exception for inserting
method(push),
• Write a generic method that called reverseStack which reverses the elements in a
stack with objects of type E, using another Stacks?
while (! isEmpty() )
{
count++;
E e=pop();
if (e.equals(key))
position=count;
temp.push(e);
}
while (!temp.isEmpty())
this.push(temp.pop());
return position;
9/26/2024 } Updated By L.Fatimah Alanizi 52
9/26/2024 Updated By L.Fatimah Alanizi 53
Excercises
• write a java code method that prints all the numbers greater than 10 in the
stack S
Public void Print_grater ( Stack <Integer> s) {
If ( ! s.isEmpty() ) {
Stack s2=new <int> stack();
int element;
while (!s.isEmpty()) {
element=s.pop();
if(element >10)
System.out.println(element);
s2.push(element);
}
while (!s2.isEmpty())
s.push(s2.pop());
} }
9/26/2024 Updated By L.Fatimah Alanizi 54
Exsercise
• What values are returned during the following series of stack operations, if executed upon an
initially empty stack?
push(5)
push(3)
pop()
push(2)
push(8)
pop()
pop() 3->8->2->1->6->7->4->9
push(9)
push(1)
pop()
push(7)
push(6)
pop()
pop()
push(4)
pop()
9/26/2024 pop() Updated By L.Fatimah Alanizi 55
References
56