共计 2038 个字符,预计需要花费 6 分钟才能阅读完成。
CSC 115 – Lab#6 Stacks
Objectives
During Lab#6, week, you will be completing a programming exercise that you will hand in
through conneX. You will learn:
How to implement common operations related to Stacks based on a singly-linked list.
How to solve a practical problem using a Stack.
How to build a tester and debug your codes in Java.
Required files
You will need to hand in two java files that you complete:
Stack.java
BracketBalance.java
The following files are provided as helper files:
Node.java
Tester.java
Stack Review
Though we usually line up as a Queue to pay our groceries in stores, we use Stack unconsciously
daily: surfing the internet using browsers, when we go back to the previous page, we
click on the <= button. The websites that we have just visited are actually stored in a Stack.
A Stack is an abstract data type which is used to store a collection of data. It has the property
that the last item placed on the stack will be the first item removed. The property is referred
to as Last-in, first-out (LIFO). Common operations related to Stack are:
empty – check if the stack is empty
push (item) – push the item on to the stack
peek – check what is on top of the stack. The stack is not changed.
pop – remove the item at the top of the stack. The stack is reduced by one.
Your Task: Download Stack.java and implement the methods. Notice that the Node class
Page 1 of 2
will be used in Stack class and you should download this file as well.
Stack Application – Bracket Balancing
Stack is very useful to check if braces are balanced in mathematic expressions. The algorithm
can be:
For a sequence of characters
If there is an open brace“{”, or bracket“(”, or square bracket“[”, then push it onto the stack;
If there is an closing brace“}”, or bracket“)”, or square bracket“]”, then:
If the stack is empty, then it is not balanced
Else
Pop the stack;
If the popped character is not the matching open brace/bracket/square-bracket, then
the sequence is not balanced.
After the sequence is processed, it is safe to say that it is balanced.
Your Task: Download BracketBalance.java and finish the implementation.
The Test.java program will give a Pass or Fail depending on the automated test results. Note
that a Pass is not an indication that everything is perfect, the intention is to reward effort, and
give enough feedback so you can seek guidance if you see that you can benefit from it.
Document created by: Tianming Wei on February 25, 2019
WX:codehelp