Вы находитесь на странице: 1из 3

Lab 2 Report

Author: Jorge Berumen


CS2302 – Data Structures

Introduction
The purpose of this lab was to review the stack, queue, and linked list data structures by implementing
them and utilizing them to solve practical problems such as arithmetic evaluation and element
counting. Although seeming as completely rudimentary at first glance, these data structures serve as the
building blocks of many contemporary algorithms that allow programmers to solve modern problems,
like complex graph-theory problems such as network mapping, with surprising ease.

Proposed solution and design implementation

Linked List Subset


To imitate a subset that has universe u={0 ,1 , 2 , ,9} with linked lists, the programmer can
easily do so by instantiating it with ten elements of type boolean – the value that indicates
whether the subset contains terms t n ∈[0, 9] where n is the index. Nonetheless, because such
algorithm design is used, it is easy to accomplish the common operations that are typically
performed on subsets since the operations are being performed on mere boolean states rather
than on the actual elements of the subset. For example, in order to accomplish a union, a method
simply returns a new subset containing a gross-accumulation of all the true elements. All of the
operations required by the lab follow this simple, yet effective notion to properly imitate a
subset.

Arithmetic Parsing and Evaluation


In order to evaluate an expression, a system must be followed in order to even begin parsing the
expression. Granted the fact that the expression is syntactically correct and that it does not
violate any predefined restrictions, the programmer can proceed to parse the expression using
stacks and queues. Since the program reads the expression as a String object, this algorithm
first attempts to read each character in order to appropriately push operators on the stack instead
of a number. It accomplishes this task by using helper methods the indicate whether the passed
String is either a number or an operator. But what if the number results in being more than
just one digit, or perhaps even a decimal? To tackle this problem, we use a
StringTokenizer that read the entire number and a counter that keeps track of where the
algorithm is currently reading the expression at. With this method, the user receives the benefit
of being able to operate on numbers greater than 9 and less than 1.

In order to parse a correct-syntax arithmetic expression, the programmer must convert it into its
post-fix notation to properly evaluate it. Since we are, indeed, reading numbers with more than
one digit, we will use a queue that holds objects that can represent either a number
(Double)or an operator (Character). Such approach is possible due to the dynamic
versatility of an object-oriented programming language – namely the usage of the data type
Object. The algorithm converts the expression by pushing and popping the characters
according to their precedence whilst appending them to a queue that contains the numbers read.
The algorithm evaluates the expression by using a queue that helps it read the post-fix
expression regardless of how long it may be.
However, there is a drawback to this approach. Since the algorithm depends on reading a digit
in order to begin assuming it might be a number, the instance of typing .5 as the representation
of one-half will be parsed incorrectly, consequently ending with an error. To correctly represent
numbers below 1, the user must provide a leading 0 to denote the entity as a number, i.e. 0.5 for
last example. Similarly, the same thing goes for whole numbers that are interpreted with
insignificant zeros past the decimal. If, for instance, the algorithm reads a 2.0 in the
expression , it does not account for the extra 2 characters. To fix it, the user must omit the zero;
in other words, 2.0 must be typed as 2. Nevertheless, both issues could have easily been
resolved by using a specialized StringTokenizer method that also returns the delimiter as
a token. Such approach would have allowed us to rid ourselves from keeping track of where in
the expression we were reading at and allow us to focus more on the content.

The entire application is structured in three packages. Labone package contains methods that read
inputs whilst handling wrong input. LabTwo package contains the source files that wrap the logic for
the application, such as input reading and execution. Labtwo.structures package contains all the
structures utilized in lab 2. The main source file Main.java wraps both parts in an easy-to-follow code
that displays a simple menu.

Experimental Results

Linked List Subset


The first part of the lab was tested by copy-and-pasting the code provided in the lab, and comparing to
the actual results. Below are the diagrams of the expected results and actual results:

{0,4}
{0,5}
{0,4,5}
{0}
{1,2,3,6,7,8,9}
{(0,0),(4,0)}

Anticipated Results Actual Results1


Figure 2.1

1 The set { 1 2 3 5 6 7 8 9 } is simply the compliment of D and should be omitted when comparing the anticipated results
with the actual results.
Arithmetic Parsing and Evaluation
If the restrictions described in the previous section are followed, the algorithm correctly evaluates
arithmetic expressions including decimals and powers:

Type Evaluated Expression

Addition

Subtraction

Multiplication

Division

Exponential

Parenthesis

Table 2.2

Вам также может понравиться