0% found this document useful (0 votes)
53 views20 pages

Stacks 01

The document discusses stacks, which are last-in, first-out (LIFO) data structures. It describes how stacks work using push and pop operations, and provides algorithms for initializing an empty stack, checking if a stack is empty, adding and removing elements, and returning the top element of a stack without removing it. It also discusses implementations of stacks using arrays and linked lists, and considerations around error checking for underflow and overflow conditions.

Uploaded by

Faiz Khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views20 pages

Stacks 01

The document discusses stacks, which are last-in, first-out (LIFO) data structures. It describes how stacks work using push and pop operations, and provides algorithms for initializing an empty stack, checking if a stack is empty, adding and removing elements, and returning the top element of a stack without removing it. It also discusses implementations of stacks using arrays and linked lists, and considerations around error checking for underflow and overflow conditions.

Uploaded by

Faiz Khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Stacks

Stack
• A stack is a last in, first out (LIFO) data
structure
– Items are removed from a stack in the reverse
order from the way they were inserted
Stack
• With a stack, we always remove the item that
was most recently inserted. This policy is known
as last-in first-out or LIFO.
• A common example of a stack is surfing the
Web. When you click a hyperlink, your browser
displays the new page (insert). You can keep
clicking on hyperlinks to visit new pages. You
can always revisit the previous page by clicking
the Back button (remove).
Stack

A stack supports the insert and remove operations,


using a LIFO policy. By convention,
we name the stack insert operation push and the stack
remove operation pop.
Initializing a Stack

This algorithm initializes a stack to empty. An


empty stack has t = -1.

Input Parameters : None


Output Parameters: None
stack_init( ) {
t = -1
}
Testing for an Empty Stack
This algorithm returns true if the stack is empty or
false if the stack is not empty. An empty stack
has t = -1.

Input Parameters: None


Output Parameters: None
empty() {
return t == -1
}
Adding an Element to a Stack
This algorithm adds the value val to a stack. The stack is
represented using an array data. The algorithm assumes
that the array is not full. The most recently added item is
at index t unless the stack is empty, in which case, t = -1.

Input Parameters : val


Output Parameters: None
push(val) {
t = t + 1
data[t] = val
}
Removing an Element From a Stack

This algorithm removes the most recently


added item from a stack. The algorithm
assumes that the stack is not empty. The
most recently added item is at index t.

Input Parameters : None


Output Parameters: None
pop() {
t = t – 1
}
Returning the Top Element in a Stack

This algorithm returns, but does not remove, the


most recently added item in a stack. The
algorithm assumes that the stack is not empty.
The stack is represented using an array data.
The most recently added item is at index t.

Input Parameters : None


Output Parameters: None
top() {
return data[t]
}
One

This algorithm returns true if there is exactly one element on the stack.
The code uses the abstract data type stack.

Input Parameter : s (the stack)


Output Parameters: None
one(s) {
if (s.empty())
return false
val = s.top()
s.pop()
flag = s.empty()
s.push(val)
return flag
}
Initializing a Stack
• This algorithm initializes a stack to empty. The
stack is implemented as a linked list. The start of
the linked list, referenced by t, is the top of the
stack. An empty stack has t = null.

Input Parameters : None


Output Parameters: None
stack_init() {
t = null
}
Testing for an Empty Stack

This algorithm returns true if the stack is empty or


false if the stack is not empty. An empty stack
has t = null.

Input Parameters: None


Output Parameters: None
empty() {
return t == null
}
Adding an Element to a Stack
This algorithm adds the value val to a stack. The stack is
implemented using a linked list. The start of the linked
list, referenced by t, is the top of the stack.

Input Parameters: val


Output Parameters: None
push(val) {
temp = new node
temp.data = val
temp.next = t
t = temp
}
Removing an Element From a Stack
This algorithm removes the most recently added
item from a stack. The stack is implemented
using a linked list. The start of the linked list,
referenced by t, is the top of the stack. The
algorithm assumes that the stack is not empty.

Input Parameters : None


Output Parameters: None
pop() {
t = t.next
}
Returning the Top Element in a Stack

This algorithm returns, but does not remove, the most


recently added item in a stack. The stack is implemented
using a linked list. The start of the linked list, referenced
by t, is the top of the stack. The algorithm assumes that
the stack is not empty.

Input Parameters : None


Output Parameters: None
top() {
return t.data
}
Pushing and popping
0 1 2 3 4 5 6 7 8 9
stk: 17 23 97 44

top = 3 or count = 4

• If the bottom of the stack is at location 0, then an empty


stack is represented by top = -1 or count = 0
• To add (push) an element, either:
– Increment top and store the element in stk[top], or
– Store the element in stk[count] and increment count
• To remove (pop) an element, either:
– Get the element from stk[top] and decrement top, or
– Decrement count and get the element in stk[count]
After popping
0 1 2 3 4 5 6 7 8 9
stk: 17 23 97 44

top = 2 or count = 3

• When you pop an element, do you just leave the “deleted”


element sitting in the array?
• The surprising answer is, “it depends”
– If this is an array of primitives, or if you are programming in C or
C++, then doing anything more is just a waste of time
– If you are programming in Java, and the array contains objects,
you should set the “deleted” array element to null
– Why? To allow it to be garbage collected!
Sharing space
• Of course, the bottom of the stack could be at
the other end
0 1 2 3 4 5 6 7 8 9
stk: 44 97 23 17

top = 6 or count = 4
• Sometimes this is done to allow two stacks to
share the same storage area
0 1 2 3 4 5 6 7 8 9
stks: 49 57 3 44 97 23 17

topStk1 = 2 topStk2 = 6
Error checking
• There are two stack errors that can occur:
– Underflow: trying to pop (or peek at) an empty stack
– Overflow: trying to push onto an already full stack
• For underflow, you should throw an exception
– If you don’t catch it yourself, Java will throw an
ArrayIndexOutOfBounds exception
– You could create your own, more informative
exception
• For overflow, you could do the same things
– Or, you could check for the problem, and copy
everything into a new, larger array

You might also like