SlideShare a Scribd company logo
1 of 4
Download to read offline
In an ancient land, the beautiful princess Eve had many suitors She decided on the following
procedure to determine which suitor she would marry First, all of the suitors would be lined up
on after the other and assigned numbers The first suitor would be number 1, the second number
2, and so on up to the last suitor, number n starting at the first suitor she would then count three
suitors down the line (because of the three letters in her name) and the third suitor would be
eliminated from winning her hand and removed from the line Eve would then continue, counting
three more suitors, and eliminating every third suitor When she reached the end of the line she
would continue counting from the beginning. For example, if there were six suitors then the
elimination process would proceed as follows: Write a program that creates a circular linked list
of nodes to determine which position you should stand in to marry the princess if there are n
suitors Your program should simulate the elimination process by deleting the node that
corresponds to the suitor that is eliminated for each step in the process.
Solution
import java.util.Scanner;
import java.util.Stack;
public class CircularLinkedListImpl {
private Node node = null;
private Node head = null;
private Node tail = null;
private static int listCount = 0;
public CircularLinkedListImpl() {
}
/* Function to delete element at position */
public void deleteAtPos(int pos) {
if (this.listCount == 1 && pos == 1) {
head = null;
tail = null;
this.listCount = 0;
return;
}
if (pos == 1) {
head = this.head.next;
tail.next = head;
listCount--;
return;
}
if (pos == listCount) {
Node s = head;
Node t = head;
while (s != tail) {
t = s;
s = s.next;
}
tail = t;
tail.next = head;
listCount--;
return;
}
Stack> stack = new Stack>();
Node ptr = head.next;
pos = pos - 1;
for (int i = 1; i < listCount - 1; i++) {
if (i == pos) {
System.out.println("kfdsf" + ptr.item);
Node tmp = ptr.next;
tmp = tmp.next;
ptr.next = tmp;
break;
}
stack.push(ptr);
}
listCount--;
}
public boolean add(E element) {
boolean isItemAdded = false;
Node tempNode = new Node(element);
Node node = this.node;
if (node == null) {
this.node = tempNode;
this.head = this.node;
this.tail = this.node;
CircularLinkedListImpl.listCount++;
isItemAdded = true;
} else {
while (node.next != null) {
System.out.println(node.next.item);
node = node.next;
}
tempNode.next = node.next;
node.next = tempNode;
this.tail = tempNode;
CircularLinkedListImpl.listCount++;
isItemAdded = true;
}
return isItemAdded;
}
public int size() {
return CircularLinkedListImpl.listCount++;
}
public String toString() {
String output = "";
if (this.node != null) {
Node crunchifyCurrent = this.node;
while (crunchifyCurrent != null) {
output += "[" + crunchifyCurrent.item.toString() + "]";
crunchifyCurrent = crunchifyCurrent.next;
}
}
return output;
}
public static void main(String[] args) {
CircularLinkedListImpl c = new CircularLinkedListImpl();
Scanner scan = new Scanner(System.in);
String str = scan.next();
int length = str.length();
long bInetger = Long.parseLong(str);
scan.close();
long ll = getDigit(length);
while (ll > 0) {
long value = bInetger / ll;
bInetger = bInetger % ll;
ll = ll / 10;
c.add(value);
}
System.out.println(" List " + c.toString());
c.deleteAtPos(3);
System.out.println("After deleting List " + c.toString());
}
private static class Node {
E item;
Node next;
Node() {
}
Node(E element) {
this.item = element;
this.next = next;
// this.prev = prev;
}
}
public static long getDigit(int length) {
StringBuilder ll = new StringBuilder();
ll.append(1);
for (int i = 1; i < length; i++) {
ll.append(0);
}
return Long.parseLong(ll.toString());
}
}

More Related Content

Similar to In an ancient land, the beautiful princess Eve had many suitors She d.pdf

I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfarkmuzikllc
 
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdfpasqualealvarez467
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointerskinan keshkeh
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 
Assume Hashtable is a simple array of size 8, with indices 0..7. Num.pdf
Assume Hashtable is a simple array of size 8, with indices 0..7. Num.pdfAssume Hashtable is a simple array of size 8, with indices 0..7. Num.pdf
Assume Hashtable is a simple array of size 8, with indices 0..7. Num.pdfarihantmum
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptxSajalFayyaz
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYvikram mahendra
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docxajoy21
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queuessuser7319f8
 
Link list part 2
Link list part 2Link list part 2
Link list part 2Anaya Zafar
 

Similar to In an ancient land, the beautiful princess Eve had many suitors She d.pdf (20)

I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
 
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Data structure
Data  structureData  structure
Data structure
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Assume Hashtable is a simple array of size 8, with indices 0..7. Num.pdf
Assume Hashtable is a simple array of size 8, with indices 0..7. Num.pdfAssume Hashtable is a simple array of size 8, with indices 0..7. Num.pdf
Assume Hashtable is a simple array of size 8, with indices 0..7. Num.pdf
 
Ch8b
Ch8bCh8b
Ch8b
 
Matlab project
Matlab projectMatlab project
Matlab project
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Task 4
Task 4Task 4
Task 4
 
Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAY
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 

More from ezzi552

Execute the following code and identify the errors in the program. D.pdf
Execute the following code and identify the errors in the program. D.pdfExecute the following code and identify the errors in the program. D.pdf
Execute the following code and identify the errors in the program. D.pdfezzi552
 
Essay question The genomes of lots and lots of organisms (mostly ba.pdf
Essay question The genomes of lots and lots of organisms (mostly ba.pdfEssay question The genomes of lots and lots of organisms (mostly ba.pdf
Essay question The genomes of lots and lots of organisms (mostly ba.pdfezzi552
 
Consider the language L = { anb2n n 0 }.Give an implementation.pdf
Consider the language L = { anb2n  n  0 }.Give an implementation.pdfConsider the language L = { anb2n  n  0 }.Give an implementation.pdf
Consider the language L = { anb2n n 0 }.Give an implementation.pdfezzi552
 
Case Study.You are the Chair of the Department of Surgery at a lar.pdf
Case Study.You are the Chair of the Department of Surgery at a lar.pdfCase Study.You are the Chair of the Department of Surgery at a lar.pdf
Case Study.You are the Chair of the Department of Surgery at a lar.pdfezzi552
 
An Unsorted Type ADT is to be extended by the addition of function S.pdf
An Unsorted Type ADT is to be extended by the addition of function S.pdfAn Unsorted Type ADT is to be extended by the addition of function S.pdf
An Unsorted Type ADT is to be extended by the addition of function S.pdfezzi552
 
alue 0.83 points M7-6 Calculating Cost of Goods Available for Sale, E.pdf
alue 0.83 points M7-6 Calculating Cost of Goods Available for Sale, E.pdfalue 0.83 points M7-6 Calculating Cost of Goods Available for Sale, E.pdf
alue 0.83 points M7-6 Calculating Cost of Goods Available for Sale, E.pdfezzi552
 
1)Using general mass-media (such as news sites) identify a recent co.pdf
1)Using general mass-media (such as news sites) identify a recent co.pdf1)Using general mass-media (such as news sites) identify a recent co.pdf
1)Using general mass-media (such as news sites) identify a recent co.pdfezzi552
 
1. The notion that two network exist in the brain, one for emotional.pdf
1. The notion that two network exist in the brain, one for emotional.pdf1. The notion that two network exist in the brain, one for emotional.pdf
1. The notion that two network exist in the brain, one for emotional.pdfezzi552
 
Which of the following is a bank liabilityA. Reserve deposits at .pdf
Which of the following is a bank liabilityA. Reserve deposits at .pdfWhich of the following is a bank liabilityA. Reserve deposits at .pdf
Which of the following is a bank liabilityA. Reserve deposits at .pdfezzi552
 
What was Eisenhower’s reinsurance plan What was Eisenhower’s r.pdf
What was Eisenhower’s reinsurance plan What was Eisenhower’s r.pdfWhat was Eisenhower’s reinsurance plan What was Eisenhower’s r.pdf
What was Eisenhower’s reinsurance plan What was Eisenhower’s r.pdfezzi552
 
What does the following program do What’s its time complexity Just.pdf
What does the following program do What’s its time complexity Just.pdfWhat does the following program do What’s its time complexity Just.pdf
What does the following program do What’s its time complexity Just.pdfezzi552
 
What is not true about the membranes of prokaryotic organism a. The.pdf
What is not true about the membranes of prokaryotic organism  a. The.pdfWhat is not true about the membranes of prokaryotic organism  a. The.pdf
What is not true about the membranes of prokaryotic organism a. The.pdfezzi552
 
What behavior characteristics are associated with each of the four s.pdf
What behavior characteristics are associated with each of the four s.pdfWhat behavior characteristics are associated with each of the four s.pdf
What behavior characteristics are associated with each of the four s.pdfezzi552
 
What are someways to better understand accounting There is a lot of.pdf
What are someways to better understand accounting There is a lot of.pdfWhat are someways to better understand accounting There is a lot of.pdf
What are someways to better understand accounting There is a lot of.pdfezzi552
 
Based on the elements from the Ruby Payne readings create a resource.pdf
Based on the elements from the Ruby Payne readings create a resource.pdfBased on the elements from the Ruby Payne readings create a resource.pdf
Based on the elements from the Ruby Payne readings create a resource.pdfezzi552
 
The answer has to be original.For this week’s discussion, complete.pdf
The answer has to be original.For this week’s discussion, complete.pdfThe answer has to be original.For this week’s discussion, complete.pdf
The answer has to be original.For this week’s discussion, complete.pdfezzi552
 
Assume you have a scanner object (called input).Declare an integer.pdf
Assume you have a scanner object (called input).Declare an integer.pdfAssume you have a scanner object (called input).Declare an integer.pdf
Assume you have a scanner object (called input).Declare an integer.pdfezzi552
 
6. Establishing priorities is an issue that local governments strugg.pdf
6. Establishing priorities is an issue that local governments strugg.pdf6. Establishing priorities is an issue that local governments strugg.pdf
6. Establishing priorities is an issue that local governments strugg.pdfezzi552
 
Suppose that A and B are attributes of a certain relational table. G.pdf
Suppose that A and B are attributes of a certain relational table. G.pdfSuppose that A and B are attributes of a certain relational table. G.pdf
Suppose that A and B are attributes of a certain relational table. G.pdfezzi552
 
RHCE is least associated with which Rh group (D--, R1r, Ror, Rzy) .pdf
RHCE is least associated with which Rh group (D--, R1r, Ror, Rzy) .pdfRHCE is least associated with which Rh group (D--, R1r, Ror, Rzy) .pdf
RHCE is least associated with which Rh group (D--, R1r, Ror, Rzy) .pdfezzi552
 

More from ezzi552 (20)

Execute the following code and identify the errors in the program. D.pdf
Execute the following code and identify the errors in the program. D.pdfExecute the following code and identify the errors in the program. D.pdf
Execute the following code and identify the errors in the program. D.pdf
 
Essay question The genomes of lots and lots of organisms (mostly ba.pdf
Essay question The genomes of lots and lots of organisms (mostly ba.pdfEssay question The genomes of lots and lots of organisms (mostly ba.pdf
Essay question The genomes of lots and lots of organisms (mostly ba.pdf
 
Consider the language L = { anb2n n 0 }.Give an implementation.pdf
Consider the language L = { anb2n  n  0 }.Give an implementation.pdfConsider the language L = { anb2n  n  0 }.Give an implementation.pdf
Consider the language L = { anb2n n 0 }.Give an implementation.pdf
 
Case Study.You are the Chair of the Department of Surgery at a lar.pdf
Case Study.You are the Chair of the Department of Surgery at a lar.pdfCase Study.You are the Chair of the Department of Surgery at a lar.pdf
Case Study.You are the Chair of the Department of Surgery at a lar.pdf
 
An Unsorted Type ADT is to be extended by the addition of function S.pdf
An Unsorted Type ADT is to be extended by the addition of function S.pdfAn Unsorted Type ADT is to be extended by the addition of function S.pdf
An Unsorted Type ADT is to be extended by the addition of function S.pdf
 
alue 0.83 points M7-6 Calculating Cost of Goods Available for Sale, E.pdf
alue 0.83 points M7-6 Calculating Cost of Goods Available for Sale, E.pdfalue 0.83 points M7-6 Calculating Cost of Goods Available for Sale, E.pdf
alue 0.83 points M7-6 Calculating Cost of Goods Available for Sale, E.pdf
 
1)Using general mass-media (such as news sites) identify a recent co.pdf
1)Using general mass-media (such as news sites) identify a recent co.pdf1)Using general mass-media (such as news sites) identify a recent co.pdf
1)Using general mass-media (such as news sites) identify a recent co.pdf
 
1. The notion that two network exist in the brain, one for emotional.pdf
1. The notion that two network exist in the brain, one for emotional.pdf1. The notion that two network exist in the brain, one for emotional.pdf
1. The notion that two network exist in the brain, one for emotional.pdf
 
Which of the following is a bank liabilityA. Reserve deposits at .pdf
Which of the following is a bank liabilityA. Reserve deposits at .pdfWhich of the following is a bank liabilityA. Reserve deposits at .pdf
Which of the following is a bank liabilityA. Reserve deposits at .pdf
 
What was Eisenhower’s reinsurance plan What was Eisenhower’s r.pdf
What was Eisenhower’s reinsurance plan What was Eisenhower’s r.pdfWhat was Eisenhower’s reinsurance plan What was Eisenhower’s r.pdf
What was Eisenhower’s reinsurance plan What was Eisenhower’s r.pdf
 
What does the following program do What’s its time complexity Just.pdf
What does the following program do What’s its time complexity Just.pdfWhat does the following program do What’s its time complexity Just.pdf
What does the following program do What’s its time complexity Just.pdf
 
What is not true about the membranes of prokaryotic organism a. The.pdf
What is not true about the membranes of prokaryotic organism  a. The.pdfWhat is not true about the membranes of prokaryotic organism  a. The.pdf
What is not true about the membranes of prokaryotic organism a. The.pdf
 
What behavior characteristics are associated with each of the four s.pdf
What behavior characteristics are associated with each of the four s.pdfWhat behavior characteristics are associated with each of the four s.pdf
What behavior characteristics are associated with each of the four s.pdf
 
What are someways to better understand accounting There is a lot of.pdf
What are someways to better understand accounting There is a lot of.pdfWhat are someways to better understand accounting There is a lot of.pdf
What are someways to better understand accounting There is a lot of.pdf
 
Based on the elements from the Ruby Payne readings create a resource.pdf
Based on the elements from the Ruby Payne readings create a resource.pdfBased on the elements from the Ruby Payne readings create a resource.pdf
Based on the elements from the Ruby Payne readings create a resource.pdf
 
The answer has to be original.For this week’s discussion, complete.pdf
The answer has to be original.For this week’s discussion, complete.pdfThe answer has to be original.For this week’s discussion, complete.pdf
The answer has to be original.For this week’s discussion, complete.pdf
 
Assume you have a scanner object (called input).Declare an integer.pdf
Assume you have a scanner object (called input).Declare an integer.pdfAssume you have a scanner object (called input).Declare an integer.pdf
Assume you have a scanner object (called input).Declare an integer.pdf
 
6. Establishing priorities is an issue that local governments strugg.pdf
6. Establishing priorities is an issue that local governments strugg.pdf6. Establishing priorities is an issue that local governments strugg.pdf
6. Establishing priorities is an issue that local governments strugg.pdf
 
Suppose that A and B are attributes of a certain relational table. G.pdf
Suppose that A and B are attributes of a certain relational table. G.pdfSuppose that A and B are attributes of a certain relational table. G.pdf
Suppose that A and B are attributes of a certain relational table. G.pdf
 
RHCE is least associated with which Rh group (D--, R1r, Ror, Rzy) .pdf
RHCE is least associated with which Rh group (D--, R1r, Ror, Rzy) .pdfRHCE is least associated with which Rh group (D--, R1r, Ror, Rzy) .pdf
RHCE is least associated with which Rh group (D--, R1r, Ror, Rzy) .pdf
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

In an ancient land, the beautiful princess Eve had many suitors She d.pdf

  • 1. In an ancient land, the beautiful princess Eve had many suitors She decided on the following procedure to determine which suitor she would marry First, all of the suitors would be lined up on after the other and assigned numbers The first suitor would be number 1, the second number 2, and so on up to the last suitor, number n starting at the first suitor she would then count three suitors down the line (because of the three letters in her name) and the third suitor would be eliminated from winning her hand and removed from the line Eve would then continue, counting three more suitors, and eliminating every third suitor When she reached the end of the line she would continue counting from the beginning. For example, if there were six suitors then the elimination process would proceed as follows: Write a program that creates a circular linked list of nodes to determine which position you should stand in to marry the princess if there are n suitors Your program should simulate the elimination process by deleting the node that corresponds to the suitor that is eliminated for each step in the process. Solution import java.util.Scanner; import java.util.Stack; public class CircularLinkedListImpl { private Node node = null; private Node head = null; private Node tail = null; private static int listCount = 0; public CircularLinkedListImpl() { } /* Function to delete element at position */ public void deleteAtPos(int pos) { if (this.listCount == 1 && pos == 1) { head = null; tail = null; this.listCount = 0; return; } if (pos == 1) { head = this.head.next; tail.next = head; listCount--;
  • 2. return; } if (pos == listCount) { Node s = head; Node t = head; while (s != tail) { t = s; s = s.next; } tail = t; tail.next = head; listCount--; return; } Stack> stack = new Stack>(); Node ptr = head.next; pos = pos - 1; for (int i = 1; i < listCount - 1; i++) { if (i == pos) { System.out.println("kfdsf" + ptr.item); Node tmp = ptr.next; tmp = tmp.next; ptr.next = tmp; break; } stack.push(ptr); } listCount--; } public boolean add(E element) { boolean isItemAdded = false; Node tempNode = new Node(element); Node node = this.node; if (node == null) { this.node = tempNode; this.head = this.node;
  • 3. this.tail = this.node; CircularLinkedListImpl.listCount++; isItemAdded = true; } else { while (node.next != null) { System.out.println(node.next.item); node = node.next; } tempNode.next = node.next; node.next = tempNode; this.tail = tempNode; CircularLinkedListImpl.listCount++; isItemAdded = true; } return isItemAdded; } public int size() { return CircularLinkedListImpl.listCount++; } public String toString() { String output = ""; if (this.node != null) { Node crunchifyCurrent = this.node; while (crunchifyCurrent != null) { output += "[" + crunchifyCurrent.item.toString() + "]"; crunchifyCurrent = crunchifyCurrent.next; } } return output; } public static void main(String[] args) { CircularLinkedListImpl c = new CircularLinkedListImpl(); Scanner scan = new Scanner(System.in); String str = scan.next(); int length = str.length(); long bInetger = Long.parseLong(str);
  • 4. scan.close(); long ll = getDigit(length); while (ll > 0) { long value = bInetger / ll; bInetger = bInetger % ll; ll = ll / 10; c.add(value); } System.out.println(" List " + c.toString()); c.deleteAtPos(3); System.out.println("After deleting List " + c.toString()); } private static class Node { E item; Node next; Node() { } Node(E element) { this.item = element; this.next = next; // this.prev = prev; } } public static long getDigit(int length) { StringBuilder ll = new StringBuilder(); ll.append(1); for (int i = 1; i < length; i++) { ll.append(0); } return Long.parseLong(ll.toString()); } }