Home > Back-end >  Programming with two-way linked list or cycle
Programming with two-way linked list or cycle

Time:10-16

Solution:
Joseph10 individuals, numbered from 1 to 10, from 1 began to count, count up to a third was killed, ask the last remaining number? Programming implementation, using two-way linked list, cycle will do

CodePudding user response:

 
Void __fastcall TForm1: : Button1Click (TObject * Sender)
{
Struct PEOPLE
{int people;
PEOPLE * next;
};
PEOPLE PEOPLE [10].
for(int i=0; I<9. I++)
{people [I] people=I + 1;
People [I] next=& amp; People [I + 1);
}
People [9]. Next=& amp; People [0].
People [9]. People=10;
//start killing
PEOPLE * p=& amp; People [1].
While (p - & gt; Next!=p)
{
//remove the next node
ListBox1 - & gt; AddItem (L "kill:" + IntToStr (p - & gt; Next - & gt; People), NULL);
P - & gt; Next=p - & gt; Next - & gt; Next;
P=p - & gt; Next - & gt; next;
}
ListBox1 - & gt; AddItem (L "the last remaining:" + IntToStr (p - & gt; People), NULL);
}

Cb2010 debugging through, results:
Kill: 3
Kill: 6
Kill: 9
Kill: 2
Kill: 7
Kill: 1
Kill: 8
Kill 5
Kill: 10
The last remaining: 4
  • Related