Sei sulla pagina 1di 20

FORMATO DE GUA DE PRCTICA DE LABORATORIO / TALLERES / CENTROS DE

SIMULACIN PARA DOCENTES

CARRERA: CIENCIAS DE LA COMPUTACIN ASIGNATURA: ESTRUCTURA DE DATOS


NRO. PRCTICA: 4 TTULO PRCTICA: PILAS
INTEGRANTE(S): SANTIAGO VIZCAINO, ANDRES GUEVARA

OBJECT

General object

To learn the use, implementation and development of stackable data structures


known as stack, with the help of the java programming language.

Specific object

Implementation of the stack type using vectors.


Interpret the code in a program that works with a data structure similar to a stack.
Implement the code for the utility of solving problems

1. THEORETICAL FRAMEWORK

Introduction to java

Java is an object-oriented programing language, is independent which means it can be run


on any operating system with any type of processor as long as the java interpreter is
available on the system. (W3schools.in, 2016)

Some of its history start in the early 90s, extending the power of network computing to
the activities of everyday life was a radical vision. In 1991, a small group of Sun engineers
called the "Green Team" believed that the next wave in computing was the union of digital
consumer devices and computers. Led by James Gosling, the team worked around the
clock and created the programming language that would revolutionize our world Java.
(Oracle, 2017)
One important part of java is its Java Virtual Machine (JVM) this executes java code, but
is written in platform specific languages such as C/C++/ASM, etc. so JVM is not written
in JAVA and hence cannot be platform independent and Java interpreter is a part of JVM.
(W3schools.in, 2016)

Introduction to eclipse

Eclipse, in the late 1990s, IBM began development of what we now know as Eclipse.
Today we see high adoption rates and evidence of successful application of this
technology across the software industry. The purpose of this article is to review the
inception of Eclipse, to illustrate the role it plays in today's development tools, and to
convey how we see the technology evolving over time. (Cernosek, 2005)

Eclipse is a community for individuals and organizations who wish to collaborate on


commercially-friendly open source software. Its projects are focused on building an open
development platform comprised of extensible frameworks, tools and runtimes for
building, deploying and managing software across the lifecycle. The Eclipse Foundation
helps cultivate both an open source community and an ecosystem of complementary
products and services. (Cernosek, 2005)

The Eclipse Project was originally created by IBM in November 2001 and supported by
a consortium of software vendors. The Eclipse Foundation was created in January 2004
as an independent not-for-profit corporation to act as the steward of the Eclipse
community. The independent not-for-profit corporation was created to allow a vendor
neutral and open, transparent community to be established around Eclipse. Today, the
Eclipse community consists of individuals and organizations from a cross section of the
software industry. (Cernosek, 2005)

Stack in Java
The stack is a sequence of elements of the same type in which access to it is done by a
single place called top:
We see how access to the elements of the stack is always done on a single end. The
operations that characterize the stack are to introduce a new element on the top (push)
and extract the element located at the top (pop). One way of looking at this data structure
is like a stack of books in which you can only pick up the book that is on top or stack
more books on it, but the books that hold the stack are not accessible because otherwise
everything would fall apart. (Roldan, s.f.)
Stack static
The elements that make up the stack are stored in arrays (arrays), in the vector container
or forming a linked list. The implementation with an array is static since the array is fixed
in size.
The PilaLineal class, with this representation, needs an array and a numeric variable, top,
that points to the last element placed on the stack. When using an array, it is necessary to
take into account that the size of the stack cannot exceed the number of elements in the
array, and the full stack condition will be significant for the design.
The usual method of introducing elements in a stack is to define the bottom of the stack
in position 0 of the array and without any element inside it, that is, define an empty stack;
then, elements are introduced in the stack (in the array), so that the first element added is
inserted in an empty stack and in position 0, the second element in position 1, the next in
position 2 and so on With these operations, the index pointing to the top of the stack is
incremented by 1 each time a new element is added. The algorithms enter, "insert" (push)
and "remove", remove, (pop). (Martnez, 2008)
Insert (push)
Check if the battery is not full.
Increase the pointer index of the stack by 1.
Store item in the pointer position of the stack.
Remove (pop)
Check if the battery is not empty.
Read the element of the pointer position of the stack.
Decrease the stack pointer by 1.
Class Pila_Lineal
The declaration of an abstract type includes the representation of the data and the
definition of the operations. In the TAD Stack the data can be of any type, and the
operations.
Stack data (Data type is any type of primitive data or class type).
createPile initializes a stack. It is like creating a stack without elements, therefore,
empty.
Check that the battery is not full before inserting or inserting ("push") an element
in the battery; verify that a stack is not empty before removing or removing
("pop") an item from the battery. If these preconditions are not met, an error
message should be displayed, and the program must finish.
pilaVacia returns true if the stack is empty and false otherwise.
Full stack returns true if the stack is full and false otherwise. These two Last
operations are used to verify the preconditions of inserting and removing.
CleanPile empties the stack, leaving it without elements and available for other
tasks.
cimaPila returns the value located at the top of the stack, but does not decrease its
pointer, since the battery remains intact. (Martnez, 2008)

Dynamic stack implementation WITH a vector


The Vector class is a container of objects that can be managed to grow and decrease
dynamically. Vector elements are of type Object. It has methods to assign an element in
a position (insertElementAt), to add an element below of the last (addElement), to obtain
the element that is in a position determined (elementAt), to remove an element
(removeElementAt), Then, it is possible to improve the implementation of the abstract
type Pile using as deposit of the elements a Vector object.
The position of the last element added to the stack is maintained with the variable top.
Initially, top = -1, which is the empty stack condition.
Inserting a new element means increasing the top and assigning the element to the top
position of the Vector. The operation is done by calling the addElement () method, which
adds an element from the last one and, if necessary, increase the capacity. It is not
necessary to implement the pilaLlena () method since Vector's capacity grows
dynamically.
The remove () method returns the top element of the stack and removes it. When using a
Vector, calling removeElementAt (top) the top element is removed, and then decreases
top. (Martnez, 2008)

1. DEVELOPMENT OF AUTO-PRACTICE

STATIC STACK
To create a stack in java we must define the size of the stack, 2 classes will be used,
in the first class called Method_pila the different methods that allow the creation of
the stack will be implemented. In the second class called Main_pila, will be where
the stack size will be defined, and a menu will be available to perform the different
operations with the stack.
Part 1: Determine the size of the stack

In this part, we must enter the

Illustration 1: size of the stack

Part 2: Stack operations

This part will be divided into 5 fundamental stack operations.

Illustration 2: Metodos_pila class.


1. Empty. This method allows to give the state of the stack. If the stack is empty,
a Boolean will return true.
This method also allows to solve the problem of underflow, this happens when
you want to eliminate an element from an empty stack.

Illustration 3: empty method algorithm.

2. Full. This method allows to give the state of the stack. If the stack is full, a
Boolean value will return true.
This method also allows you to solve the overflow problem, this happens when
you want to add an element to a full stack.

Illustration 4: full method algorithm.

3. Push. This method, if the stack is not full, allows you to add an element to the
stack.

Illustration 5: push method algorithm.


4. Pop. This method, if the stack is not empty, allows you to remove an element
from the stack.

Illustration 6: pop method algorithm.

5. Show Stack. This method, if the stack is not empty, allows to show all the
elements of the stack.

Illustration 7: show stack method algorithm.


Part 2: Menu.

In the main_pila class, a menu is implemented, which is useful in a dynamic way with
the user to relate the operations that this requires.

The menu will be available until the user enters the number 0.

Illustration 8: men algorithm.

Part 3: desktop test

In this part, we can determinate the size of stack.

Illustration 9: size of stack


In this part, we can see the menu.

Illustration 10: menu.

In this part, we can see the method push working. And overflow problem solved.

Illustration 11: add element to Stack.


Illustration 12 overflow problem solved.

In this part, we can see the method pop working. And underflow problem solved.

Illustration 13: underflow problem solved.


Illustration 14: delete element to Stack.

In this part, the stack is showing.

Illustration 15: show stack.


DYNAMIC STACK

For the creation of the dynamic stack we will need a java API class that is the Linked List
which can be seen as a kind of vector in which we can store an infinity of data until the
machine supports it since it has not defined a specific limit.

For that we call the methods that this class contains in the following way:

LinkedList list = new LinkedList ();

With the creation of the list object we are going to use the methods of this class and in
fact the list is already defined.

To interact with our program, we will create a menu of operations where we will enter,
delete, and values.

With the help of the switch cycle that is implemented to validate menus where the user
decides what they want to do.

For case 1 we are going to add values to the stack, it is worth mentioning that if we
type that option, it will ask for a value and we will return to the options menu.

With the help of the add-First method, it locates the value that we enter at the
beginning of the list.

For the case 2 in which the elimination of a value is done, we use the remove-First
method which eliminates the first value from the list.
For case 3 we are going to use the methods of the interator class which will help us to
print the data of the list for it, we use the while loop in which we condition through
the method hasNext () which returns if the object is not the last one of the list while
the cycle is true will send the data to the console.

For the last case (4) we are going to use the clear () method that is inside the
LinkedList class, which removes the data from the stack.

Now we will go to the execution of the programs.

Stack Dynamic

Now we will proceed with dynamic batteries.

In the same way we will manage an operations menu similar to the static stack program
in this case we will select option 1 to enter a data to the list, but it is worth mentioning
that once we enter the values we will return to the options menu and also as it is dynamic
the spaces are going to be created as we are loading values into said list.
In this case, enter two values to the list and effectively visualize the data that is loaded.

Now we are going to eliminate a data of the list for it we are going to execute the option
2 and then we are going to realize the impression of data.

Effectively the data was deleted and at the time of printing the data the number does not
appear which its position was taken by the number 34.

In this way it is how a dynamic stack is executed, it is possible to add or eliminate


elements that contain said list intuitively and we do not waste memory resources.
STACK JAVA LIBRARY

The java.util.Stack library represents a last-in-first-out (LIFO) stack of objects. It extends


class Vector with five operations that allow a vector to be treated as a stack. The usual
push and pop operations are provided, as well as a method to peek at the top item on the
stack, a method to test for whether the stack is empty, and a method to search the stack
for an item and discover how far it is from the top.
When a stack is first created, it contains no items.
With this way of creating Stack we can use not only the element that is on the top of the
stack, but you can also get any value that is in the stack with the help of their methods.

This package contains the 2 classes:


1. Class constructors.

Stack() This constructor creates an empty stack.

2. Class methods

empty() This method tests if this stack is empty.

peek() This method looks at the object at the top of this stack without
removing it from the stack.

push(E item) This method pushes an item onto the top of this stack.

search(Object o) This method returns the 1-based position where an


object is on this stack.

pop() Removes the object at the top of this stack and returns that
object as the value of this function.

Part 1: Stack algorithm.

To use the Stack class, the first thing to do is import the util.Stack class

Illustration 16: import class.


The second action to use the Stack class is to instantiate an object to the class

Illustration 17: instantiate an object.

Finally the use of methods that can be used according to the needs of the practice. in
this practice the push and pop method has been used.

Illustration 18: used methods.


2. RESOURCES USED
java
computer
Internet
3. RESULTS
- STATIC STACK

With this program we got a way to make a Static Stack.

The result of the algorithm is the creation of a Static Stack.


- STACK LYBRARY

With this library we got a way to make a Stack.

The result of this implementation is the creation of a Stack.


4. CONCLUSIONS

Finally we can conclude that Stack are structures that allow storing
information, this information is stored in a specific order, the only way to
retrieve the information is in the LIFO mode. That is, the last one to enter
will be the first one to leave.

Stacks like any other information container, can suffer from problems such
as overflow and underflow. For this, the empty and full methods must be
implemented correctly, which will allow solving these problems.

The batteries are used in real life, for when another program is called, the
program that is running does not lose the information. That is to say, they
allow to save the state of the variables at the time the call is made, to
continue occupying them when returning from the subprogram.

5. RECOMMENDATIONS
The teacher should implement the use of the stacks in an easy problem, so that
the student can understand in a better way the use of these.
Investigate the relationship between these types of data storage in the creation
of more complex programs and even adapt them to the knowledge that has
been acquiring.

Potrebbero piacerti anche