package circularlinkedlist; import java.util.Iterator; public class CircularLinkedList implements Iterable { // Your variables Node head; Node tail; int size; // BE SURE TO KEEP TRACK OF THE SIZE // implement this constructor public CircularLinkedList() { }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

package circularlinkedlist;
import java.util.Iterator;

public class CircularLinkedList<E> implements Iterable<E> {



// Your variables
Node<E> head;
Node<E> tail;
int size; // BE SURE TO KEEP TRACK OF THE SIZE


// implement this constructor

public CircularLinkedList() {
}


// I highly recommend using this helper method
// Return Node<E> found at the specified index
// be sure to handle out of bounds cases
private Node<E> getNode(int index ) {

return null;
}


// attach a node to the end of the list
public boolean add(E item) {
this.add(size,item);
return false;

}


// Cases to handle
// out of bounds
// adding to empty list
// adding to front
// adding to "end"
// adding anywhere else
// REMEMBER TO INCREMENT THE SIZE
public void add(int index, E item){

}

 



// remove must handle the following cases
// out of bounds
// removing the only thing in the list
// removing the first thing in the list (need to adjust the last thing in the list to point to the beginning)
// removing the last thing
// removing any other node
// REMEMBER TO DECREMENT THE SIZE
public E remove(int index) {
return null;
}




// Turns your list into a string
// Useful for debugging
public String toString(){
Node<E> current = head;
StringBuilder result = new StringBuilder();
if(size == 0){
return "";
}
if(size == 1) {
return head.item.toString();

}
else{
do{
result.append(current.item);
result.append(" ==> ");
current = current.next;
} while(current != head);
}
return result.toString();
}


public Iterator<E> iterator() {
return new ListIterator<E>();
}

// provided code for different assignment
// you should not have to change this
// change at your own risk!
// this class is not static because it needs the class it's inside of to survive!
private class ListIterator<E> implements Iterator<E>{

Node<E> nextItem;
Node<E> prev;
int index;

@SuppressWarnings("unchecked")
//Creates a new iterator that starts at the head of the list
public ListIterator(){
nextItem = (Node<E>) head;
index = 0;
}

// returns true if there is a next node
// this is always should return true if the list has something in it
public boolean hasNext() {
// TODO Auto-generated method stub
return size != 0;
}

// advances the iterator to the next item
// handles wrapping around back to the head automatically for you
public E next() {
// TODO Auto-generated method stub
prev = nextItem;
nextItem = nextItem.next;
index = (index + 1) % size;
return prev.item;

}

// removed the last node was visted by the .next() call
// for example if we had just created a iterator
// the following calls would remove the item at index 1 (the second person in the ring)
// next() next() remove()
public void remove() {
int target;
if(nextItem == head) {
target = size - 1;
} else{
target = index - 1;
index--;
}
CircularLinkedList.this.remove(target); //calls the above class
}

 

this is SolitaireEncryption.java

public class SolitaireEncryption {

public static char encryptChar(char letter, int key) {
int value = letter - 'a';
int encryptedValue = (value + key) % 26;
char encryptedChar = (char) (encryptedValue+'a');

return encryptedChar;
}


public static char decryptChar(char letter, int key) {
int value = letter - 'a';
int decryptedValue = (value + (26-key)) % 26;
char decryptedChar = (char) (decryptedValue+'a');

return decryptedChar;
}

public int getKey(CircularLinkedList<Integer> deck){ // calls the steps methods
return -1;
}

private static void step1(CircularLinkedList<Integer> deck){
}

private static void step2(CircularLinkedList<Integer> deck){

}
private static void step3(CircularLinkedList<Integer> deck){

}
private static void step4(CircularLinkedList<Integer> deck){

}
private static int step5(CircularLinkedList<Integer> deck){
return -1;
}

public static void main(String[] args) {
CircularLinkedList<Integer> deck = new CircularLinkedList<>();
}
}


}

// It's easiest if you keep it a singly linked list
// SO DON'T CHANGE IT UNLESS YOU WANT TO MAKE IT HARDER
private static class Node<E>{
E item;
Node<E> next;

public Node(E item) {
this.item = item;
}

}

public static void main(String[] args){






}

 



}

5 Assignment
Use CircularLinkedList.java as a starting point to make a new program that
reads in a 'deck' of 28 numbers (from a file, from a command line, or an array,
your choice), asks the user for one or more messages to decrypt, and decrypts
them using the modified Solitaire algorithm described above. Note that if your
program is decrypting multiple messages, all but the first should be decrypted
using the deck as it exists after the decryption of the previous message. (The
first uses the deck provided, of course.)
5.1
Why a Circular Linked List?
We use Circular Linked List here for a few reasons
• Good practice solving and thinking for the upcoming exam.
• The wrap around nature of the Circular Linked List allows us to not worry
about Steps 1 and 2.
• Linked Lists are ideal for these kinds of manipulations, as we can move
entire sections of the list around much, much quicker than an ArrayList.
For example, in step 3, we can split our deck into multiple smaller decks
and recombine them very quickly.
5.2 Output
Your output will be just the decrypted messages
lists of characters without
spaces or punctuation.
Transcribed Image Text:5 Assignment Use CircularLinkedList.java as a starting point to make a new program that reads in a 'deck' of 28 numbers (from a file, from a command line, or an array, your choice), asks the user for one or more messages to decrypt, and decrypts them using the modified Solitaire algorithm described above. Note that if your program is decrypting multiple messages, all but the first should be decrypted using the deck as it exists after the decryption of the previous message. (The first uses the deck provided, of course.) 5.1 Why a Circular Linked List? We use Circular Linked List here for a few reasons • Good practice solving and thinking for the upcoming exam. • The wrap around nature of the Circular Linked List allows us to not worry about Steps 1 and 2. • Linked Lists are ideal for these kinds of manipulations, as we can move entire sections of the list around much, much quicker than an ArrayList. For example, in step 3, we can split our deck into multiple smaller decks and recombine them very quickly. 5.2 Output Your output will be just the decrypted messages lists of characters without spaces or punctuation.
3 Example
As usual, an example will really help make sense of the algorithm. Let's say
that this is the original ordering of our half-deck of cards:
1 4 7 10 13 16 19 22 25 28 3 6 9 12 15 18 21 24 27 2 5 8 11 14 17 20 23 26
Step 1
Swap 27 with the value following it. So, we swap 27 and 2:
1 4 7 10 13 16 19 22 25 28 3 6 9 12 15 18 21 24 2 27 5 8 11 14 17 20 23 26
Step 2
Move 28 two places down the list. It ends up between 6 and 9:
1 4 7 10 13 16 19 22 25 3 6 28 9 12 15 18 21 24 2 27 5 8 11 14 17 20 23 26
Step 3
Do the triple cut. Everything above the first joker (28 in this case) goes to the
bottom of the deck, and everything below the second (27) goes to the top:
5 8 11 14 17 20 23 26 28 9 12 15 18 21 24 2 27 1 4 7 10 13 16 19 22 25 3 6
Step 4
The bottom card is 6. The first 6 cards of the deck are 5, 8, 11, 14, 17, and 20.
They go just ahead of 6 at the bottom end of the deck:
23 26 28 9 12 15 18 21 24 2 27 1 4 7 10 13 16 19 22 25 3 5 8 11 14 17 20 6
Step 5
The top card is 23. Thus, our generated keystream value is the 24 th card, which
is 11.
Repeat
Self Test: What is the next keystream value? The answer is provided at the
end of this document.
Transcribed Image Text:3 Example As usual, an example will really help make sense of the algorithm. Let's say that this is the original ordering of our half-deck of cards: 1 4 7 10 13 16 19 22 25 28 3 6 9 12 15 18 21 24 27 2 5 8 11 14 17 20 23 26 Step 1 Swap 27 with the value following it. So, we swap 27 and 2: 1 4 7 10 13 16 19 22 25 28 3 6 9 12 15 18 21 24 2 27 5 8 11 14 17 20 23 26 Step 2 Move 28 two places down the list. It ends up between 6 and 9: 1 4 7 10 13 16 19 22 25 3 6 28 9 12 15 18 21 24 2 27 5 8 11 14 17 20 23 26 Step 3 Do the triple cut. Everything above the first joker (28 in this case) goes to the bottom of the deck, and everything below the second (27) goes to the top: 5 8 11 14 17 20 23 26 28 9 12 15 18 21 24 2 27 1 4 7 10 13 16 19 22 25 3 6 Step 4 The bottom card is 6. The first 6 cards of the deck are 5, 8, 11, 14, 17, and 20. They go just ahead of 6 at the bottom end of the deck: 23 26 28 9 12 15 18 21 24 2 27 1 4 7 10 13 16 19 22 25 3 5 8 11 14 17 20 6 Step 5 The top card is 23. Thus, our generated keystream value is the 24 th card, which is 11. Repeat Self Test: What is the next keystream value? The answer is provided at the end of this document.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education