Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chow6542_a03;
- public class LinkMaster<T> {
- private class Node<T> {
- private T data;
- private Node<T> link;
- public Node() {
- data = null;
- link = null;
- }
- public Node(T newData, Node<T> linkValue) {
- data = newData;
- link = linkValue;
- }
- }
- private Node<T> head;
- public LinkMaster() {
- head = null;
- }
- public void addToStart(T itemData) {
- head = new Node<T>(itemData, head);
- }
- public boolean deleteHeadNode() {
- if (head != null) {
- System.out.println();
- System.out.println("eliminate Suitor # " + head.data);
- head = head.link;
- return true;
- } else {
- return false;
- }
- }
- public void removeNode(int deleteSpot){
- Node<T> position = head;
- for(int i = 1; i < deleteSpot - 1; i++){
- position = position.link;
- }
- System.out.println();
- System.out.println("eliminate Suitor # " + position.link.data);
- position.link = position.link.link;
- }
- public void removeSuitors(int size){
- int spot = 1;
- while(size > 1){
- for(int i = 0; i < 2; i++){
- if(spot == size){
- spot = 1;
- } else if(spot > size){
- spot = 1;
- } else {
- spot++;
- }
- }
- if(spot == 1){
- deleteHeadNode();
- size--;
- } else {
- removeNode(spot);
- size--;
- if(spot > size){
- spot = 1;
- }
- }
- outputList();
- }
- }
- public int size() {
- int count = 0;
- Node<T> position = head;
- while (position != null) {
- count++;
- position = position.link;
- }
- return count;
- }
- // other methods similar to LinkedList3
- // print the sequence of suitor eliminations
- // after each elimination, the resulting list is printed
- public void printWinningPosition() {
- Node<T> position = head;
- System.out.println();
- System.out.println("The winning position is: " + position.data);
- }
- // print the list of suitors
- public void outputList() {
- Node<T> position = head;
- while (position != null) {
- System.out.print(position.data + " ");
- position = position.link;
- }
- }
- }// end LinkedList3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement