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

ICS 46 Data Structures - Homework 4

Homework 4 requires the implementation of Stack and Queue data structures using both array and linked list methods, along with empirical performance testing of these implementations. Students will also write a function to check if parentheses are balanced using a stack, and will need to submit their work via GitHub to GradeScope. The document outlines the necessary class hierarchy, testing procedures, and submission requirements.

Uploaded by

kamishwang
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 views5 pages

ICS 46 Data Structures - Homework 4

Homework 4 requires the implementation of Stack and Queue data structures using both array and linked list methods, along with empirical performance testing of these implementations. Students will also write a function to check if parentheses are balanced using a stack, and will need to submit their work via GitHub to GradeScope. The document outlines the necessary class hierarchy, testing procedures, and submission requirements.

Uploaded by

kamishwang
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/ 5

2025/2/4, 21:46

Homework 4: Implementing
Stacks and Queues using both
Array and LinkedList
Overview and Objectives
Implementation of Classes and Files
1 Class Holder Hierarchy
2 Function balancing parentheses
Empirical Testing of Data Structures
Sample spreadsheet for your data - make a copy
How to Test
How to Submit

Overview and Objectives

For Homework 4, implement the data structures Stack and


Queue, each using two different implementations with array and
linked list, and empirically measure the performance of all 4 concrete
classes. To ease our implementation, I introduce a common base
class for Stack and Queue— class Holder. The purpose of class
Holder is to define a common interface for the testing framework to
test all concrete implementations of Stack and Queue. Both data
structures hold elements, add them, and remove them; what varies is
the order they come out.

In addition, write a program with a function that uses a stack data


structure, an std::stack<char>, to determine if parentheses are
properly nested and balanced within a given parameter string. This
particular problem requires a stack data structure to solve it.

Relation to course objectives

With Homework 4, you learn about the behaviors of stacks and


queues, how to implement them efficiently, and how to code an
example application that uses a stack.

https://docs.google.com/document/u/1/d/e/2PACX-1vQg7QSymE8L…QQtCKa80Mq5xKvmE9gqToh3T_rrsnS4dU5q5wmZR/pub?embedded=true Page 1 of 5
2025/2/4, 21:46

Implementation of Classes and Files

1 Class Holder Hierarchy

In files holder.h, holder.cpp, and main.cpp, declare, define,


and use a class hierarchy with a base class named Holder, and two
derived classes named Stack and Queue, with the methods given in
the starter code on GitHub, including the helper functions that
provide a framework for measuring the performance of the methods
insert(), remove(), and peek().

Our goal is to achieve performance of these three correctly


implemented methods. All key operations of our
Holder implementations can be done in constant time. (Printing the
values contained in any holder must be , but print() is not a
key operation— it is used only for debugging.)

Code for holder.h and other needed files may be provided


on the hw4 repo on GitHub.

Holder

Stack Queue

ArrayStack LinkedStack ArrayQueue LinkedQueue

The implementations of array and linked list are similar to what you
have done in earlier homework assignments. ArrayStack and
LinkedStack are especially straightforward— implemented
similarly to UnorderedList using Array and LinkedList.

Both Queue implementations are more interesting.


LinkedQueue will require modification by adding a tail pointer, so
that we can access the tail of the list in constant time.
ArrayQueue will require a new technique, because the contents of
the queue will move within the array as we insert and remove
https://docs.google.com/document/u/1/d/e/2PACX-1vQg7QSymE8L…QQtCKa80Mq5xKvmE9gqToh3T_rrsnS4dU5q5wmZR/pub?embedded=true Page 2 of 5
2025/2/4, 21:46

elements. The naive implementation using copy_down()and


copy_up() operations is very inefficient. Instead, we use a circular
buffer implementation, which will allow the elements to be added
and removed within the queue, by wrapping around within the array
to avoid any expensive array-element copy operations. Another tricky
issue with the circular buffer is how to distinguish between the
conditions is_full() and is_empty().

2 Function balancing parentheses

In files named is_balanced.h, is_balanced.cpp, and


is_balanced_main.cpp, write a function bool
is_balanced(string parens) that returns true only if the
parentheses given in the parameter string are balanced properly.
This function handles four types of parens {}[]()<> and allows
nesting and adjacent parens. For example, (()())() is
balanced, but not (() or ()) which have unmatched parens. Use
std::stack<char> to handle the nesting of parentheses.

For handling only one type of paren, a single integer counter


can represent the stack. It is tempting to believe this problem
can be solved with 4 counters, but a stack of char is the right
way to solve it.

Here are some examples of use:

● is_balanced(“({(())})((([({})])))(((((<>
([{()}])(<>))))))()”)
returns true because all are correctly matched and balanced

● is_balanced(“({(<>)})((([({})])))(((((()
([{()}])(())))))”)
returns false because it is missing a closing paren

● is_balanced(“({(<>)})((([({})])))((((([]
([{<>}])(()))))))()])”)
returns false because it has too many closing square brackets

For computing the time complexity of this function, assume that N is


the length of the string parameter parens.

Empirical Testing of Data Structures

https://docs.google.com/document/u/1/d/e/2PACX-1vQg7QSymE8L…QQtCKa80Mq5xKvmE9gqToh3T_rrsnS4dU5q5wmZR/pub?embedded=true Page 3 of 5
2025/2/4, 21:46

With the same general method as Homework 3, empirically measure


the execution times of the operations insert() and remove() for
all Holders with std::string and with a sample input file
words.txt derived from the Linux dictionary. Use only this
alphabetically sorted list, which will make it easy to see if the order of
your data structure is working correctly. The file words.txt contains
45,392 words, defined as constexpr NWORDS at the top of
holder.h.

Use the same measurement framework concept we used in


Homework 3, which divides the data into K partitions, then measures
performance of each algorithm (insert, remove) for each concrete
data structure (ArrayStack, LinkedStack, ArrayQueue,
LinkedQueue).

Fill in the 4 tables of the results, with their accompanying graphs. In


addition, fill in the table with the time complexity of methods for
ArrayStack, LinkedStack, ArrayQueue, LinkedQueue. Verify
that all your operations are for all four concrete classes.

Sample spreadsheet for your data - make a copy

How to Test
In the file student_gtests.cpp, write good GTests for Stack and
Queue. Apply them to all four concrete class implementations.

Set NWORDS to a smaller number, like 450 for initial testing. Be sure
to print out your data structures to be sure items being removed
are coming out in the correct order. Once your program seems to
be working correctly, run it on the full-size word files. Try it on
words.txt which is sorted, making it easier to see if words are
coming out in the correct order.

How to Submit
In GradeScope for Homework 4, upload from GitHub the files
detailed above:

● .Analysis_46HW4
● Timer.h
● holder.h
● holder.cpp

https://docs.google.com/document/u/1/d/e/2PACX-1vQg7QSymE8L…QQtCKa80Mq5xKvmE9gqToh3T_rrsnS4dU5q5wmZR/pub?embedded=true Page 4 of 5
2025/2/4, 21:46

● main.cpp
● student_gtests.cpp
● is_balanced.h
● is_balanced.cpp
● is_balanced_main.cpp
● words.txt

https://docs.google.com/document/u/1/d/e/2PACX-1vQg7QSymE8L…QQtCKa80Mq5xKvmE9gqToh3T_rrsnS4dU5q5wmZR/pub?embedded=true Page 5 of 5

You might also like