0% found this document useful (0 votes)
2 views9 pages

Esdap Unit 4 Activity 2

This document discusses the application of stacks and queues in programming, providing practical examples for each data structure. It explains how stacks are used in recursion, checking parentheses, and job processing, while queues are applied in shared printers, task management, and traffic control. The conclusion highlights the importance of these structures in optimizing data processing and improving code maintainability.

Uploaded by

ortegaangel557
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Esdap Unit 4 Activity 2

This document discusses the application of stacks and queues in programming, providing practical examples for each data structure. It explains how stacks are used in recursion, checking parentheses, and job processing, while queues are applied in shared printers, task management, and traffic control. The conclusion highlights the importance of these structures in optimizing data processing and improving code maintainability.

Uploaded by

ortegaangel557
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

U4A2:

“Application of stacks and queues”

TECNICO SUPERIOR UNIVERSITARIO EN


TECNOLOGÍAS DE LA INFORMACIÓN
ÁREA DESARROLLO DE SOFTWARE Y
MULTIPLATAFORMA

PRESENTA:

ANGEL GUSTAVO ORTEGA GARCIA

CD. REYNOSA, TAMAULIPAS MARCH 2025.


Table of Content.
TOPIC PAGE

Table of Content. ................................................................................... I


Table of Figure. ..................................................................................... II

Introduction ........................................................................................... 1

Stacks ....................................................................................................... 2
Recursion in Programming .......................................................................... 2

Checking Parentheses in Text Strings .......................................................... 2

Task or Job Processing. .............................................................................. 2

Queues ..................................................................................................... 4
Shared Printers .......................................................................................... 4

Task Management in an Operating System. ................................................. 4

Traffic Control in a Traffic Light. .................................................................. 4

Conclusion ............................................................................................... 6

I
Table of Figure.

FIGURE PAGE
Figure 1 Example stack .................................................................................. 3
Figure 2 Example of Queues........................................................................... 5

II
Introduction

This document presents an explanation of the use of important data structures in


programming: stacks and queues. It discusses three practical examples of stacks
and three examples of queues, showing how they are implemented in code,
explaining how they work and highlighting the importance of their application in
different scenarios. In addition, it describes in which situations each of these
structures is most useful and how they contribute to the optimization and
organization of data processing. It also provides a clear description of the code
used, explaining its purpose and how each code fragment solves a specific
problem, allowing the reader to understand its application in software
development.

1
Stacks

Recursion in Programming

In programming, when one function is called inside another (recursive functions),


function calls are stacked on the stack. Each time a recursive call is made, it is
placed on the stack and, when the execution of a function ends, it is unstacked.
Why it is useful: It facilitates the execution of recursive functions and ensures
that the system follows the proper order to return to the correct point after each
call.

Checking Parentheses in Text Strings

When checking if the parentheses in a mathematical expression are balanced, we


use a stack. Whenever we find an open parenthesis, we stack it; when we find a
closed parenthesis, we unstack the corresponding parenthesis.
Why it is useful: It allows us to keep track of the open and closed parentheses in
the correct order, ensuring that the expression is well-formed.

Task or Job Processing.

2
In a job or task processing system, when several jobs have to be executed in
reverse order, a stack may be used. For example, in a printing system where jobs
are stacked and printed in reverse order of arrival.
Why it is useful: Ensures that the most recent jobs are processed first, ideal for
certain resource management applications.

A stack is a data structure that follows the LIFO (Last In, First Out) principle. This
means that the last element to be added to the stack is the first to be removed.

We need to validate the balance of parentheses in a mathematical expressions.


In this case the stack would be useful because, when you find an open
parenthesis, you stack it, and when you find a closed one, you unstack it to make
sure they are balanced.

Figure 1 Example stack

In Figure 1 we can see how to define a function called validate_parenthesis, which


checks if the parentheses in a given expression are correctly balanced.

3
Queues

A queue is a module that allows you to implement queues, which are linear data
structures that follow the FIFO (first in, first out) rule.

Shared Printers

When several people send documents to a printer, the jobs are placed in a print
queue. The job that arrives first will be the first to be printed.
Why it is useful: Ensures that documents are printed in the order in which they
were sent, preventing anyone from having to wait indefinitely.

Task Management in an Operating System.

Operating systems manage tasks in queues. When a task is waiting (for example,
waiting for disk or CPU access), it is placed in the task queue. The operating
system processes the tasks in the order in which they arrive.
Why it is useful: Ensures that tasks are managed fairly and efficiently, following
the FIFO principle.

Traffic Control in a Traffic Light.

4
Vehicle traffic at an intersection with traffic lights works like a queue. Vehicles
arrive and line up, and the traffic light allows them to pass in the order in which
they arrived.
Why it is useful: It organizes the flow of traffic, ensuring that all vehicles pass in
an orderly fashion, without skipping a turn.

In the case of queuing, a classic application is the management of tasks or


processes in an operating system. The tasks to be performed are queued and
processed in the order in which they arrived, following the FIFO principle.

Figure 2 Example of Queues

In figure 2 shows how to implement a queue data structure and a function to


manage tasks using this structure.

5
Conclusion

In conclusion, this document looks at the use of stacks and queues in object-
oriented programming to solve some problems. The abstraction and
encapsulation offered by classes makes it easier to handle these structures,
making their use intuitive and modular. This not only improves the readability and
maintainability of the code, but also allows reuse and extension of functionality
according to the specific needs of the problem. With this approach, efficiency in
problem solving is achieved, from the validation of mathematical expressions to
the management of tasks in operating systems, thus demonstrating its usefulness
in different real-life contexts.

You might also like