0% found this document useful (0 votes)
21 views17 pages

Chapter 4 Stacks and Queue

Uploaded by

yenazurinperez
Copyright
© © All Rights Reserved
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)
21 views17 pages

Chapter 4 Stacks and Queue

Uploaded by

yenazurinperez
Copyright
© © All Rights Reserved
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/ 17

DATA STRUCTURES AND

FILE ORGANIZATIONS
CHAPTER 4
STACKS AND QUEUES
TOPICS

• Stacks
• Infix, Postfix, and Infix Expressions
• Conversion of expressions
• Manual conversion
• Stack implementation
• Queues
STACKS

• Stacks are linear data structures, that can only be accessed at


one end for storing and retrieving data.
• New data is added to the top of a stack, and data is also
retrieved from the top of the stack.
• Similar to a stack of trays in a canteen.
• It is a LIFO structure (Last In First Out).
EXAMPLES OF STACK
STACKS

• Key operations;
• Clear() – clears the stack
• isEmpty() – Tests if the stack is empty
• Push(el) – adds ‘el’ to the top of the stack
• Pop() – Retrieves the top element of the stack
• topEl() – Returns the top element without removing it.
STACK USE

• Consider the problem of matching delimiters in a program;


• Delimiters : [, ], {, }, (, ), /*, */
• Problem; to test the delimiters have been correctly matched;
• A) while(m<(n[8] + o)) {p=7; /*initialise p*/ r=6;}
• B) a = b + ( c – d ) * ( e - f ))
• Case A should return a success, while case B should
return an error.
delimiterMatching(file)
read character ch from file;
while not end of file
if ch is ‘(‘, ‘[‘, or ‘{‘
push(ch);
else if ch is ‘/’
read next character;
if this character is ‘*’
skip all characters until “*/” is found and report an error
if the eof is reached before “*/” is encountered;
else ch = the character read in;
continue; // go to beginning of the loop;
else if ch is ‘)’, ‘]’, or ‘}’
if ch and popped off delimiter do not match
failure;
// else ignore other characters;
read next character ch from file;
if stack is empty
success;
else failure;
STACK CASE

• A) while(m<(n[8] + o)) {p=7; /*initialise p*/ r=6;}

• Add to stack ( - (
• Add to stack ( - ((
• Add to stack [ - (([
• Remove from stack [ - ((
• Remove from stack ( - (
• Remove from stack ( -
• Add to stack { - {
• Add to stack /* - { /*
• Remove from stack */ - }
• Remove from stack } -
addingLargeNumbers()
read the numerals of the first number and store the numbers
corresponding to them on one stack;
read the numerals of the second number and store the numbers
corresponding to them another stack;
result = 0;
while at least one stack is not empty
pop a number from each nonempty stack and add them
to result;
push the unit part on the result stack;
store carry in result;
push carry on the result stack if it is not zero;
pop numbers from the result stack and display them;
SPECIAL TOPIC

on
Stack Implementation
(Refer to Chapter 4a)
QUEUES

• Queues are also linear data structures, however it is a waiting


line, where both ends are used.
• Data is added to one end of the line, and retrieved from the
other.
• Similar to a Queue in a bank etc.
• It is a FIFO structure (First In First Out).
QUEUE

• Key Operations;
• Clear() – Clear the queue
• isEmpty() – Check if the queue is empty
• Enqueue(el) – Add ‘el’ to end of queue
• Dequeue() – Take first element from queue
• firstEl() – Return first element without removing it.
QUEUE USE

• Simulating any queue;


• To determine how many staff are needed in a bank to maintain a good
level of service,
• Or, how many kiosks to open at the motorway toll.
Round the wondrous globe I wander wild,
Up and down-hill – Age succeeds to youth –
Toiling all in vain to find a child
Half so loving, half so dear as Ruth
by Lewis Carroll

acrosticIndicator()
while not finished
read a line of poem;
enqueue the first letter of the line;
while queue is not empty
dequeue and print a letter;

You might also like