-
الگوریتم شانس دوم
کد:
// stdafx.cpp : source file that includes just the standard includes
// ConsoleApplication4.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h" //hossein mahdavi
#include "targetver.h"
using namespace std;
#include <iostream>
int n,i;
struct listpeivandi
{
int num;
bool flag;
listpeivandi *next;
};
listpeivandi *first=NULL;
listpeivandi *last=NULL;
listpeivandi *temp1=NULL;
void add( )
{
listpeivandi *temp;
temp = new listpeivandi;
cout<<" enter num and flag";
cin>>temp->num>>temp->flag; //خواندن num و flage
if( first == NULL )//اگر عضو اول لیست باشد
{
first = temp;
first->next = NULL;
last = first;
}
else//اگر عضو اول لیست نباشد
{
last->next = temp;
last = temp;
last->next = NULL;
}
}
void showlist()
{
listpeivandi *temp;
temp = new listpeivandi;
temp=first;
while(temp->next!=NULL) //تا زمانی که next به null اشاره نمیکنه
{
cout<<temp->num<<" "<<temp->flag<<"\n"; // num و flage رو چاپ کن
temp=temp->next;
} // وقتی که برابر null شد
cout<<last->num<<" "<<last->flag<<"\n"; ///num و flage روlast چاپ کن
cout<<"==========================================\n"; // سطر جدید
}
void main()
{
cout<<"enter number of nodes ---->";
cin>>n; //تعداد گره ها را میگیرد
for(i=1;i<=n;i++)
add();
showlist();
cout<<"enter number of repeat second shance ---->";
int j;
cin>>j;
for(int i=1;i<=j;i++)
{
if(first->flag==1) //اگر بیت دسترسی صفحه 1باشد باید بیت دسترسی آن صفر شده و به انتهای صف برود
{
first->flag=0;
listpeivandi *temp;
temp = new listpeivandi;
temp=first;
first=first->next;
temp->next=NULL; //قرار است در آخر لیست قرار بگیرد
last->next=temp; //آخرین خانه به این گره اشاره کند
last=temp; //این گره last میشود
showlist(); //نمایش لیست
continue;
}
if(first->flag==0) //اگر بیت دسترسی صفحه صفر باشد صفحه باید حذف شود
first=first->next;
showlist();
}//end of for
}//end of main