IM-CS123-StacksQueues CS
IM-CS123-StacksQueues CS
2
Intended Learning Outcomes
• Define Stacks ADT.
• Identify Operations of Stacks ADT.
• Discuss the Implementations of Stacks ADT
using Array Implementation.
• Define Queue ADT.
• Identify Operations of Queue ADT.
• Discuss the Implementations of Queue ADT
using Pointer Implementation.
3
Abstract Data Type “Stacks”
•A stack is a special kind of list in which all insertions
and deletions take place at one end, called the top.
•Other names for a stack are "pushdown list," and
"LIFO" or "last-in first-out" list.
•The intuitive model of a stack is a pile of poker chips
on a table, books on a floor, or dishes on a shelf,
where it is only convenient to remove the top object
on the pile or add a new one above the top.
4
Operations of STACKS ADT
An abstract data type in the STACK family often includes
the following five operations.
5
Pseudocode
procedure EDIT;
abc#d##e
var
S: STACK;
STACK c: char;
ea
begin ae - reverse
0 E MAKENULL(S);
1 A while not eoln do begin
read(c);
if c = '#' then
POP(S)
else if c = '@' then
MAKENULL(S)
else { c is an ordinary character }
PUSH(c, S)
end;
print S in reverse order
end; { EDIT } 6
Applications of “Stacks”
•A text editor can process a line of text using a stack. The editor
reads one character at a time. If the character read is neither
the kill nor the erase character, it pushes the character onto the
stack. If the character read is the erase character, the editor
pops the stack, and if it is the kill character, the editor makes
the stack empty.
•Text editors always allow some character (for example,
"backspace") to serve as an erase character, which has the
effect of canceling the previous uncanceled character. For
example, if '#' is the erase character, then the string abc#d##e
is really the string ae.
•Text editors also have a kill character, whose effect is to cancel
all previous characters on the current line. For the purposes of
this example, we shall use '@’ as the kill character.
7
Array Implementation of Stacks
8
Abstract Data Type “Queue”
• A queue is another special kind of list, where items
are inserted at one end (the rear) and deleted at
the other end (the front). Another name for a
queue is a "FIFO" or "first-in first-out" list.
• The operations for a queue are analogous to those
for a stack, the substantial differences being that
insertions go at the end of the list, rather than the
beginning, and that traditional terminology for
stacks and queues is different.
9
Operations of QUEUE ADT
We shall use the following operations on queues.
1. MAKENULL(Q) makes queue Q an empty list.
2. FRONT(Q) is a function that returns the first element on
queue Q. FRONT(Q) can be written in terms of list
operations as RETRIEVE(FIRST(Q), Q).
3. ENQUEUE(x, Q) inserts element x at the end of queue Q.
In terms of list operations, ENQUEUE(x, Q) is INSERT(x,
END(Q), Q).
4. DEQUEUE(Q) deletes the first element of Q; that is,
DEQUEUE(Q) is DELETE(FIRST(Q), Q).
5. EMPTY(Q) returns true if and only if Q is an empty queue.
where x = element, Q = Queue
10
Pointer Implementation of Queue
11
Summary
• Abstract Data Type Stacks is a special kind of
list in which all insertions and deletions take
place at one end.
• Abstract Data Type Queue is a special kind
of list in where items are inserted at one end
(the rear) and deleted at the other end (the
front).
• Stacks and Queues can be implemented in
an array or pointer-based.
Insert Running Title 12
References
• Aho, A., Hopcroft, J. Ullman, J. (1983). Data Structures and Algorithms.
• Storer, J. (2002). An Introduction to Data Structures and Algorithms. Birkhauser
Publishing.
• Lafore, R. (2003). Data Structures and Algorithms in Java (Second Edition). Sams
Publishing.
• Tutorialspoint.com. Data Structures and Algorithms. Retrieved August 2020
from
https://www.tutorialspoint.com/data_structures_algorithms/data_structures_a
lgorithms_tutorial.pdf.