0% found this document useful (0 votes)
8 views4 pages

Java Linked List Lab Exercises

This document provides instructions for three linked list problems: 1) Implementing a circular singly linked list with operations like insertion, deletion, and searching. 2) Modeling the Josephus problem of eliminating people in a circle using a counting approach with a circular linked list. 3) Completing functions for a doubly linked list like insertion, deletion, and traversal in both directions.

Uploaded by

chungdmse171186
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)
8 views4 pages

Java Linked List Lab Exercises

This document provides instructions for three linked list problems: 1) Implementing a circular singly linked list with operations like insertion, deletion, and searching. 2) Modeling the Josephus problem of eliminating people in a circle using a counting approach with a circular linked list. 3) Completing functions for a doubly linked list like insertion, deletion, and traversal in both directions.

Uploaded by

chungdmse171186
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

Linked List

==============================================================================
REMEMBER 1: On the top of every program, including the following comment header:
/* Name: Your Name

Student Code: Your Student Code

Purpose: A short description of the program/exercise.

*/
REMEMBER 2: You must include a [Link] file. The file contains instructions on how to
compile and run your code.
==============================================================================

1. Introduction:

You have learnt about Linked List data structure. This lab will help you go through some
practice manners of Linked List data in Java programming language.

2. Problems:

Problem I: Circular Linked list

A circular list is a linked list in which the last link points back to the first link. There are many
ways to design a circular list. Sometimes there is a pointer to the "start" of the list. However,
this makes a list less like a real circle and more like an ordinary list with its end attached to its
beginning.

Make a class for a singly linked circular list with no end or beginning. The only access to the list
is a single reference, current, that can point to any link on the list. This reference can move
around the list as needed. (See next problem for a situation in which such a circular list is ideally
suited.).

Your list should handle insertion, searching, and deletion. You may find it convenient if these
operations take place one link downstream of the link pointed to by current. (Because the
upstream link is singly linked, you can't get at it without going around the circle.)

You should also be able to display the list (although you'll need to break the circle at some
arbitrary point to print it on the screen). A step() method that moves current along to the next
link might come in handy too.

============================================================
Problem II: Josephus Problem ([Link]

The Josephus Problem is a famous mathematical puzzle that goes back to ancient times. There
are many stories to go with the puzzle. One is that Josephus was one of a group of Jews who
were about to be captured by the Romans. Rather than be enslaved, they chose to commit
suicide. They arranged themselves in a circle and, starting at a certain person, started counting
off around the circle. Every nth person had to leave the circle and commit suicide. Josephus
decided he didn't want to die, so he arranged the rules so he would be the last person left. If
there were (say) 41 people, and he was the 16th person from the start of the circle, what
number should he tell them to use for counting off? The problem is made much more
complicated because the circle shrinks as the counting continues.

The problem—given the number of people, starting point, direction, and number to be
skipped—is to choose the position in the initial circle to avoid execution.

Task:

- Create an application that uses a circular linked list (like that in Programming Project
5.3) to model this problem.
- The application must ask for user inputs:
o the number of people in the circle
o the number used for counting off
o the number of the person where counting starts (usually 1).
- The application must print out the output: the list of people being eliminated in order
(assuming you go around clockwise).

See Figure 1 below for a visualization of an example. There are 41 soldiers numbered 1 through
41. They start counting at 1 and with a step size of three. The last two soldiers remaining are
#16 and #31. Note: if the counting continues with those two soldiers, then the last one
remaining is #31 only. Figure 2 shows the example input and output that your application
should have.
Figure 1: Claude Gaspar Bachet de Méziriac's interpretation of the Josephus problem with 41
soldiers and a step size of 3, showing that places 16 and 31 are last to be killed – time
progresses inwards along the spiral, green dots denoting live soldiers, grey dead soldiers, and
crosses killings.

Figure 2: Example input and output

============================================================

Problem 3: Doubly Linked list

Complete and submit the in-class lab assignment

Download file [Link]

Complete the functions:

• insertFirst
• insertLast
• deleteFirst
• deleteLast
• deleteKey
• displayForward
• displayBackward

Common questions

Powered by AI

The core challenge of the Josephus problem lies in determining the safe position in a shrinking circle where every nth person is eliminated. A circular linked list is well-suited to model this scenario because it can simulate the continuous aspect of counting around a circle without needing to reset or create additional data structures. The circular linked list allows seamless wrap-around logic while progressively reducing the list size, thus accurately representing the problem dynamics .

Including a README.txt file is crucial as it provides necessary instructions and context for users or reviewers to understand, compile, and run the program. Typically, a README should contain the purpose of the program, how to install or set up the environment if needed, how to compile the code (including necessary tools and commands), and any additional configuration or usage instructions. It serves to enable users to efficiently and effectively interact with the software .

Bidirectional traversal in a doubly linked list enhances versatility and efficiency by enabling direct navigation to previous nodes, simplifying algorithms like reverse traversal, backward searches, and easier deletion without needing auxiliary structures. This capability increases operational options and enhances algorithmic performance, especially where reverse operations are crucial, reducing the need to re-traverse from the head and significantly improving real-time data manipulation and computational efficiency .

When handling user inputs for the Josephus problem, the application must ensure valid integer inputs for the number of people, counting step, and starting point. Logical considerations include managing the counting in a circular fashion with wrap-around logic inherent in a circular linked list, adjusting the list indices to fit a 1-based numbering for ease of user understanding, and appropriately decrementing the list size as people are iteratively removed. These considerations impact the accuracy and reliabilities of the simulation .

The primary operations on a doubly linked list include `insertFirst`, `insertLast`, `deleteFirst`, `deleteLast`, `deleteKey`, `displayForward`, and `displayBackward`. These operations leverage pointers to both previous and next nodes, allowing bidirectional traversal and more flexible deletion and insertion at both ends of the list. Compared to a singly linked list, a doubly linked list allows more efficient backward traversal and direct access for deletion without needing to reset from the list head, which simplifies many algorithms .

A singly linked circular list benefits software applications by supporting continuous looping through data without additional logic, making it useful in applications requiring repeated access or traversal from any node. However, its drawbacks include challenges in implementing forward-only traversal, which complicates the pointing and re-pointing logic, especially during deletions. Also, since it's singly linked, accessing nodes backwards is inefficient and can require substantial extra logic, which might lead to increased complexity for certain operations .

The algorithmic complexity of inserting and deleting elements in a doubly linked list is O(1) for operations at the head or tail due to direct access pointers, allowing for constant time complexity. However, inserting or deleting a specific key (not at the head or tail) is O(n) in the worst case as it requires traversal from either end to find the node, where n is the number of elements. This highlights the efficiency of access at the list extremes and the trade-off in searching elsewhere .

A circular linked list differs from a traditional linked list in that the last node points back to the first node, creating a loop. This allows traversal from any point in the list without needing to start from the beginning or end. Consequently, insertions and deletions can behave differently because the reference to `current` can simplify operations by only having to consider the downstream node for modification. This design makes operations like moving `current` with a `step()` method efficient, but it complicates operations like deletion, which require caution to avoid breaking the circle .

Understanding the Josephus problem aids in designing algorithms by providing insights into efficiently managing circular data structures and optimizing decision-making processes in constrained environments. Applications include optimizing resource allocation where sequential removal or prioritization is necessary, designing fair round-robin scheduling systems, and enhancing fault-tolerant computing frameworks that require cyclic redundancy checks. Mastery of such combinatorial problems drives innovation in algorithm design for complex iterative processes .

When implementing the `step()` method in a circular linked list, considerations include ensuring the method accurately updates the `current` pointer to the next node, accommodating the list structure by wrapping around to the start if the end is reached. Additionally, it must maintain the integrity of links to prevent breaking the circular nature, and should handle edge cases such as empty lists gracefully. Efficiency is crucial to keep the traversal swift, minimizing unnecessary computation .

You might also like