Download D0b3 Algorithmics: Exercises on Linear Data Structures and ADTs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
D0b3 Algorithmics: Exercises on Linear Data
Structures and ADTs
These exercises will not contribute to your overall mark for this course, but will give
you valuable hands-on experience of building and using the kinds of data structures
we have been talking about in lectures. Don’t feel that you have to do all of the
problems listed below, but if you want to hand in any of them by the 31st of January, I
will mark them for you.
i)
Implement the Stack ADT in a Java class using a Vector as the underlying
data structure.
What are the relative merits of your new Stack class as compared to the
StackArray and StackList classes we discussed in lectures?
ii)
Use an implementation of the Stack ADT to implement a post-fix
calculator.
iii)
A Stack can be used for a neat implementation of in-fix to post-fix
expression conversion. Implement this algorithm and combine it with your
answer to (ii) to obtain a command line in-fix calculator.
The algorithms for (ii) and (iii) are discussed in many of the books on the
reading list, in particular, Chapter 12 of Wiener and Pinson deals with
them directly. I can give you a photocopy if you have trouble tracking this
book down.
iv)
Implement a Queue ADT using a Vector as the underlying data structure.
Compare and contrast this implementation with QueueArray and
QueueList?
v)
Implement a priority Queue. Each item stored in a priority Queue has an
associated numerical priority. Items with high priority are dequeued before
any items with lower priority, even if they were enqueued after.
Hints: Use a sequence as the underlying data structure.
Think carefully about what objects exist in your program before
you start coding. Draw a class relationship diagram first.
Priority queues are essential in operating systems for scheduling prioritised
processor jobs.
vi)
Implement a Sequence ADT using a doubly linked list. Draw before and
after diagrams for each operation and be careful to identify all the special
cases. Test all the operations thoroughly.
If you can get this working, I think you’ve mastered the idea of linked lists
and linear data structures.
Can you think of a more economical data structure that can be used to
implement a Sequence efficiently? I.e., one that:
- is truly dynamic (no overflow, no wasted space),
- requires only one link at each node,
- has a single point of access,
- allows addition and deletion of elements at either end of the sequence
without chaining.
vii)
Implement a “Towers of Hanoi” game using appropriate data structures
from the selection you have seen. The user of your program should be
asked for a tower from which to take the top item and a new tower on
which to place it. Your system should check that the move is valid and, if
so, display the new configuration of the towers until the game is over.
Here is the starting configuration for the game:
!
!
#!#
!
!
##!##
!
!
###!###
!
!
####!####
!
!
#####!#####
__________________________________________________
1
2
3
The user enters 3 and 2, to indicate that the top block of tower 3 should be
moved onto the top of tower 2, which results in the following new
configuration:
!
!
!
!
!
##!##
!
!
###!###
!
!
####!####
!
#!#
#####!#####
__________________________________________________
1
2
3
In case you don’t know… the game is over when you get the blocks in
increasing width order on tower 1, i.e., in this configuration:
#!#
!
!
##!##
!
!
###!###
!
!
####!####
!
!
#####!#####
!
!
__________________________________________________
1
2
3
And you cannot place a wider block on top of a narrower one at any stage
during the game.
This document was created with Win2PDF available at http://www.daneprairie.com.
The unregistered version of Win2PDF is for evaluation or non-commercial use only.